在更新到Xcode12和iOS14之后,我在动画Xcode12和iOS14上遇到了问题--过去在iOS13上工作的动画并不适用于iOS14。下面是动画的代码:
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作为属性存储,下面是它们的初始化代码:
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)
}
有什么不对的地方吗?我该怎么解决?此外,我不会收到任何编译器错误或警告。控制台日志也是空的。
发布于 2020-09-17 05:26:59
这个很管用。我认为每次您调用动画时,都会调用layoutSubviews,从而删除图层和动画。您可以只更新那里的框架,或者检查该层是否已经添加。如果这不是问题,那么你的按压或触摸功能就有问题。创建一个具有单个视图的项目,并将其复制并粘贴到ViewController文件中。
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
}
}
https://stackoverflow.com/questions/63937516
复制相似问题