本文实例为大家分享了ios实现图片抖动效果的具体代码,供大家参考,具体内容如下
效果图:
核心代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
//
// 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视图抖动效果的实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/**
* 抖动效果
*
* @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];
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u010981736/article/details/77683517