- (void)earthquake:(UIView *)itemView {
CGFloat t = 4.0;
CGAffineTransform leftQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, t, -t);
CGAffineTransform rightQuake = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, t);
itemView.transform = leftQuake;
[UIView beginAnimations:@"eratquake" context:(__bridge void * _Nullable)(itemView)];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:3];
[UIView setAnimationDuration:0.07];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(earthquakeEnded:finished:context:)];
itemView.transform = rightQuake;
[UIView commitAnimations];
}
- (void)earthquakeEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([finished boolValue]) {
UIView *item = (__bridge UIView *)context;
item.transform = CGAffineTransformIdentity;
}
}
#pragma mark - 2
- (void)shakeView:(UIView *)viewToShake {
CGFloat t = 4.0;
CGAffineTransform transformRight = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
CGAffineTransform transformLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);
viewToShake.transform = transformLeft;
[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
[UIView setAnimationRepeatCount:2.0];
viewToShake.transform = transformRight;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
viewToShake.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
}];
}];
}
本文介绍了一种在iOS应用中实现视图震动效果的方法。通过使用UIView动画,可以轻松地为视图添加地震般的震动效果,包括左右震动及恢复原始状态的过程。文中提供了详细的代码示例,展示了如何设置动画参数来达到期望的效果。
1531

被折叠的 条评论
为什么被折叠?



