2014-10-18 49 views
-2

使用圖像到目前爲止,我有以下代碼,以動畫的圖像晃動:在Xcode

-(void)shakeView { 

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

不過,我得到以下錯誤:未聲明的標識符「image.jpg文件」的使用。 我需要做些什麼來解決這個問題?任何幫助表示讚賞。謝謝!

回答

0

我猜想圖像是UIImageView。所以,只是刪除jpg和使用它像[image.layer addAnimation:....

0

假設你有定義爲這樣一個的UIImageView:

@property (weak, nonatomic) IBOutlet UIImageView *imageView; 

那麼你是無法撼動用下面的代碼視圖。

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

只需將image.jpg替換爲_imageView即可。