2012-09-10 46 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