【发布时间】:2017-05-26 09:25:11
【问题描述】:
我有一个 UIView,它有一个图层为我做所有的绘图。因此,现在当我更改视图的值时,它会将其转发到图层。当我在 UIView-Animaiton 块中时,我希望该更改具有动画效果。
那么我目前在做什么:
class SampleView: UIView {
var myProperty: CGFloat {
set {
sampleLayer.myProperty = newValue
}
get {
return sampleLayer.myProperty
}
}
override class var layerClass: AnyClass {
return SampleViewLayer.self
}
private var sampleLayer: SampleViewLayer {
return layer as! SampleViewLayer
}
}
class SampleViewLayer: CALayer {
@NSManaged public var myProperty: CGFloat
override func draw(in ctx: CGContext) {
// Incredible beautiful drawing is happening here depending on myProperty
}
override static func needsDisplay(forKey key: String) -> Bool {
if key == "myProperty" {
return true
}
return super.needsDisplay(forKey: key)
}
override func action(forKey event: String) -> CAAction? {
if event == "myProperty" {
// 1. How do I find out if the change should be animated?
let animation = CABasicAnimation(keyPath: event)
animation.fromValue = presentation()?.value(forKey: event)
// 2. How do I find out the proper timingFunction as well as the proper duration?
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
animation.duration = 1.0
return animation
}
return super.action(forKey: event)
}
}
我希望能够像这样对其进行动画处理:
....
instanceOfMySampleView.myProperty = 0 // inital value should be set without animation
UIView.animate(withDuration: 4, animations: {
self.instanceOfMySampleView.myProperty = 10 // should animate to this value
})
....
所以这两个实际问题在我的示例代码中...... 1. 我如何确定更改是否应该被动画化?那么如何判断它发生在 UIView-Animation-Block 内部呢?
- 如果是动画,如何为 duration 和 timingFunction 找到合适的参数?
还有。如果我在代码中创建我的视图实例,将其 myProperty 值设置为初始值,然后直接执行 UIViewAnimationBlock,PresentationLayer 将为 nil,因此我的动画将无法正常工作...
【问题讨论】:
标签: ios calayer uiviewanimation caanimation