前言
ios中抽屉效果的简单实现现在很多应用中都使用到了,网上也有很多了例子,本文主要是通过简单的一些代码来实现的,有需要的可以一起学习学习。
下面是效果图
示例代码如下
1
2
3
4
5
|
#import <uikit/uikit.h>
@interface mainviewcontroller : uiviewcontroller
+ (instancetype)mainviewcontrollerwithleftviewcontroller:(uiviewcontroller *)leftviewcontroller withmainpageviewcontroller:(uiviewcontroller *)mainpageviewcontroller;
@end
|
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
|
#import "mainviewcontroller.h"
#define kwidth self.view.frame.size.width
#define kheight self.view.frame.size.height
@interface mainviewcontroller ()
@property (nonatomic,strong)uiviewcontroller *leftvc;
@property (nonatomic,strong)uiviewcontroller *centervc;
@property (nonatomic,assign) bool isslider;
@property (nonatomic,strong)uiview *corverview;
@end
@implementation mainviewcontroller
+ (instancetype)mainviewcontrollerwithleftviewcontroller:(uiviewcontroller *)leftviewcontroller withmainpageviewcontroller:(uiviewcontroller *)mainpageviewcontroller{
mainviewcontroller *mainvc = [[mainviewcontroller alloc] init];
mainvc.leftvc = leftviewcontroller;
mainvc.centervc = mainpageviewcontroller;
return mainvc;
}
- ( void )viewdidload{
[super viewdidload];
self.isslider = no;
self.view.backgroundcolor = [uicolor whitecolor];
[self addchildviewcontroller:self.leftvc];
[self.view addsubview:self.leftvc.view];
[self addchildviewcontroller:self.centervc];
[self.view addsubview:self.centervc.view];
// 给左视图和主视图添加手势
[self addgestureforview];
}
// 给主视图添加遮盖
- ( void )addcorverview{
if (self.corverview) {
[self.corverview removefromsuperview];
self.corverview = nil;
}
self.corverview = [[uiview alloc] initwithframe:self.centervc.view.bounds];
_corverview.backgroundcolor = [[uicolor blackcolor] colorwithalphacomponent:0.0];
[self.centervc.view addsubview:self.corverview];
}
// 移除主视图遮盖
- ( void )removecoverview{
if (self.corverview) {
[self.corverview removefromsuperview];
self.corverview = nil;
}
}
// 给左视图和主视图添加手势
- ( void )addgestureforview{
uiswipegesturerecognizer *rightgesture = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(swiperightaction:)];
rightgesture.direction = uiswipegesturerecognizerdirectionright;
[self.centervc.view addgesturerecognizer:rightgesture];
uiswipegesturerecognizer *leftgesture = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(swipeleftaction:)];
leftgesture.direction = uiswipegesturerecognizerdirectionleft;
[self.centervc.view addgesturerecognizer:leftgesture];
uiswipegesturerecognizer *leftvcleftswipegesture = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(leftvcleftswipeaction:)];
leftvcleftswipegesture.direction = uiswipegesturerecognizerdirectionleft;
[self.leftvc.view addgesturerecognizer:leftvcleftswipegesture];
}
- ( void )swiperightaction:(id)sender{
[self moveview:self.centervc.view scale:0.8 panvalue:kwidth];
self.isslider = yes;
[self addcorverview];
}
- ( void )swipeleftaction:(id)sender{
[self moveview:self.centervc.view scale:1 panvalue:kwidth / 2];
self.isslider = no;
[self removecoverview];
}
- ( void )leftvcleftswipeaction:(id)sender{
[self moveview:self.centervc.view scale:1 panvalue:kwidth / 2];
self.isslider = no;
[self removecoverview];
}
// 平移和缩放一个视图
- ( void )moveview:(uiview *)view scale:(cgfloat)scale panvalue:(cgfloat)value{
[uiview beginanimations:nil context:nil];
view.transform = cgaffinetransformscale(cgaffinetransformidentity,scale,scale);
view.center = cgpointmake(value, view.center.y);
[uiview commitanimations];
}
@end
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能有所帮助,如果有疑问大家可以留言交流。