2012-09-10 55 views
1

我正面臨與UIImageView旋轉的問題。CATransform3DMakeRotation導致UIimage旋轉隨着UIimageview

我需要水平旋轉UIImageView,並使用廣告等動畫。

要做到這一點,我使用以下代碼:

[UIView animateWithDuration:1.0 
     delay:0.0 
     options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction 
     animations:^{ 
      if (baddview) { 
       baddview = FALSE; 
       adimageView.layer.transform = CATransform3DMakeRotation(0, 0, 1, 0);       
      } else { 
       baddview = TRUE; 
       adimageView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);       
      } 
     } 
     completion:^(BOOL finished) { 
     } 
]; 

的ImageView的旋轉,因爲我需要它,但在圖像視圖中的圖像出現顛倒,像旋轉180度(you can see the image here)之後。

如何在不旋轉UIImage的情況下動畫UIImageView旋轉?

+0

喜ü要執行什麼...? baddview是什麼...? – Spynet

+0

是否要旋轉圖像或不... – Rajneesh071

+0

不,我想要的圖像作爲原始,只需要動畫的Uiimageview –

回答

2

一種可能的方法是製作2 UIImageView,並對它們進行動畫處理。你必須做的是:

  1. 動畫塊之前,創建新UIImageView並將其放置在當前UIImageView落後,但要確保它具有180
  2. 旋轉取2個動畫塊,一個旋轉第一視圖度一半(即0.5秒)的持續時間,另一個塊將動畫視圖延後到CATransform3DIdentity全部持續時間(即1.0秒)。

如果需要,我會添加一些代碼。

或者,也可以將isDoubleSided的屬性CALayer設置爲NO。然後,將兩個視圖旋轉180度。頂視圖將被隱藏,因爲它不是雙面的。


我剛剛創建了一個新的單一視圖項目,並增加了一個UIButton進行翻轉。這裏是代碼的其餘部分:

頭文件:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController { 
    UIImageView* _imageView1; 
    UIImageView* _imageView2; 
} 

- (IBAction)flip:(id)sender; 

@end 

實現文件:

#import "ViewController.h" 
#import <QuartzCore/QuartzCore.h> 
@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIImage* image1 = [UIImage imageNamed:@"gaara1.png"]; 
    _imageView1 = [[UIImageView alloc] initWithImage:image1]; 
    [_imageView1 setFrame:CGRectMake((CGRectGetWidth(self.view.bounds) - 300)/2, 100.f, 300, 75)]; 

    UIImage* image2 = [UIImage imageNamed:@"gaara2.png"]; 
    _imageView2 = [[UIImageView alloc] initWithImage:image2]; 
    [_imageView2 setFrame:CGRectMake((CGRectGetWidth(self.view.bounds) - 300)/2, 100.f, 300, 75)]; 
    // the second imageView is in the back 
    [self.view addSubview:_imageView2]; 
    [self.view addSubview:_imageView1]; 

} 

- (IBAction)flip:(id)sender { 
    [_imageView1.layer setDoubleSided:NO]; 
    [_imageView2.layer setDoubleSided:NO]; 

    [_imageView1.layer setZPosition:10.f]; 

    [_imageView1.layer setTransform:CATransform3DIdentity]; 
    [_imageView2.layer setTransform:CATransform3DMakeRotation(-M_PI, 1.f, 0.f, 0.f)]; 

    [UIView animateWithDuration:1.0 
          delay:0.0 
         options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         _imageView1.layer.transform = CATransform3DMakeRotation(M_PI, 1.f, 0.f, 0.f); 
         _imageView2.layer.transform = CATransform3DIdentity; 
        } 
        completion:NULL]; 

} 

@end 

的圖像:

enter image description here

enter image description here

+0

似乎像你的解決方案解決我的問題。但有點混亂,你可以提供一些示例代碼。 –

+0

@maheshbabu我現在很菜的一些代碼,請準備菜餚..很快會發布。 – Mazyod

+0

是great..i已經準備好與菜餚 –

0

您的代碼可能會導致問題。 UIView動畫是動畫UIView的屬性,而不是圖層。

如果您嘗試操作UIView動畫塊中的圖層轉換,我預計會出現問題。你應該把自己侷限在一個UIView動畫塊改變視圖屬性:

[UIView animateWithDuration:1.0 
         delay:0.0 
        options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction 
       animations:^{ 
        _imageView1.transform = CGAffineTransformMakeRotation(M_PI); 
        _imageView2.transform = CGAffineTransformIdentity; 
       } 
       completion:NULL]; 

CAAnimation對象是動畫層。

如果你想你的動畫視圖的層轉換,直接這樣做(這將給你一個隱含的動畫),或創建一個CABasicAnimation對象進行更復雜的動畫。

+0

納阿,你的代碼實際上旋轉在xy平面視圖,而OP要旋轉XYZ空間的觀點。動畫層是唯一的方法。 – Mazyod