您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關怎么在iOS中實現一個圖片抖動效果,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
#import "ViewController.h" #define angle2Rad(angle) ((angle) / 180.0 *M_PI) @interface ViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageV; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.imageV.userInteractionEnabled = YES; //添加長按手勢 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self.imageV addGestureRecognizer:longPress]; } - (void)longPress:(UILongPressGestureRecognizer *)longPress{ //創建動畫對象 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; anim.keyPath = @"transform.rotation"; anim.values = @[@(angle2Rad(-5)),@(angle2Rad(5))]; anim.repeatCount = MAXFLOAT; // anim.duration = 1; anim.autoreverses = YES; [self.imageV.layer addAnimation:anim forKey:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
小編再給大家補充一段iOS UIView視圖抖動效果的實現代碼:
/** * 抖動效果 * * @param view 要抖動的view */ - (void)shakeAnimationForView:(UIView *) view { CALayer *viewLayer = view.layer; CGPoint position = viewLayer.position; CGPoint x = CGPointMake(position.x + 1, position.y); CGPoint y = CGPointMake(position.x - 1, position.y); CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]]; [animation setFromValue:[NSValue valueWithCGPoint:x]]; [animation setToValue:[NSValue valueWithCGPoint:y]]; [animation setAutoreverses:YES]; [animation setDuration:.06]; [animation setRepeatCount:3]; [viewLayer addAnimation:animation forKey:nil]; }
上述就是小編為大家分享的怎么在iOS中實現一個圖片抖動效果了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。