gpt4 book ai didi

swift - CAShapeLayer 动画不会触发 animationDidStop

转载 作者:行者123 更新时间:2023-11-28 10:16:57 27 4
gpt4 key购买 nike

使用 Swift 3.0 运行 tvOS

基本应用程序,这是 View Controller 中的代码...我想在 CAShapeLayer 淡出后删除它。动画完美运行,但是完成方法 animationDidStop 永远不会被调用?

想使用override func animationDidStop 方法,但无法编译。给我错误“方法没有覆盖其父类(super class)中的任何方法”,这肯定是错误的,animationDidStopCAAnimation 类的成员。

这段代码有什么问题?我可以用计时器或我猜的其他东西来解决这个问题,但这是一个软糖。

import UIKit
import QuartzCore


class ViewController: UIViewController, CALayerDelegate, CAAnimationDelegate {

var shapeLayer = CAShapeLayer()


override func viewDidLoad() {
super.viewDidLoad()
highlight(XCord: 100, YCord: 100)
clearhighlight()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func highlight(XCord: CGFloat, YCord: CGFloat) {
DispatchQueue.main.async {
let circlePath = UIBezierPath(arcCenter: CGPoint(x: XCord,y: YCord), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

self.shapeLayer.path = circlePath.cgPath

//change the fill color
//shapeLayer.fillColor = UIColor.clear.cgColor
self.shapeLayer.fillColor = UIColor.red.cgColor
self.shapeLayer.opacity = 0.5
self.shapeLayer.fillRule = kCAFillRuleEvenOdd

//you can change the stroke color
self.shapeLayer.strokeColor = UIColor.red.cgColor
//you can change the line width
self.shapeLayer.lineWidth = 3.0

DispatchQueue.main.async {
self.view.layer.addSublayer(self.shapeLayer)
}
}
}

func clearhighlight() {
DispatchQueue.main.async {
let fadeInAndOut = CABasicAnimation(keyPath: "opacity")
fadeInAndOut.duration = 10.0;
//fadeInAndOut.autoreverses = true;
fadeInAndOut.repeatCount = 1
fadeInAndOut.fromValue = 1.0
fadeInAndOut.toValue = 0.0
fadeInAndOut.isRemovedOnCompletion = true
fadeInAndOut.fillMode = kCAFillModeForwards;
//self.view.layer.delegate = self
self.shapeLayer.delegate = self
self.shapeLayer.add(fadeInAndOut, forKey: "fadeOut")
self.shapeLayer.setNeedsDisplay()
}
}

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
print("animationDidStop")
DispatchQueue.main.async {
self.shapeLayer.removeFromSuperlayer()
}
}



}

最佳答案

您正在设置形状层的委托(delegate):

self.shapeLayer.delegate = self

但是如果你想调用animationDidStop,你必须设置动画的delegate,而不是层的:

fadeInAndOut.delegate = self

你说:

Want to use override func animationDidStop method, but it won't compile. Gives me the error "method does not override any method from its superclass" which is wrong surely, animationDidStop is a member of CAAnimation class.

您正在您的 View Controller 中实现它并且 UIViewController 不提供 animationDidStop 的基类实现,因此没有什么可以覆盖。

animationDidStop 是由 CAAnimationDelegate 协议(protocol)定义的方法,因此 (a) View Controller 应声明其符合该协议(protocol); (b) 不要使用 override 关键字。

关于swift - CAShapeLayer 动画不会触发 animationDidStop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40308176/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com