2009-11-11 12 views
6

Próbuję wibrować/wstrząsnąć elementem interfejsu użytkownika, a mianowicie kontrolką, tak jakby została uderzona i rezonuje. Można użyć animacji Core wstrząsnąć pionowo, poziomo lub zarówno o jeden piksel:Najlepszy sposób wibrowania elementu interfejsu użytkownika?

CABasicAnimation* rotationXAnimation; 
rotationXAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"]; 
rotationXAnimation.fromValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y-1.0 ]; 
rotationXAnimation.toValue = [NSNumber numberWithFloat: ((UIControl *) sender).center.y+1.0 ]; 
rotationXAnimation.duration = 0.2; 
rotationXAnimation.cumulative = NO; 
rotationXAnimation.repeatCount = 10.0; 
rotationXAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:rotationXAnimation forKey:@"upDownAnimation"]; 

albo można obracać przez mały łuk do tyłu i do przodu wokół osi Z (-M_PI M_PI * 0,02 * 0,02) :

CABasicAnimation* rotationAnimation; 
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; 
rotationAnimation.fromValue = [NSNumber numberWithFloat: -M_PI * 0.02 ]; 
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 0.02 ]; 
rotationAnimation.duration = 0.2; 
rotationAnimation.cumulative = NO; 
rotationAnimation.repeatCount = 10.0; 
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:rotationAnimation forKey:@"rotationAnimation"]; 

Żaden z tych elementów nie wygląda tak przyjemnie. Czy ktoś ma dobre alternatywy? Byłbym szczególnie wdzięczny za fajne pomysły na keyPaths.

Dzięki!

Aktualizacja:

dodaje się, gdzie jestem zamawiających i rozszerza kontrolę, jest lepiej, ale ciągle szukam innych pomysłów.

CABasicAnimation* scaleAnimation; 
scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
scaleAnimation.fromValue = [NSNumber numberWithFloat: 0.95 ]; 
scaleAnimation.toValue = [NSNumber numberWithFloat: +1.05 ]; 
scaleAnimation.duration = 0.1; 
scaleAnimation.cumulative = NO; 
scaleAnimation.repeatCount = 10.0; 
scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 

[((UIControl *) sender).layer addAnimation:scaleAnimation forKey:@"scaleAnimation"]; 

Odpowiedz

6

drżę jeden z moich wejścia opinii po niewłaściwej wprowadzanych przez użytkownika za pomocą:

- (void)earthquake:(UIView*)itemView 
{ 
    CGFloat t = 2.0; 
    CGAffineTransform leftQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, t, -t); 
    CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, t); 

    itemView.transform = leftQuake; // starting point 

    [UIView beginAnimations:@"earthquake" context:itemView]; 
    [UIView setAnimationRepeatAutoreverses:YES]; // important 
    [UIView setAnimationRepeatCount:4]; 
    [UIView setAnimationDuration:0.07]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)]; 

    itemView.transform = rightQuake; // end here & auto-reverse 

    [UIView commitAnimations]; 
} 

- (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context 
{ 
    if ([finished boolValue]) 
    { 
     UIView* item = (UIView *)context; 
     item.transform = CGAffineTransformIdentity; 
    } 
} 

Wygląda to całkiem naturalne, mam ten kod z jakiegoś internetowego wątku, Nie pamiętam.

4

Możesz użyć animacji klatki kluczowej na pozycji warstwy. Jest to oczywiście subiektywne, ponieważ nie jestem pewien, co uważasz za "przyjemne". Animacja wstrząsowa, którą dostajesz, gdy niepoprawnie wpisujesz hasło OS X, duplikowałem używając animacji rdzeniowej w tym blog post on Cocoa Is My Girlfriend. Nie jestem pewien, czy właśnie tego chcesz, ale jest to jedna z alternatyw.

poważaniem,

+0

To jest tylko w jeden kierunek - z boku na bok - prawda? – mahboudz

+0

Tak. Ale możesz utworzyć animację klatki kluczowej, która używa ścieżki w ten sam sposób, animując pole "position", które jest CGPoint. –

2

aktualizację Bersaelor's ładnym małym wytrząsania odpowiedzi tutaj użyć UIView animacje blokowych zamiast niektórzy ludzie nie zawsze wiedzą, można użyć auto-reverse i wielokrotnym liczy w nich:

self.transform = CGAffineTransformMakeTranslation(2.0, -2.0); 
[UIView animateWithDuration:0.07 
         delay:0.0 
        options:UIViewAnimationOptionAutoreverse 
       animations:^{ 
        UIView.animationRepeatCount = 4; 
        self.transform = CGAffineTransformMakeTranslation(-2.0, 2.0); 
       } 
       completion:^(BOOL finished){ 
        self.transform = CGAffineTransformIdentity; 
       }]; 
Powiązane problemy