Various improvements

This commit is contained in:
Ilya Laktyushin
2023-07-16 03:37:43 +02:00
parent b263f37d73
commit f801c81a9d
6 changed files with 65 additions and 21 deletions

View File

@@ -226,7 +226,7 @@ public extension CALayer {
self.add(animationGroup, forKey: key)
}
func animateKeyframes(values: [AnyObject], duration: Double, keyPath: String, timingFunction: String = CAMediaTimingFunctionName.linear.rawValue, mediaTimingFunction: CAMediaTimingFunction? = nil, removeOnCompletion: Bool = true, additive: Bool = false, completion: ((Bool) -> Void)? = nil) {
func animateKeyframes(values: [AnyObject], keyTimes: [NSNumber]? = nil, duration: Double, keyPath: String, timingFunction: String = CAMediaTimingFunctionName.linear.rawValue, mediaTimingFunction: CAMediaTimingFunction? = nil, removeOnCompletion: Bool = true, additive: Bool = false, completion: ((Bool) -> Void)? = nil) {
let k = Float(UIView.animationDurationFactor())
var speed: Float = 1.0
if k != 0 && k != 1 {
@@ -235,17 +235,21 @@ public extension CALayer {
let animation = CAKeyframeAnimation(keyPath: keyPath)
animation.values = values
var keyTimes: [NSNumber] = []
for i in 0 ..< values.count {
if i == 0 {
keyTimes.append(0.0)
} else if i == values.count - 1 {
keyTimes.append(1.0)
} else {
keyTimes.append((Double(i) / Double(values.count - 1)) as NSNumber)
var effectiveKeyTimes: [NSNumber] = []
if let keyTimes {
effectiveKeyTimes = keyTimes
} else {
for i in 0 ..< values.count {
if i == 0 {
effectiveKeyTimes.append(0.0)
} else if i == values.count - 1 {
effectiveKeyTimes.append(1.0)
} else {
effectiveKeyTimes.append((Double(i) / Double(values.count - 1)) as NSNumber)
}
}
}
animation.keyTimes = keyTimes
animation.keyTimes = effectiveKeyTimes
animation.speed = speed
animation.duration = duration
animation.isAdditive = additive