2013-06-03 22 views
4

我一直在試圖實現這一點,但找不到任何代碼,可以搖動它像iPhone應用程序在刪除它們時動搖!如何把搖動動畫放在uiimageview上?

CABasicAnimation *animation = 
         [CABasicAnimation animationWithKeyPath:@"position"]; 
[animation setDuration:0.05]; 
[animation setRepeatCount:8]; 
[animation setAutoreverses:YES]; 
[animation setFromValue:[NSValue valueWithCGPoint: 
       CGPointMake([lockView center].x - 20.0f, [lockView center].y)]]; 
[animation setToValue:[NSValue valueWithCGPoint: 
       CGPointMake([lockView center].x + 20.0f, [lockView center].y)]]; 
[[lockView layer] addAnimation:animation forKey:@"position"]; 
+0

看到這個答案,它可以幫助你:http://stackoverflow.com/questions/1632364/shake-visual-effect-on-iphone -not-shaking-the-device – pbibergal

回答

6

您可以通過以下代碼實現此目的,您可以定義來自和來自的旋轉角度。

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]; 
    [[imageView layer] addAnimation:anim forKey:@"iconShake"]; 
+0

謝謝,這段代碼有效! – Napster

6

試試這個:

- (void)animateImage 
{ 
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; 

    CGFloat wobbleAngle = 0.06f; 

    NSValue* valLeft = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)]; 
    NSValue* valRight = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)]; 
    animation.values = [NSArray arrayWithObjects:valLeft, valRight, nil]; 

    animation.autoreverses = YES; 
    animation.duration = 0.125; 
    animation.repeatCount = HUGE_VALF; 

    [[lockView layer] addAnimation:animation forKey:@"shakeAnimation"]; 
} 
+0

謝謝它真的很不錯... –

+0

工程就像一個魅力!謝謝。 – Edgars