【问题标题】:UIImageView animated rotationUIImageView 动画旋转
【发布时间】:2011-06-22 15:26:35
【问题描述】:

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

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

CABasicAnimation,我需要为此添加一个框架吗?

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

提前谢谢你

【问题讨论】:

    标签: xcode uiimageview cgaffinetransform cabasicanimation


    【解决方案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;
    
    
    
    
    
    }
    

    }

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        按下按钮时调用此方法:

        - (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 <QuartzCore/QuartzCore.h>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-03
          相关资源
          最近更新 更多