加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:34:58 所属栏目:百科 来源:网络整理
导读:这是目标c和 swift的两个代码,两者都是相同的. 使用目标c它可以正确地缩放比例,但是在快速中它只是跳到最终值,我认为这是一个错误,有人可以测试并验证它吗? 目标C. UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(200,200,100,100)]; testVi
这是目标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")
    }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读