我有一個UIView,正在爲我完成所有的繪圖。所以現在當我改變我的視圖的值時,它會將其轉發給圖層。而當我在UIView-Animaiton Block中時,我希望這種變化能夠成爲動畫。自定義CALayer動畫
所以我現在做的事:
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內部?
- 如果它是動畫,如何找到持續時間和timing功能的正確參數?
另外。如果我在代碼中創建View的實例,將它的myProperty值設置爲初始值,然後直接執行UIViewAnimationBlock,則PresentationLayer將爲零,所以我的動畫將無法正常工作...
好吧,不是我希望的答案,但謝謝你的幫助!我會稍微等一下,看看其他人是否可能知道是否/在哪裏訪問這些值,如果沒有,我當然會接受你的答案!謝謝! – Georg
@Georg同意,把它留在這裏,希望有更好的一個。不過,我建議你無論如何都要把它包起來。至少你可以在這裏設置默認值,例如動畫持續時間。人們傾向於在整個應用程序中粘貼相同的值。所以基本上你可以實現整個事情,如果你找到一個合適的解決方案,你需要做的就是改變屬性的getters。 –