2011-08-11 7 views
7

Próbuję zrobić ikonę "potrząsając".iPhone Animacja "Wobbly" na UiImageView

Na ładuje mój kontroler tworzę Timer tak:

[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(shakeIphonePic) userInfo:nil repeats:YES]; 

A oto moja metoda shaker:

- (void)shakeIphonePic 
{ 
    [UIView animateWithDuration:0.09 
          delay:0 
         options:UIViewAnimationOptionAllowUserInteraction 
        animations:^{ 
         self.iphonePic.layer.transform = CATransform3DMakeRotation(DegreesToRadians(8.0), 0.0, 0.0, 1.0); 
        } 
        completion:^(BOOL finished) { 
         [UIView animateWithDuration:0.09 
             animations:^(void) { 
              self.iphonePic.layer.transform = CATransform3DMakeRotation(DegreesToRadians(-16.0), 0.0, 0.0, 1.0); 
             }]; 
        } 
    ]; 
} 

To nie jest tak ładny jak się spodziewałem, ale ... to nie jest główny problem.

Wygląda na to, że znacznie spowolnił resztę mojego interfejsu użytkownika, który wcześniej był dobry.

Czy możesz zaproponować mi bardziej efektywny sposób poruszania ikoną?

Odpowiedz

27
CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"]; 
[anim setToValue:[NSNumber numberWithFloat:0.0f]]; 
[anim setFromValue:[NSNumber numberWithDouble:M_PI/16]]; // rotation angle 
[anim setDuration:0.1]; 
[anim setRepeatCount:NSUIntegerMax]; 
[anim setAutoreverses:YES]; 
[self.viewYouAreShaking.layer addAnimation:anim forKey:@"iconShake"]; 

Swift wersja

let anim=CABasicAnimation(keyPath: "transform.rotation") 
anim.toValue=NSNumber(double: -M_PI/16) 
anim.fromValue=NSNumber(double: M_PI/16) 
anim.duration=0.1 
anim.repeatCount=1.5 
anim.autoreverses=true 
viewYouAreShaking.layer.addAnimation(anim, forKey: "iconShake") 
+1

Dobrym wprowadzeniem jest rozdział Animacja Programowanie iOS4 Matt Neuburg. Publiczny projekt jest dostępny pod adresem http://www.apeth.com/iOSBook/ch17.html – Jano

+0

Jak wyłączyć animację? Próbowałem '[self.viewYouAreShaking.layer removeAllAnimations];' ale wstrząsanie zatrzymuje się po podniesieniu palca. Jak mogę go zignorować, aby dotknąć długiego naciśnięcia i rozpoznać tylko długie naciśnięcie przycisku? – Mason

Powiązane problemy