使用 Xcode 版本 7.2.1 (7C1002),
SDK iOS 9.2
尝试从左(离屏)到右为 imageView 设置动画并重复动画。我知道动画在 viewDidLoad 中按预期工作,但这并不理想,因为当出现新 Controller 或应用程序进入后台并返回前台时动画停止。我希望在 Controller 再次出现时重新启动动画。当我将代码移动到 viewwillappear 时,动画永远不会发生。
我试过了:
动画唯一起作用的时间是从 viewDidLoad 调用时。任何帮助,将不胜感激。我一定错过了一些非常明显的东西。这是代码(不起作用)...
MainViewController.h
@property (nonatomic, strong) IBOutlet UIImageView *movingL2R_1_ImageView;
@property (nonatomic) IBOutlet NSLayoutConstraint *movingL2R_1_LeadingConstraint;
MainViewController.m
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self startAnimations];
}
-(void) startAnimations{
[self animateMovingL2R_1_ImageView];
// other animations will go here...
}
-(void) animateMovingL2R_1_ImageView {
NSTimeInterval animateInterval = 15;
[UIView animateWithDuration:animateInterval delay:0 options:UIViewAnimationOptionCurveEaseOut | UIViewAnimationOptionRepeat animations:^{
self.movingL2R_1_LeadingConstraint.constant = 500;
[self.movingL2R_1_ImageView layoutIfNeeded];
} completion:^(BOOL finished) {
// back to original position
self.movingL2R_1_LeadingConstraint.constant = -100;
}];
}
注意:在 Interface Builder 中,movingL2R_1_LeadingConstraint 设置为 -100,这就是它开始的地方。
Storyboard入口点 --> MainController
最佳答案
如前所述,如果 [self startAnimations];
,上面的代码可以完美运行。在 viewDidLoad 中调用,但不是在 viewDidAppear 中,而是 this answer helped me discover the subtle issue .
为了让它在 viewDidAppear 中工作,我需要替换 [self.movingL2R_1_ImageView layoutIfNeeded];
和[self.view layoutIfNeeded];
世界再次一切正常。
关于ios - animateWithDuration 在 viewWillAppear ios 9 objective-c 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36454465/