gpt4 book ai didi

uiview - 快速转换动画错误?

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:30 27 4
gpt4 key购买 nike

这是objective c和swift的两段代码,都是一样的。使用 objective c 它可以正确地设置动画比例,但很快它就会跳到最终值,我认为这是一个错误,有人可以测试并验证它吗?

objective-c

    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
testView.backgroundColor = [UIColor greenColor];
[self addSubview:testView];

CAKeyframeAnimation * transformAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
transformAnim.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeRotation(3 * M_PI/180, 0, 0, -1)],
[NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(3 * M_PI/180, 0, 0, 1))],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1)],
[NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(-8 * M_PI/180, 0, 0, 1))]];
transformAnim.keyTimes = @[@0, @0.349, @0.618, @1];
transformAnim.duration = 1;
[testView.layer addAnimation:transformAnim forKey:@"test"];

swift

        let testView = UIView(frame: CGRectMake(200, 200, 100, 100))
testView.backgroundColor = UIColor.greenColor()
self.addSubview(testView)

var transformAnim = CAKeyframeAnimation(keyPath:"transform")
transformAnim.values = [NSValue(CATransform3D: CATransform3DMakeRotation(3 * CGFloat(M_PI/180), 0, 0, -1)),
NSValue(CATransform3D: CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(3 * CGFloat(M_PI/180), 0, 0, 1))),
NSValue(CATransform3D: CATransform3DMakeScale(1.5, 1.5, 1)),
NSValue(CATransform3D: CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(-8 * CGFloat(M_PI/180), 0, 0, 1)))]
transformAnim.keyTimes = [0, 0.349, 0.618, 1]
transformAnim.duration = 1

testView.layer.addAnimation(transformAnim, forKey: "transform")

这是 objc 动画,它按预期工作

objc transform anim这是快速动画,缩放变换只是跳跃 swift transform anim

最佳答案

我在 Objective-C 和 Swift 中测试了您的代码,两种语言的行为完全相同(在这两种情况下看起来都像您的第一个 gif 动画)。

enter image description here

以下是确认结果的方法。您的代码对我来说没有意义(一次添加一个 View 并为其设置动画?)所以我将其分解为两个按钮方法,然后将其移至 View Controller 中。所以,这是在 Objective-C 中:

@interface ViewController ()
@property (nonatomic) UIView* testView;
@end

@implementation ViewController
- (IBAction) doButton1 {
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)];
testView.backgroundColor = [UIColor greenColor];
[self.view addSubview:testView];
self.testView = testView;
}
- (IBAction) doButton2 {
CAKeyframeAnimation * transformAnim = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
transformAnim.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeRotation(3 * M_PI/180, 0, 0, -1)],
[NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(3 * M_PI/180, 0, 0, 1))],
[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1)],
[NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(-8 * M_PI/180, 0, 0, 1))]];
transformAnim.keyTimes = @[@0, @0.349, @0.618, @1];
transformAnim.duration = 1;
[self.testView.layer addAnimation:transformAnim forKey:@"test"];
}
@end

这是在 Swift 中:

class ViewController: UIViewController {
var testView : UIView!

@IBAction func doButton1() {
let testView = UIView(frame: CGRectMake(200, 200, 100, 100))
testView.backgroundColor = UIColor.greenColor()
self.view.addSubview(testView)
self.testView = testView
}
@IBAction func doButton2() {
var transformAnim = CAKeyframeAnimation(keyPath:"transform")
transformAnim.values = [NSValue(CATransform3D: CATransform3DMakeRotation(3 * CGFloat(M_PI/180), 0, 0, -1)),
NSValue(CATransform3D: CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(3 * CGFloat(M_PI/180), 0, 0, 1))),
NSValue(CATransform3D: CATransform3DMakeScale(1.5, 1.5, 1)),
NSValue(CATransform3D: CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1), CATransform3DMakeRotation(-8 * CGFloat(M_PI/180), 0, 0, 1)))]
transformAnim.keyTimes = [0, 0.349, 0.618, 1]
transformAnim.duration = 1
self.testView.layer.addAnimation(transformAnim, forKey: "transform")
}
}

现在,无论使用哪种语言,点击第一个按钮(添加 View ),然后点击第二个按钮(为其设置动画)。

关于uiview - 快速转换动画错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26172447/

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