2012-10-10 48 views
1

閱讀了關於動畫的一些教程後,我測試了一些東西。我創建了一個帶故事板的單個視圖ARC項目。我在主視圖中添加了一個UIImageView,中心位於(200,200)。我正確設置了插座。混合UIView動畫和CGAffineTransformMakeRotation

我的想法是動畫圖像(UIImageView * bug)向左移動,然後向右旋轉180°,向右移動,向左旋轉180°等等。

這是我的代碼

viewcontroller.h

@interface ViewController : UIViewController 

@property (nonatomic, assign) IBOutlet UIImageView *bug; 
@end 

viewcontroller.m

#import "ViewController.h" 
#import <CoreGraphics/CoreGraphics.h> 

@interface ViewController() 

@end 

@implementation ViewController 

@synthesize bug; 

#pragma mark - Animations 


- (void)moveToLeft 
{ 

    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
         NSLog(@"Animation 1"); 
         bug.center = CGPointMake(100, 200); 
        } 
        completion:^(BOOL finished){ 
         [self turnToRight]; 
        } 
    ];  
} 

-(void)turnToRight 
{ 
    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
         NSLog(@"Animation 2"); 
         bug.transform = CGAffineTransformMakeRotation(M_PI);      } 
        completion:^(BOOL finished){ 
        [self moveToRight]; 
        } 
    ]; 
} 

- (void)moveToRight 
{ 

    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
         NSLog(@"Animation 3"); 
         bug.center = CGPointMake(200, 200); 
        } 
        completion:^(BOOL finished){ 
         [self turnToLeft]; 
        } 
    ]; 
} 

-(void)turnToLeft 
{ 
    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
         NSLog(@"Animation 4"); 

          bug.transform = CGAffineTransformMakeRotation(0.0); 
        } 
        completion:^(BOOL finished){ 
         [self moveToLeft]; 
        } 
    ]; 
} 

一切都OK,直到第二次調用CGAffineTransformMakeRotation。圖像左轉,右轉,右轉,然後問題!動畫中斷並且錯誤移動到第一個CGAffineTransformMakeRotation的位置,然後向左旋轉,重複這些步驟。

我試過不同的方法,但似乎每次後續調用CGAffineTransform都不考慮UIImageView的新位置,並始終在第一個位置調用它。我已經檢查了很多例子,代碼似乎是正確的,我只是不明白爲什麼將CGAffineTransformMakeRotation與普通動畫混合導致這種行爲。

回答

0

您應該使用UIViewAnimationOptionCurveEaseInOut,而不是UIViewAnimationCurveEaseInOut。它們來自不同的枚舉,並且在使用塊動畫時,始終使用名稱中包含Option的塊。
您指定的值爲UIViewAnimationOptionLayoutSubviews,這可能會導致重定位。

這是我在代碼中發現的唯一問題。如果這不是解決方案,那麼你發現了一個bug :)