2013-08-28 39 views
3

如何在翻轉動畫的中途更改UIImageView的圖像。 我的代碼似乎翻轉了UIImageView併成功改變了圖像,但它瞬間改變了。我想達到的目的只是在180度翻蓋達到90度翻蓋後才改變圖像。這裏是我的代碼:更改UIImageView圖像翻轉動畫的一半

在視圖中創建的UIImageView:

UIImage *image = [UIImage imageNamed:@"fb1.jpg"]; 
    imageView = [[UIImageView alloc] initWithImage:image]; 
    vw = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)]; 
    vw.layer.cornerRadius = vw.frame.size.width/2; 
    imageView.frame = CGRectMake(20, 20, 80, 80); 
    imageView.layer.cornerRadius = imageView.frame.size.width/2; 
    imageView.layer.masksToBounds = YES; 
    imageView.layer.borderColor = [[UIColor blackColor] CGColor]; 
    imageView.layer.borderWidth = 1.0; 
    [self.view addSubview: imageView]; 
    //[self.view addSubview:vw]; 
    [imageView setUserInteractionEnabled:YES]; 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(taptap:)]; 
    [imageView addGestureRecognizer:tap]; 

動畫的UIImageView上點擊:

[UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut 
        animations:^(void) { 
         imageView.transform = CGAffineTransformMakeScale(-1, 1); 
         [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionTransitionCrossDissolve 
              animations:^(void) { 
               imageView.image = [UIImage imageNamed:@"fb2.jpg"]; 
              } completion:^(BOOL finished) { 

              }]; 
        } 
        completion:^(BOOL finished) { 
        }]; 

而且,我做的具體的UIImageView敲擊手勢這部動畫。

+0

http://stackoverflow.com/questions/1194752/how-can-i-flip-a-uiimageview – iOS

+0

我試圖按照該鏈接上的答案讓我圓的UIImageView平方彈射啓動時。 –

回答

19

您可以輕鬆地使用UIView類方法+ (void)transitionWithView:duration:options:animations:completion:

使用這樣的代碼爲您的動畫:

[UIView transitionWithView:imageView 
        duration:0.4 
        options:UIViewAnimationOptionTransitionFlipFromRight 
       animations:^{ 
        // Set the new image 
        // Since its done in animation block, the change will be animated 
        imageView.image = newImage; 
       } completion:^(BOOL finished) { 
        // Do whatever when the animation is finished 
       }]; 

您可以通過以下任一選項替換UIViewAnimationOptionTransitionFlipFromRight設置翻頁的方向:

UIViewAnimationOptionTransitionFlipFromLeft 
UIViewAnimationOptionTransitionFlipFromRight 
UIViewAnimationOptionTransitionFlipFromTop 
UIViewAnimationOptionTransitionFlipFromBottom 
+0

謝謝你,但我需要的動畫是一個像翻轉硬幣一樣的動畫,我只想在硬幣轉向一半時只想改變圖像。 –

+0

我沒有得到什麼exaclty你試圖完成。你想180°翻轉動畫(從紙的一面到另一面)還是完整的360°翻轉動畫?我的代碼使180°翻轉動畫:使90°(半翻轉),因此圖像的視覺寬度= 0,更改圖像並將寬度恢復爲原始的) –

+0

這就是我想要實現的。半翻轉後,我希望圖像立即改變。 –

0

嘗試兩步動畫,如果你不喜歡這個,你可以查看連接選項的文檔。

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseIn 
       animations:^(void) { 
        //do your half rotation here 
        imageView.transform = CGAffineTransformMakeScale(0, 1); 
       } 
       completion:^(BOOL finished) { 
        if(finished){ 
         imageView.image = [UIImage imageNamed:@"fb2.jpg"]; 
         [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut 
              animations:^(void) { 
               //do your second half rotation here 
               imageView.transform = CGAffineTransformMakeScale(-1, 1); 
              } completion:^(BOOL finished) { 

              }]; 
        } 
       }];