iOS实现图片抖动效果
本文实例为大家分享了iOS实现图片抖动效果的具体代码,供大家参考,具体内容如下
效果图:
核心代码:
//
//ViewController.m
//图标抖动
//
//Createdbyllkjon2017/8/29.
//Copyright©2017年LayneCheung.Allrightsreserved.
//
#import"ViewController.h"
#defineangle2Rad(angle)((angle)/180.0*M_PI)
@interfaceViewController()
@property(weak,nonatomic)IBOutletUIImageView*imageV;
@end
@implementationViewController
-(void)viewDidLoad{
[superviewDidLoad];
self.imageV.userInteractionEnabled=YES;
//添加长按手势
UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];
[self.imageVaddGestureRecognizer:longPress];
}
-(void)longPress:(UILongPressGestureRecognizer*)longPress{
//创建动画对象
CAKeyframeAnimation*anim=[CAKeyframeAnimationanimation];
anim.keyPath=@"transform.rotation";
anim.values=@[@(angle2Rad(-5)),@(angle2Rad(5))];
anim.repeatCount=MAXFLOAT;
//anim.duration=1;
anim.autoreverses=YES;
[self.imageV.layeraddAnimation:animforKey:nil];
}
-(void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}
@end
小编再给大家补充一段iOSUIView视图抖动效果的实现代码:
/**
*抖动效果
*
*@paramview要抖动的view
*/
-(void)shakeAnimationForView:(UIView*)view{
CALayer*viewLayer=view.layer;
CGPointposition=viewLayer.position;
CGPointx=CGPointMake(position.x+1,position.y);
CGPointy=CGPointMake(position.x-1,position.y);
CABasicAnimation*animation=[CABasicAnimationanimationWithKeyPath:@"position"];
[animationsetTimingFunction:[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionDefault]];
[animationsetFromValue:[NSValuevalueWithCGPoint:x]];
[animationsetToValue:[NSValuevalueWithCGPoint:y]];
[animationsetAutoreverses:YES];
[animationsetDuration:.06];
[animationsetRepeatCount:3];
[viewLayeraddAnimation:animationforKey:nil];
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。