2014-10-03 42 views
4

这里是两个代码为目标c和swift,两者都是一样的。 使用目标C它正确地动画缩放,但在迅速它只是跳到最终值,我认为这是一个错误,有人可以测试和验证它?在swift中转换动画bug?

目标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"]; 

斯威夫特

 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

+0

什么是动画应该做的?我想确保我玩的是正确的方向。 – 2014-10-03 04:14:23

+0

@MinnesotaSteve它的动画应该更大,并且动画旋转。它使用objc可以正常工作,但是在swift中旋转动画效果很好,但是它只是跳至最终缩放而不需要动画 – 2014-10-03 07:55:59

+0

好吧,+1用于制作漂亮的gif动画来说明问题! :) – matt 2014-10-03 15:49:38

回答

2

请务必将

self.targetView.layoutIfNeeded() 

调用动画之前。

testView.layer.transform = CATransform3DConcat(CATransform3DMakeScale(1.5, 1.5, 1),  CATransform3DMakeRotation(-8 * CGFloat(M_PI/180), 0, 0, 1)) 
    testView.layer.addAnimation(transformAnim, forKey: "transform") 

它工作正常

+0

Nop,不工作 – 2014-10-03 07:57:07

+0

它是在viewDidAppear? – user3677173 2014-10-03 13:08:27

+0

好吧,我得到了。只需在调用动画 – user3677173 2014-10-03 13:15:45

5

我测试你的代码都Objective-C和斯威夫特和行为正是在两种语言(它看起来像在这两种情况下,你第一 GIF动画)相同。

enter image description here

下面是如何确认我的结果。你的代码对我来说没有任何意义(添加一个视图并将其一次性动画化),所以我将它分解为两个按钮方法,并将其移动到视图控制器中。所以,在这里它是在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 

这里,它是在斯威夫特:

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

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

+0

我已经用gif图像更新了我的问题, 我已经复制了你的Swift代码,但规模仍然跳跃,我不知道什么问题? – 2014-10-03 14:17:12

+0

在我的测试中,它在动画完成时跳到_ end_,但是这非常有意义,并且它在两种语言中都有。 – matt 2014-10-03 15:48:44

+0

你在测试什么系统? iOS 7或iOS 8? – matt 2014-10-03 15:56:11

0

我对这个答案有点迟,但我也注意到了这个错误。在Swift中,您好像在动画之前不能立即添加子视图。看起来好像你必须插入一个延迟,例如:

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

    var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "animate", userInfo: nil, repeats: false) 
} 

func animate() { 
    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") 
}