2016-08-01 87 views
0

我想动画一个UIView的中心,我在viewDidLoad中这样做:的UIView不动画

_test.center = CGPointMake(100.0,100.0); 
[UIView animateWithDuration:10.0 
     delay:5 
     options:UIViewAnimationOptionCurveLinear 
     animations:^{_test.center = CGPointMake(100.0,-100.0);} 
     completion:^(BOOL finished) { 

}]; 

但是当我运行的代码,测试视图中心已经为100, - 100。为什么它不动画?

+2

移动代码'viewDidAppear'。 – Avi

+0

@Avi它的作品谢谢! – GiddensA

回答

0

将代码从viewDidLoad移至viewDidAppear。在viewDidLoad中,UIView已加载,但它尚未在视觉上显示。

0

我想你正在使用自动约束,它阻止了你正在做的动画视图。

为了解决这个问题,请尝试以下步骤:

第一:

移动你的代码- (void)viewDidLayoutSubviews

二:

之前,做任何动画视图后,执行下列步骤操作:

- (void)viewDidLayoutSubviews{ 

    _test.translatesAutoresizingMaskIntoConstraints = YES; 

    _test.center = CGPointMake(100.0,100.0); 
    [UIView animateWithDuration:10.0 
          delay:5 
         options:UIViewAnimationOptionCurveLinear 
         animations:^{_test.center = CGPointMake(100.0,-100.0);} 
         completion:^(BOOL finished) { 
         }]; 

    [_test updateConstraints]; 

}