如何为 UIViews 'center' 属性 设置动画?
How to animate a UIViews 'center' property?
正如我在 Apple documentation 中读到的那样,我能够为 UIView
.
的 'center' 属性 设置动画
我正在使用 CAKeyframeAnimation
来执行此操作并为该动画提供三个关键帧和三个 CGPoint。
动画似乎不工作。我是不是做错了什么?
NSMutableArray* values = [NSMutableArray new];
[values addObject:@(CGPointMake(0, 0))];
[values addObject:@(CGPointMake(5, 8))];
[values addObject:@(CGPointMake(10, 8))];
NSMutableArray* keyTimes = [NSMutableArray new];
[keyTimes addObject:@(0)];
[keyTimes addObject:@(0.5)];
[keyTimes addObject:@(1)];
NSMutableArray* timingFunctions = [NSMutableArray new];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
CAKeyframeAnimation* myAimation = [CAKeyframeAnimation animationWithKeyPath:@"center"];
myAimation.duration = 0.5;
myAimation.values = values;
myAimation.timingFunctions = timingFunctions;
myAimation.keyTimes = keyTimes;
[myView.layer addAnimation:myAimation forKey:nil];
尝试将键路径从 "center" 更改为 "position"
我的 swift 代码正在使用这个
func animate() {
var values: [AnyHashable] = []
values.append(NSValue(cgPoint: CGPoint(x: 0, y: 0)))
values.append(NSValue(cgPoint: CGPoint(x: 200, y: 140)))
values.append(NSValue(cgPoint: CGPoint(x: 140, y: 200)))
var keyTimes: [NSNumber] = []
keyTimes.append(NSNumber(value: 0))
keyTimes.append(NSNumber(value: 0.5))
keyTimes.append(NSNumber(value: 1))
var timingFunctions: [AnyHashable] = []
timingFunctions.append(CAMediaTimingFunction(name: .easeOut))
timingFunctions.append(CAMediaTimingFunction(name: .easeInEaseOut))
let myAimation = CAKeyframeAnimation(keyPath: "position")
myAimation.duration = 0.5
myAimation.values = values
myAimation.timingFunctions = timingFunctions as? [CAMediaTimingFunction]
myAimation.keyTimes = keyTimes
myView.layer.add(myAimation, forKey: nil)
}
它不会起作用,因为center
不是 CALayer
的 属性。
您可以从
查看完整列表
尝试[CAKeyframeAnimation animationWithKeyPath:@"position.x"];
。
正如我在 Apple documentation 中读到的那样,我能够为 UIView
.
我正在使用 CAKeyframeAnimation
来执行此操作并为该动画提供三个关键帧和三个 CGPoint。
动画似乎不工作。我是不是做错了什么?
NSMutableArray* values = [NSMutableArray new];
[values addObject:@(CGPointMake(0, 0))];
[values addObject:@(CGPointMake(5, 8))];
[values addObject:@(CGPointMake(10, 8))];
NSMutableArray* keyTimes = [NSMutableArray new];
[keyTimes addObject:@(0)];
[keyTimes addObject:@(0.5)];
[keyTimes addObject:@(1)];
NSMutableArray* timingFunctions = [NSMutableArray new];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
[timingFunctions addObject:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
CAKeyframeAnimation* myAimation = [CAKeyframeAnimation animationWithKeyPath:@"center"];
myAimation.duration = 0.5;
myAimation.values = values;
myAimation.timingFunctions = timingFunctions;
myAimation.keyTimes = keyTimes;
[myView.layer addAnimation:myAimation forKey:nil];
尝试将键路径从 "center" 更改为 "position" 我的 swift 代码正在使用这个
func animate() {
var values: [AnyHashable] = []
values.append(NSValue(cgPoint: CGPoint(x: 0, y: 0)))
values.append(NSValue(cgPoint: CGPoint(x: 200, y: 140)))
values.append(NSValue(cgPoint: CGPoint(x: 140, y: 200)))
var keyTimes: [NSNumber] = []
keyTimes.append(NSNumber(value: 0))
keyTimes.append(NSNumber(value: 0.5))
keyTimes.append(NSNumber(value: 1))
var timingFunctions: [AnyHashable] = []
timingFunctions.append(CAMediaTimingFunction(name: .easeOut))
timingFunctions.append(CAMediaTimingFunction(name: .easeInEaseOut))
let myAimation = CAKeyframeAnimation(keyPath: "position")
myAimation.duration = 0.5
myAimation.values = values
myAimation.timingFunctions = timingFunctions as? [CAMediaTimingFunction]
myAimation.keyTimes = keyTimes
myView.layer.add(myAimation, forKey: nil)
}
它不会起作用,因为center
不是 CALayer
的 属性。
您可以从
尝试[CAKeyframeAnimation animationWithKeyPath:@"position.x"];
。