2014-02-19 43 views
-1

我正在第二個視圖控制器中的動畫應用程序。動畫完成後,我試圖自動延續到下一個視圖控制器。我在完成布爾成功不工作

完成SEGUE:^(BOOL成功){...

,但它無法正常工作。然後我嘗試了一個簡單的NSLog @「但是這也不起作用,我猜測問題在於BOOL沒有完成或註冊成功,我發生的動畫和結束......但完成BOOL是沒有進行。請幫助。

從secondViewController的.m文件...

#import "secondViewController.h" 

@interface secondViewController() 

@property (nonatomic,weak) IBOutlet UIImageView *animatedImage; 

@end 

@implementation secondViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
[self moveUp:nil finished:nil context:nil]; 
} 
- (void)moveUp:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 

[UIView animateWithDuration:2.0 
         delay:1.0 
options:  (UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction) 
       animations:^{ 
        [UIView setAnimationDelegate:self]; 
        [UIView setAnimationDidStopSelector:@selector(moveDown:finished:context:)]; 
        self.animatedImage.center = CGPointMake(160, 304); 
       } 
       completion:^(BOOL finished){ 
        NSLog(@"Move up done"); 

       }]; 


} 

- (void)moveDown:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { 


[UIView animateWithDuration:2.0 
         delay:1.5 
      options: (UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction) 
       animations:^{ 
        [UIView setAnimationDelegate:self]; 
        [UIView setAnimationDidStopSelector:@selector(moveUp:finished:context:)]; 
        self.animatedImage.center = CGPointMake(160, 775); 

       } 
       completion:^(BOOL finished){ 

        [self performSegueWithIdentifier:@"backHome" sender:self]; 
       }]; 
} 

@end 
+1

你能告訴我們多一點的代碼嗎?作爲例子,你創建動畫 – drolando

回答

0

下面的代碼將很好,可讀。

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; //Invoke super method 
    [self doAnimation]; 
} 

- (void)doAnimation{ 
    [UIView animateWithDuration:2.0 
         delay:1.0 
        options:(UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction) 
       animations:^{ 
        self.animatedImage.center = CGPointMake(160, 304); 
       } 
       completion:^(BOOL finished){ 
        [UIView animateWithDuration:2.0 
              delay:1.5 
             options:(UIViewAnimationCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction) 
            animations:^{ 
              self.animatedImage.center = CGPointMake(160, 775); 
            } 
            completion:^(BOOL finished){ 
              [self performSegueWithIdentifier:@"backHome" sender:self]; 
            } 
        ];} 

    }]; 
} 
+0

非常感謝!我試過了,好消息是完成塊正在工作 - 但是UIImageView不再具有動畫效果。它只停留幾秒鐘,然後完成踢進任何想法? – user3294722

+0

挖掘更深一點,我發現它只是沒有發生的第一個動畫的運動。換句話說,延遲發生,CGPointMake被跳過,下一個持續時間發生,第二個CGPointMake發生。任何想法爲什麼第一個CGPointMake被跳過? – user3294722

+0

想通了......在文件檢查器中未選中「使用自動佈局」。現在工作完美。 – user3294722

0

你結合舊式和基於塊的動畫。不要那樣做。

刪除

[UIView setAnimationDelegate:self]; 
[UIView setAnimationDidStopSelector:...]; 

可能會解決問題。相反,將完成邏輯放入完成塊。

+0

謝謝你的部分。我刪除了這兩個和完成:^(BOOL完成){NSLog(@「上移完成」);工作,但圖像不再動畫。另外,很抱歉,將完成邏輯放在完成塊中意味着什麼? – user3294722