本文实例为大家分享了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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#import "dragerviewcontroller.h"
#define screenw [uiscreen mainscreen].bounds.size.width
@interface dragerviewcontroller ()
@property (nonatomic, weak) uiview *leftv;
@property (nonatomic, weak) uiview *rightv;
@property (nonatomic, weak) uiview *mainv;
@end
@implementation dragerviewcontroller
- ( void )viewdidload {
[super viewdidload];
//添加子控件
[self setup];
//添加手势
uipangesturerecognizer *pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(pan:)];
[self.mainv addgesturerecognizer:pan];
//给控制器的view添加点按手势
uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tap)];
[self.view addgesturerecognizer:tap];
}
- ( void )tap{
//让mainv复位
[uiview animatewithduration:0.5 animations:^{
self.mainv.frame = self.view.bounds;
}];
}
#define targetr 275
#define targetl -275
- ( void )pan:(uipangesturerecognizer *)pan{
//获取偏移量
cgpoint transp = [pan translationinview:self.mainv];
//为什么不使用transform,是因为我们还要去修改高度,使用transform,只能修改,x,y
//self.mainv.transform = cgaffinetransformtranslate(self.mainv.transform, transp.x, 0);
self.mainv.frame = [self framewithoffsetx:transp.x];
//判断拖动的方向
if (self.mainv.frame.origin.x > 0){
//向右
self.rightv.hidden = yes;
} else if (self.mainv.frame.origin.x < 0){
//向左
self.rightv.hidden = no;
}
//当手指松开时,做自动定位.
cgfloat target = 0;
if (pan.state == uigesturerecognizerstateended) {
if (self.mainv.frame.origin.x > screenw * 0.5 ) {
//1判断在右侧
//当前view的x有没有大于屏幕宽度的一半,大于就是在右侧
target = targetr;
} else if (cgrectgetmaxx(self.mainv.frame) < screenw * 0.5){
//2.判断在左侧
//当前view的最大的x有没有小于屏幕宽度的一半,小于就是在左侧
target = targetl;
}
//计算当前mainv的frame.
cgfloat offset = target - self.mainv.frame.origin.x;
[uiview animatewithduration:0.5 animations:^{
self.mainv.frame = [self framewithoffsetx:offset];
}];
}
//复位
[pan settranslation:cgpointzero inview:self.mainv];
}
#define maxy 100
//根据偏移量计算mainv的frame
- (cgrect)framewithoffsetx:(cgfloat)offsetx {
nslog(@ "offsetx===%f" ,offsetx);
cgrect frame = self.mainv.frame;
nslog(@ "x====%f" ,frame.origin.x);
frame.origin.x += offsetx;
//当拖动的view的x值等于屏幕宽度时,maxy为最大,最大为100
// 375 * 100 / 375 = 100
//对计算的结果取绝对值
cgfloat y = fabs ( frame.origin.x * maxy / screenw);
frame.origin.y = y;
//屏幕的高度减去两倍的y值
frame.size.height = [uiscreen mainscreen].bounds.size.height - (2 * frame.origin.y);
return frame;
}
- ( void )setup{
//leftv
uiview *leftv = [[uiview alloc] initwithframe:self.view.bounds];
leftv.backgroundcolor = [uicolor bluecolor];
self.leftv = leftv;
[self.view addsubview:leftv];
//rightv
uiview *rightv = [[uiview alloc] initwithframe:self.view.bounds];
rightv.backgroundcolor = [uicolor greencolor];
self.rightv = rightv;
[self.view addsubview:rightv];
//mianv
uiview *mainv = [[uiview alloc] initwithframe:self.view.bounds];
mainv.backgroundcolor = [uicolor redcolor];
self.mainv = mainv;
[self.view addsubview:mainv];
}
@end
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u010981736/article/details/77447098