Animation swift4指令无效
我在视图上使用了层动画,但在swift4上无法工作 我的视图覆盖layerClass属性Animation swift4指令无效,animation,layer,swift4,Animation,Layer,Swift4,我在视图上使用了层动画,但在swift4上无法工作 我的视图覆盖layerClass属性 public override class var layerClass: Swift.AnyClass { return CoordLayer.self } 和自定义关键点创建关键帧动画 let animation = CAKeyframeAnimation(keyPath: "moveX") animation.timingFunction = CAMediaTimingFunction(na
public override class var layerClass: Swift.AnyClass {
return CoordLayer.self
}
和自定义关键点创建关键帧动画
let animation = CAKeyframeAnimation(keyPath: "moveX")
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
animation.values = values
animation.keyTimes = times.map{ NSNumber(value: $0) }
animation.duration = duration
animation.fillMode = kCAFillModeForwards
animation.delegate = self
self.layer.add(animation, forKey: "moveX")
CoordLayer类
class CoordLayer: CALayer {
override class func needsDisplay(forKey key: String) -> Bool {
if "moveX" == key {
return true // ..... swift4 No call ....
}
return super.needsDisplay(forKey: key)
}
}
override func display(){
// .......some custom operations......
// ....... swift4 No call .......
}
}
视图没有动画效果swift4关键帧动画在swift4中也按预期工作: 请检查实现,您需要在实现中进行一些更正 让我们看看 定义控制器: 定义MyView: 定义协作层: 动画代理: 执行中的更正: 您也可以在MyView中添加动画逻辑,因为您正在层上添加动画。动画委托可以由MyView确认。(由您决定) 谢谢。:-)
谢谢,使用“transform.translation.x”代替“moveX”是可行的,但在swift4之前,我使用了自定义关键点并覆盖了类func needsDisplay(forKey:String)->Bool自定义关键点reutrn是可行的我的视图自定义动画,使用“transform.translation.x”不可行,因为视图是MapPinView,我将坐标转换为视图场,并使用层动画使管脚视图平滑移动覆盖func display(){conver coordiante set layer position}我需要自定义关键点路径和覆盖类func needsDisplay(forKey:String)->布尔自定义关键点重新设置,func display()需要一直被调用我已经解决了这个问题。CoordLayer继承CALayer,CALayer继承NSObject swift4 NSObject称为swift字符串(例如自定义键“movex”),CoordLayer需要@objcMembers类CoordLayer:CALayer,它可以工作!是的,@objc是必需的。谢谢。我已经解决了这个问题。CoordLayer继承CALayer,CALayer继承NSObject swift4 NSObject称为swift字符串(例如自定义键“movex”),CoordLayer需要@objcMembers类CoordLayer:CALayer,它可以工作!
import UIKit
// Controller
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addView()
}
private let myView : MyView = {
let view = MyView()
view.translatesAutoresizingMaskIntoConstraints = false // enable auto layout
view.backgroundColor = .red
return view
}() // execute closure
private func addView() {
view.addSubview(myView)
NSLayoutConstraint.activate([
myView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor), // leading
myView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor), // top
myView.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor, multiplier: 0.2), // width = 20 % of view width
myView.heightAnchor.constraint(equalTo: myView.widthAnchor, multiplier: 1.0) // height = width
])
}
}
// View
class MyView : UIView {
// draw view
override func draw(_ rect: CGRect) {
super.draw(rect)
}
// layerClass
public override class var layerClass: Swift.AnyClass {
return CoordLayer.self
}
}
// Layer
class CoordLayer : CALayer {
override func display() {
// some custom operations
backgroundColor = UIColor.red.cgColor // to visualize
customAnimation() // add animation
print("Display")
}
override class func needsDisplay(forKey key: String) -> Bool {
if key == "transform.translation.x" {
return true
}
return super.needsDisplay(forKey: key)
}
// add animation
private func customAnimation() {
let values = [100,150,200,250]
let times : [NSNumber] = [0.0, 0.25, 0.5, 1.0] // not necessary always
let animation = CAKeyframeAnimation(keyPath: "transform.translation.x") // moving view along x direction
animation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)] // array of timing function
animation.values = values // taking the animated property values for animation
animation.keyTimes = times // define the timing array
animation.duration = 5.0 // CFTimeInterval
animation.isRemovedOnCompletion = false // do not remove the animation effect, no state changes.
animation.fillMode = kCAFillModeForwards
animation.delegate = self
// add animation on coordLayer
self.add(animation, forKey: "animation")
}
}
extension CoordLayer : CAAnimationDelegate {
func animationDidStart(_ anim: CAAnimation) {
print("Animation started")
}
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
print("Animation stopped")
}
}
(1) Instead of "moveX", use "transform.translation.x"
(2) Give some animation duration, animation.duration = 5.0 // CFTimeInterval
(3) animation.isRemovedOnCompletion = false , so that after animation, view will not move it's beginning position.