UIImageView动画旋转

时间:2011-06-22 15:26:36

标签: xcode uiimageview cgaffinetransform cabasicanimation

我想在按下一个按钮(360度)后顺时针绕着他的中心旋转一个UIImageView ......它应该是一个动画......所以它得到一个方向盘,它应该为用户自己旋转...

我看到有很多方法可以做到这一点,但没有什么对我有用......: - )

CABasicAnimation,我是否必须为此添加框架?

有人可以给我一个示例代码,用于围绕其中心旋转UIImageView(360度)吗??

提前谢谢

3 个答案:

答案 0 :(得分:1)

听到下面给我做同样的事情......但是我的要求是当touchBegin那个视图将旋转360度......它可能对你有所帮助......

 - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

    self.userInteractionEnabled=YES;
[UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self   cache:YES];
   [UIView commitAnimations];
  [delegate changeImage:self];

 }
 -(void)changeImage:(DraggableImages *)selectedView
 {
count++;
clickCounter++;
counterLable.text=[NSString stringWithFormat:@"%i",clickCounter];
selectedView.image=[images objectAtIndex:selectedView.tag];
if (count==1) {
    firstSelectedView=selectedView;
}
if (count==2) {
    self.view.userInteractionEnabled=NO;
    secondSelectedView=selectedView;
    count=0;
    [self performSelector:@selector(compareImages) withObject:nil     afterDelay:0.7];
}

}
  -(void)compareImages
 {
if (firstSelectedView.tag==secondSelectedView.tag) 
{
    matches++;
    //NSLog(@"%@",matches);
    Score=Score+10;
    scoreLable.text=[NSString stringWithFormat:@"%d",Score];

    firstSelectedView.alpha=0.5;
    secondSelectedView.alpha=0.5;
    self.view.userInteractionEnabled=YES;
    if(matches==[images count])
    {

        [timer invalidate];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"Do you want to countinue?"] delegate:self cancelButtonTitle:nil otherButtonTitles:@"YES",@"NO", nil];
        [alertView show];
        [alertView release];

    } 
}

else {
    Score=Score-1;
    scoreLable.text=[NSString stringWithFormat:@"%d",Score];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:firstSelectedView cache:YES];
    firstSelectedView.image=[UIImage imageNamed:@"box.jpg"];
    [UIView commitAnimations];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationBeginsFromCurrentState:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:secondSelectedView cache:YES];
    secondSelectedView.image=[UIImage imageNamed:@"box.jpg"];
    [UIView commitAnimations];
    firstSelectedView.userInteractionEnabled=YES;
    secondSelectedView.userInteractionEnabled=YES;
    self.view.userInteractionEnabled=YES;





}

}

答案 1 :(得分:0)

您需要添加QuartzCore框架,并且可以阅读有关CABasicAnimation的更多信息。

一些教程

Interrupting Animation Progress

Slider Based Layer Rotation

CABasicAnimation basics

Core Animation Paths

答案 2 :(得分:0)

按下按钮会调用此方法:

- (void) animate{

CABasicAnimation *imageRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
imageRotation.removedOnCompletion = NO; // Do not turn back after anim. is finished
imageRotation.fillMode = kCAFillModeForwards;

imageRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];

imageRotation.duration = 1.5;
imageRotation.repeatCount = 1;

[yourImageView.layer setValue:imageRotation.toValue forKey:imageRotation.keyPath];
[yourImageView.layer addAnimation:imageRotation forKey:@"imageRotation"];

}

是的,正如WrightsCS所说,你需要在项目中包含QuartzCore框架。不要忘记import语句:

#import <QuartzCore/QuartzCore.h>