1. 程式人生 > 程式設計 >iOS實現圖片抖動效果

iOS實現圖片抖動效果

本文例項為大家分享了iOS實現圖片抖動效果的具體程式碼,供大家參考,具體內容如下

效果圖:

iOS實現圖片抖動效果

核心程式碼:

//
// ViewController.m
// 圖示抖動
//
// Created by llkj on 2017/8/29.
// Copyright © 2017年 LayneCheung. All rights reserved.
//

#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];
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。