直接傳入要都動(dòng)的View
#pragma mark 抖動(dòng)動(dòng)畫
- (void)shakeAnimationForView:(UIView *) view
{
// 獲取到當(dāng)前的View
CALayer *viewLayer = view.layer;
// 獲取當(dāng)前View的位置
CGPoint position = viewLayer.position;
// 移動(dòng)的兩個(gè)終點(diǎn)位置
CGPoint x = CGPointMake(position.x + 10, position.y);
CGPoint y = CGPointMake(position.x - 10, position.y);
// 設(shè)置動(dòng)畫
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
// 設(shè)置運(yùn)動(dòng)形式
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
// 設(shè)置開始位置
[animation setFromValue:[NSValue valueWithCGPoint:x]];
// 設(shè)置結(jié)束位置
[animation setToValue:[NSValue valueWithCGPoint:y]];
// 設(shè)置自動(dòng)反轉(zhuǎn)
[animation setAutoreverses:YES];
// 設(shè)置時(shí)間
[animation setDuration:.06];
// 設(shè)置次數(shù)
[animation setRepeatCount:3];
// 添加上動(dòng)畫
[viewLayer addAnimation:animation forKey:nil];
}
我覺得還可以用彈簧效果實(shí)現(xiàn)http://www.tuicool.com/articles/ZR7nYv