2012-10-03 67 views
16

在我的xcode項目中,我有一個UIImage。我想讓它從左向右搖晃(動畫)一點點。我寫了這個代碼,但是,它不工作。如何製作「搖動」動畫

-(void)errorShake 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:1.0]; 



    [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(-20 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(20 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI/180.0)]; 
    [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI/180.0)]; 


    [UIView commitAnimations]; 
} 

請問你能告訴我,我該如何讓我的形象動搖?

回答

45

下面是我用於此效果的代碼。

-(void)shakeView { 

     CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"]; 
     [shake setDuration:0.1]; 
     [shake setRepeatCount:2]; 
     [shake setAutoreverses:YES]; 
     [shake setFromValue:[NSValue valueWithCGPoint: 
           CGPointMake(lockImage.center.x - 5,lockImage.center.y)]]; 
     [shake setToValue:[NSValue valueWithCGPoint: 
           CGPointMake(lockImage.center.x + 5, lockImage.center.y)]]; 
     [lockImage.layer addAnimation:shake forKey:@"position"]; 
    } 

作爲Jere提到。確保包括進口:

#import <QuartzCore/QuartzCore.h> 

您也可以將其添加爲一個類別上UIView

+0

謝謝!但是,我需要爲項目添加任何框架/庫嗎?由於第一行,Xcode提供了一個錯誤。 – SmartTree

+2

將'QuartzCore'框架添加到您的項目!並在要使用該動畫的文件中添加'#import ',或者在應用程序前綴頭文件中添加''import',以便您可以從任何類中看到它。 – jere

+0

太棒了!還有一個問題:什麼是[自我中心]? – SmartTree

11

brynbodayles回答也適用於iOS8下swift中的搖動動畫。

隨着QuartzCore進口:

func shakeView(){ 
    var shake:CABasicAnimation = CABasicAnimation(keyPath: "position") 
    shake.duration = 0.1 
    shake.repeatCount = 2 
    shake.autoreverses = true 

    var from_point:CGPoint = CGPointMake(LoginField.center.x - 5, LoginField.center.y) 
    var from_value:NSValue = NSValue(CGPoint: from_point) 

    var to_point:CGPoint = CGPointMake(LoginField.center.x + 5, LoginField.center.y) 
    var to_value:NSValue = NSValue(CGPoint: to_point) 

    shake.fromValue = from_value 
    shake.toValue = to_value 
    LoginField.layer.addAnimation(shake, forKey: "position") 
} 
+0

如何在搖晃重複時停止動畫 –

-1

Corona答案

給搖動畫多個按鈕,並在視圖中SWIFT3

用於觀看動畫只是改變擴展UIButton的分機的UIView

extension UIButton 
{ 
    func shakeAnim() 
    { 
     let shake:CABasicAnimation = CABasicAnimation(keyPath: "position") 
     shake.duration = 0.1 
     shake.repeatCount = 2 
     shake.autoreverses = true 

     let from_point:CGPoint = CGPoint(x:self.center.x - 5,y: self.center.y) 
     let from_value:NSValue = NSValue(cgPoint: from_point) 

     let to_point:CGPoint = CGPoint(x:self.center.x + 5,y: self.center.y) 
     let to_value:NSValue = NSValue(cgPoint: to_point) 

     shake.fromValue = from_value 
     shake.toValue = to_value 
     self.layer.add(shake, forKey: "position") 
    } 
}