[英]animateWithDuration not working in viewWillAppear ios 9 objective c
Using Xcode Version 7.2.1 (7C1002), SDK iOS 9.2 使用Xcode版本7.2.1(7C1002),SDK iOS 9.2
Trying to animate imageView from left(offscreen) to right and have the animation repeat . 尝试从左(屏幕外)向右制作imageView动画,并使动画重复 。 I know the animation works as desired in viewDidLoad, but it's not ideal since the animation stops when a new controller is presented or the app has gone to the background and back to the foreground. 我知道动画在viewDidLoad中可以按预期工作,但是这不是理想的,因为当出现新的控制器或应用程序进入后台并回到前台时,动画会停止。 I would like the animation to restart when the controller appears again. 我希望动画在控制器再次出现时重新启动。 When I move the code to viewwillappear, the animation never happens. 当我将代码移动到viewwill时,动画永远不会发生。
I've tried: 我试过了:
the only time the animation works is when called from viewDidLoad. 动画工作的唯一时间是从viewDidLoad调用时。 Any help would be appreciated. 任何帮助,将不胜感激。 I must be missing something really obvious. 我一定想念一些确实很明显的东西。 here's the code (which doesn't work)... 这是代码(不起作用)...
MainViewController.h MainViewController.h
@property (nonatomic, strong) IBOutlet UIImageView *movingL2R_1_ImageView;
@property (nonatomic) IBOutlet NSLayoutConstraint *movingL2R_1_LeadingConstraint;
MainViewController.m 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;
}];
}
NOTE: the movingL2R_1_LeadingConstraint is set to -100 in Interface Builder so that is where it starts. 注意:MovingL2R_1_LeadingConstraint在Interface Builder中设置为-100,因此它是从此处开始的。
storyboard entry point --> MainController 故事板入口点-> MainController
As stated, the code above works perfectly if [self startAnimations];
如前所述,如果[self startAnimations];
,则上面的代码可以完美地工作[self startAnimations];
is called in viewDidLoad, but not in viewDidAppear, but this answer helped me discover the subtle issue . 在viewDidLoad中调用,但在viewDidAppear中未调用,但是此答案帮助我发现了微妙的问题 。
To get it working in viewDidAppear I needed to replace 为了使其在viewDidAppear中工作,我需要更换
[self.movingL2R_1_ImageView layoutIfNeeded];
with 同
[self.view layoutIfNeeded];
all is right with the world again. 一切都再次与世界同在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.