到目前为止,我有以下代码来动画图像抖动:
-(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 :(得分:0)
我猜图像是UIImageView。所以,只需删除.jpg并像[image.layer addAnimation:....
答案 1 :(得分: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"];
只需用_imageView替换image.jpg。