gpt4 book ai didi

ios - 如何为 UIViews 'center' 属性设置动画?

转载 作者:行者123 更新时间:2023-11-28 23:29:53 24 4
gpt4 key购买 nike

正如我在 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];

最佳答案

尝试将键路径从“中心”更改为“位置”我的快速代码正在处理这个

    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)
}

关于ios - 如何为 UIViews 'center' 属性设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57004416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com