首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >动画CALayer不适用于iOS 14

动画CALayer不适用于iOS 14
EN

Stack Overflow用户
提问于 2020-09-17 11:55:42
回答 1查看 639关注 0票数 0

在更新到Xcode12和iOS14之后,我在动画Xcode12和iOS14上遇到了问题--过去在iOS13上工作的动画并不适用于iOS14。下面是动画的代码:

代码语言:javascript
代码运行次数:0
运行
复制
private func animateCellPress() {
    let lightShadowAnimation = CABasicAnimation(keyPath: "shadowOffset")
    lightShadowAnimation.fromValue = CGSize(width: -self.shadowRadius, height: -self.shadowRadius)
    lightShadowAnimation.toValue = CGSize(width: self.shadowRadius, height: self.shadowRadius)
    lightShadowAnimation.fillMode = .forwards;
    lightShadowAnimation.isRemovedOnCompletion = false;
    lightShadowAnimation.duration = self.animationDuration
    self.lightShadow.add(lightShadowAnimation, forKey: lightShadowAnimation.keyPath)

    let darkShadowAnimation = CABasicAnimation(keyPath: "shadowOffset")
    darkShadowAnimation.fromValue = CGSize(width: self.shadowRadius, height: self.shadowRadius)
    darkShadowAnimation.toValue = CGSize(width: -self.shadowRadius, height: -self.shadowRadius)
    darkShadowAnimation.fillMode = .forwards;
    darkShadowAnimation.isRemovedOnCompletion = false;
    darkShadowAnimation.duration = self.animationDuration
    self.darkShadow.add(darkShadowAnimation, forKey: darkShadowAnimation.keyPath)
}

lightShadow和darkShadow作为属性存储,下面是它们的初始化代码:

代码语言:javascript
代码运行次数:0
运行
复制
private func initializeDarkShadow() {
    darkShadow = CALayer()
    darkShadow.frame = bounds
    darkShadow.backgroundColor = backgroundColor?.cgColor
    darkShadow.cornerRadius = cornerRadius
    darkShadow.shadowOffset = CGSize(width: shadowRadius, height: shadowRadius)
    darkShadow.shadowOpacity = 1
    darkShadow.shadowRadius = shadowRadius
    darkShadow.shadowColor = Asset.Colors.darkShadow.color.cgColor
    let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: bounds.size)
    darkShadow.shadowPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
    layer.insertSublayer(darkShadow, at: 0)
}

private func initializeLightShadow() {
    lightShadow = CALayer()
    lightShadow.frame = bounds
    lightShadow.backgroundColor = backgroundColor?.cgColor
    lightShadow.cornerRadius = cornerRadius
    lightShadow.shadowOffset = CGSize(width: -shadowRadius, height: -shadowRadius)
    lightShadow.shadowOpacity = 1
    lightShadow.shadowRadius = shadowRadius
    lightShadow.shadowColor = Asset.Colors.lightShadow.color.cgColor
    let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: bounds.size)
    lightShadow.shadowPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
    layer.insertSublayer(lightShadow, at: 0)
}

有什么不对的地方吗?我该怎么解决?此外,我不会收到任何编译器错误或警告。控制台日志也是空的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-17 13:26:59

这个很管用。我认为每次您调用动画时,都会调用layoutSubviews,从而删除图层和动画。您可以只更新那里的框架,或者检查该层是否已经添加。如果这不是问题,那么你的按压或触摸功能就有问题。创建一个具有单个视图的项目,并将其复制并粘贴到ViewController文件中。

代码语言:javascript
代码运行次数:0
运行
复制
import UIKit

class TableViewCell : UITableViewCell{
    let darkShadowColor = UIColor.green
    let lightShadowColor = UIColor.red
    let animationDuration : Double = 2
    
    var darkShadow = CALayer()
    var lightShadow = CALayer()
    let cornerRadius : CGFloat = 4
    let shadowRadius : CGFloat = 3
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeDarkShadow()
        initializeLightShadow()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initializeDarkShadow()
        initializeLightShadow()
    }
    
    private func initializeDarkShadow() {
        darkShadow = CALayer()
        darkShadow.frame = bounds
        darkShadow.backgroundColor = backgroundColor?.cgColor
        darkShadow.cornerRadius = cornerRadius
        darkShadow.shadowOffset = CGSize(width: shadowRadius, height: shadowRadius)
        darkShadow.shadowOpacity = 1
        darkShadow.shadowRadius = shadowRadius
        darkShadow.shadowColor = darkShadowColor.cgColor
        let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: bounds.size)
        darkShadow.shadowPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
        layer.insertSublayer(darkShadow, at: 0)
    }

    private func initializeLightShadow() {
        lightShadow = CALayer()
        lightShadow.frame = bounds
        lightShadow.backgroundColor = backgroundColor?.cgColor
        lightShadow.cornerRadius = cornerRadius
        lightShadow.shadowOffset = CGSize(width: -shadowRadius, height: -shadowRadius)
        lightShadow.shadowOpacity = 1
        lightShadow.shadowRadius = shadowRadius
        lightShadow.shadowColor = lightShadowColor.cgColor
        let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: bounds.size)
        lightShadow.shadowPath = UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath
        layer.insertSublayer(lightShadow, at: 0)
    }
    
    func animateCellPress() {
        print("called")
        let lightShadowAnimation = CABasicAnimation(keyPath: "shadowOffset")
        lightShadowAnimation.fromValue = CGSize(width: -self.shadowRadius, height: -self.shadowRadius)
        lightShadowAnimation.toValue = CGSize(width: self.shadowRadius, height: self.shadowRadius)
        lightShadowAnimation.beginTime = CACurrentMediaTime()
        lightShadowAnimation.fillMode = .both;
        lightShadowAnimation.isRemovedOnCompletion = false;
        lightShadowAnimation.duration = self.animationDuration
        self.lightShadow.add(lightShadowAnimation, forKey: nil)


        let darkShadowAnimation = CABasicAnimation(keyPath: "shadowOffset")
        darkShadowAnimation.fromValue = CGSize(width: self.shadowRadius, height: self.shadowRadius)
        darkShadowAnimation.toValue = CGSize(width: -self.shadowRadius, height: -self.shadowRadius)
        darkShadowAnimation.beginTime = CACurrentMediaTime()
        darkShadowAnimation.fillMode = .both;
        darkShadowAnimation.isRemovedOnCompletion = false;
        darkShadowAnimation.duration = self.animationDuration
        self.darkShadow.add(darkShadowAnimation, forKey: nil)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.lightShadow.frame = self.bounds
        self.darkShadow.frame = self.bounds
    }
}

class ViewController: UIViewController {

    let identifier = "someCellID"
    lazy var tableView : UITableView = {
        let tv = UITableView(frame: self.view.bounds)
        tv.delegate = self
        tv.dataSource = self
        tv.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        tv.register(TableViewCell.self, forCellReuseIdentifier: identifier)
        return tv
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.addSubview(tableView)
    }
}

extension ViewController : UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? TableViewCell{
            cell.selectionStyle = .none
            return cell
        }
        print("error")
        return UITableViewCell()
    }
    
    
    
    func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) as? TableViewCell{
            cell.animateCellPress()
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63937516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档