Core Animation 또는 UIDynamics를 사용하여 다른 매개 변수를 제어하는 데 사용할 수있는 애니메이션 값 (float)을 생성하고 싶습니다.애니메이션을 사용하여 보간 값 생성하기
예. 쉽게 애니메이션을 가지고, 나는 '완화'값으로 색상을 제어하고 싶습니다.
Core Animation 또는 UIDynamics를 사용하여 다른 매개 변수를 제어하는 데 사용할 수있는 애니메이션 값 (float)을 생성하고 싶습니다.애니메이션을 사용하여 보간 값 생성하기
예. 쉽게 애니메이션을 가지고, 나는 '완화'값으로 색상을 제어하고 싶습니다.
가장 좋은 추측은 CADisplayLink입니다. 이 post을 참조하십시오. KVO는 애니메이션의 변경 사항을 관찰 할 수 없습니다.
Core Animation이 확실히 당신을 대신 할 수 있습니다. (스위프트에서 @NSManaged
는 오브젝티브 C @dynamic
속성으로 colorPercentage
을 치료하는 코어 애니메이션 현재 필요한 해킹
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
서브 클래스를 작성해야합니다.)
이 패턴에 따라 고유 한 애니메이션 가능 속성이 포함 된 사용자 지정 레이어를 만들 수 있습니다. 보너스로
let animation = CABasicAnimation(keyPath: "colorPercentage")
animation.duration = 1.0
animation.fromValue = customLayer.colorPercentage
customLayer.colorPercentage = 1.0
customLayer.add(animation, forKey: "colorPercentageAnimation")
, action(forKey:)
이에서 작업을 생성 할 수 있습니다 : display()
은 그러나 당신이 원하는 메인 스레드에서 애니메이션의 프레임마다 호출됩니다, 그래서 당신은 시간이 지남에 따라 값이 변경에 응답 할 수 있습니다 Core Animation이 알고있는 "참조 애니메이션"이며이를 사용자 지정 속성과 함께 작동하도록 구성합니다. 이와 함께, 훨씬 더 사용하기 편리 UIKit 스타일의 애니메이션 폐쇄 내부 CALayer
애니메이션, 호출 가능 :이 ** 모니터링을위한 가장 유용한
UIView.animate(withDuration: 1.0, animations: {
customLayer.colorPercentage = 1.0
})
를 ** 기존의 애니메이션 속성의 프리젠 테이션 값, 일부보기의 불투명도와 마찬가지로 해당 값의 시각적 변화에 응답합니다. 그러나 커스텀 프로퍼티의 경우, 애니메이션 가능한 것과 같은 커스텀 프로퍼티를 정의하는'CALayer' 서브 클래스를 만드는 것이 더 깔끔합니다. – LucasTizma