UIImageView анимированное вращение - PullRequest
0 голосов
/ 22 июня 2011

Я бы хотел повернуть UIImageView по часовой стрелке вокруг его центра после нажатия кнопки (360 градусов) ... Это должна быть анимация ... Итак, он получает руль, и он должен вращаться сам по себе, видимый для пользователя ...

Я видел, что есть много способов сделать это, но у меня ничего не получалось ...: -)

CABasicAnimation, я должен добавить каркас для этого?

Может кто-нибудь дать мне пример кода для вращения UIImageView вокруг его центра на (360 градусов), который работает?!?

Заранее спасибо

Ответы [ 3 ]

1 голос
/ 09 сентября 2011

Слушайте ниже, я делаю то же самое ......, но мое требование - когда 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;





}

}

0 голосов
/ 28 сентября 2012

После нажатия кнопки вызовите этот метод:

- (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>
0 голосов
/ 09 сентября 2011

Вам необходимо добавить QuartzCore рамки, и вы можете прочитать больше о CABasicAnimation.

Некоторые учебники

Прерывание анимации

Поворот слоя на основе ползунка

Основы анимации CABasic

Основные пути анимации

...