Core AnimationまたはUIDynamicsを使用して他のパラメータを制御するために使用できるアニメーション値(浮動小数点数)を生成したいと考えています。アニメーションを使用した補間値の生成
など。簡単にアニメーションができたら、私は「緩和された」値で色をコントロールしたいと思います。
Core AnimationまたはUIDynamicsを使用して他のパラメータを制御するために使用できるアニメーション値(浮動小数点数)を生成したいと考えています。アニメーションを使用した補間値の生成
など。簡単にアニメーションができたら、私は「緩和された」値で色をコントロールしたいと思います。
CADisplayLinkを使用することをお勧めします。このpostを参照してください。 KVOはアニメーションの変更を観察することはできません。
コアアニメーションは確かにあなたのためにこれを行うことができます。 (スウィフトでは、@NSManaged
は、Objective-Cの@dynamic
プロパティとしてcolorPercentage
を治療するためのCore Animationのために、現在必要なハックです
class CustomLayer: CALayer {
@NSManaged var colorPercentage: Float
override required init(layer: AnyObject) {
super.init(layer: layer)
if let layer = layer as? CustomLayer {
colorPercentage = layer.colorPercentage
} else {
colorPercentage = 0.0
}
}
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
}
override class func needsDisplay(forKey key: String) -> Bool {
var needsDisplay = super.needsDisplay(forKey: key)
if key == "colorPercentage" {
needsDisplay = true
}
return needsDisplay
}
override func action(forKey key: String) -> CAAction? {
var action = super.action(forKey: key)
if key == "colorPercentage" {
action = super.action(forKey: "opacity") // Create reference action from an existing CALayer key
if let action = action as? CABasicAnimation {
action.keyPath = key
action.fromValue = value(forKeyPath: key)
}
}
return action
}
override func display() {
guard let presentationLayer = presentation() else { return }
print("Current 'colorPercentage' value: \(presentationLayer.colorPercentage)")
// Called for every frame of an animation involving "colorPercentage"
}
}
:あなたがアニメーション化したい新しいプロパティを持っているCALayer
サブクラスを作成する必要があります。 )
このパターンに従って、独自のアニメーション可能なプロパティを含むカスタムレイヤーを作成できます。あなたが欲しいしかし、時間の経過とともに値の変化に対応できるようdisplay()
は、メインスレッド上のアニメーションのフレームごとに呼び出されます。ボーナスとして
let animation = CABasicAnimation(keyPath: "colorPercentage")
animation.duration = 1.0
animation.fromValue = customLayer.colorPercentage
customLayer.colorPercentage = 1.0
customLayer.add(animation, forKey: "colorPercentageAnimation")
、action(forKey:)
はからアクションを作成するために使用することができますCore Animationが知っている「参照アニメーション」とそれをあなたのカスタムプロパティで動作するように設定します。これにより、はるかに便利使用するようにしているのUIKitスタイルのアニメーションの閉鎖、内部のCALayer
アニメーションを起動することが可能です:
UIView.animate(withDuration: 1.0, animations: {
customLayer.colorPercentage = 1.0
})
これは**監視のために最も有用であろう**既存のアニメーション可能プロパティのプレゼンテーション値、一部のビューの不透明度と同様に、その値の視覚的変化に対応します。しかし、カスタムプロパティの場合、アニメーション化可能なカスタムプロパティを定義する 'CALayer'サブクラスを作成する方がクリーンです。 – LucasTizma