当前位置: 首页 > 工具软件 > UIView-Shake > 使用案例 >

UIView--震动效果

淳于健
2023-12-01

//震动效果
- (void)shake:(UIView *)view
{
CGRect frame = view.frame;
CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

CGMutablePathRef shakePath = CGPathCreateMutable();
CGPathMoveToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2, frame.origin.y+frame.size.height/2);
int index;
for (index = 3; index >=0; --index)
{
CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 - frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 + frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
}
CGPathCloseSubpath(shakePath);

shakeAnimation.path = shakePath;
shakeAnimation.duration = 0.5f;
shakeAnimation.removedOnCompletion = YES;

[view.layer addAnimation:shakeAnimation forKey:nil];
CFRelease(shakePath);
}

转载于:https://www.cnblogs.com/zhangliukou/p/4127100.html

 类似资料: