微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

uiview – 在swift中转换动画错误?

这是目标c和 swift的两个代码,两者都是相同的.
使用目标c它可以正确地缩放比例,但是在快速中它只是跳到最终值,我认为这是一个错误,有人可以测试并验证它吗?

目标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,-1)],[NSValue valueWithCATransform3D:CATransform3DConcat(CATransform3DMakeScale(1.5,1.5,1),CATransform3DMakeRotation(3 * M_PI/180,1))],[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5,1)],CATransform3DMakeRotation(-8 * M_PI/180,1))]];
    transformAnim.keyTimes              = @[@0,@0.349,@0.618,@1];
    transformAnim.duration              = 1;
    [testView.layer addAnimation:transformAnim forKey:@"test"];

迅速

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

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

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

这是objc anim,它按预期工作

这是快速的动画,比例变换只是跳跃

解决方法

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

以下是如何确认我的结果.你的代码对我来说没有意义(添加一个视图并在一次动作中设置动画?)所以我将其分解为两个按钮方法,然后将其移动到视图控制器中.所以,这是在Objective-C:

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

@implementation ViewController
- (IBAction) dobutton1 {
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(200,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,@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,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),1]
        transformAnim.duration       = 1
        self.testView.layer.addAnimation(transformAnim,forKey: "transform")
    }
}

现在,在任一种语言中,点击第一个按钮(添加视图),然后点击第二个按钮(为其设置动画).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐