正如我在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];
答案 0 :(得分:1)
尝试将键路径从“中心”更改为“位置” 我的快速代码正在与此配合使用
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)
}
答案 1 :(得分:0)
该功能不起作用,因为center
不是CALayer
的属性。
您可以从this answer
查看完整列表 上阅读尝试[CAKeyframeAnimation animationWithKeyPath:@"position.x"];
。