[英]animateWithDuration not working in viewWillAppear ios 9 objective c
使用Xcode版本7.2.1(7C1002),SDK iOS 9.2
嘗試從左(屏幕外)向右制作imageView動畫,並使動畫重復 。 我知道動畫在viewDidLoad中可以按預期工作,但是這不是理想的,因為當出現新的控制器或應用程序進入后台並回到前台時,動畫會停止。 我希望動畫在控制器再次出現時重新啟動。 當我將代碼移動到viewwill時,動畫永遠不會發生。
我試過了:
動畫工作的唯一時間是從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;
}];
}
注意:MovingL2R_1_LeadingConstraint在Interface Builder中設置為-100,因此它是從此處開始的。
故事板入口點-> MainController
如前所述,如果[self startAnimations];
,則上面的代碼可以完美地工作[self startAnimations];
在viewDidLoad中調用,但在viewDidAppear中未調用,但是此答案幫助我發現了微妙的問題 。
為了使其在viewDidAppear中工作,我需要更換
[self.movingL2R_1_ImageView layoutIfNeeded];
同
[self.view layoutIfNeeded];
一切都再次與世界同在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.