1

工作不使用的Xcode 7.2.1版本(7C1002), SDK的iOS 9.2animateWithDuration在viewWillAppear中的ios 9 Objective C的

試圖從動畫左ImageView的(屏幕外)到右,有動畫重複 。我知道動畫在viewDidLoad中按照需要工作,但它並不理想,因爲動畫在呈現新控制器或應用程序已轉到後臺並返回前臺時停止。當控制器再次出現時,我希望動畫重新啓動。當我將代碼移動到查看willappear時,動畫不會發生。

我試着:

  1. 加入performSelector以3秒的延遲 - >不動畫
  2. 在dispatch_async包裹animateWithDuration(dispatch_get_main_queue(),^ {...}); - >不動畫
  3. 覆蓋viewDidLayoutSubviews方法,並從那裏調用動畫代碼 - >不動畫

的動畫作品是從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設置爲-100在界面生成器中,因此它是啓動的地方。

故事板的切入點 - > MainController

+0

當你說從'viewDidLoad'調用它的時候,它確實有效,你真的認爲'viewDidAppear'?因爲這是我在您發佈的代碼中看到的內容。 – Gavin

+0

它應該在viewDidAppear中工作,但不在viewDidLoad –

+0

@Gavin - 對不起,澄清我發佈的代碼不起作用。當調用viewDidLoad中的[self startAnimations]時,動畫會起作用(當然,在viewDidAppear中註釋掉該調用) – tdios

回答

2

如前所述,上面的代碼完美地工作,如果[self startAnimations];被稱爲viewDidLoad中,而不是在viewDidAppear,但this answer helped me discover the subtle issue

爲了得到它在viewDidAppear的工作,我需要

[self.view layoutIfNeeded];

更換

[self.movingL2R_1_ImageView layoutIfNeeded];

都是正確的與世界一次。