-
Notifications
You must be signed in to change notification settings - Fork 1
/
WWSideslipViewController.m
179 lines (132 loc) · 5.09 KB
/
WWSideslipViewController.m
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//
// WWSideslipViewController.m
// WWSideslipViewControllerSample
//
// Created by 王维 on 14-8-26.
// Copyright (c) 2014年 wangwei. All rights reserved.
//
#import "WWSideslipViewController.h"
@interface WWSideslipViewController ()
@end
@implementation WWSideslipViewController
@synthesize speedf,sideslipTapGes;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//[self.view addSubview:mainControl.view];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(instancetype)initWithLeftView:(UIViewController *)LeftView
andMainView:(UIViewController *)MainView
andRightView:(UIViewController *)RighView
andBackgroundImage:(UIImage *)image;
{
if(self){
speedf = 0.5;
leftControl = LeftView;
mainControl = MainView;
righControl = RighView;
UIImageView * imgview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
[imgview setImage:image];
[self.view addSubview:imgview];
//滑动手势
UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[mainControl.view addGestureRecognizer:pan];
//单击手势
sideslipTapGes= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];
[sideslipTapGes setNumberOfTapsRequired:1];
[mainControl.view addGestureRecognizer:sideslipTapGes];
leftControl.view.hidden = YES;
righControl.view.hidden = YES;
[self.view addSubview:leftControl.view];
[self.view addSubview:righControl.view];
[self.view addSubview:mainControl.view];
}
return self;
}
#pragma mark - 滑动手势
//滑动手势
- (void) handlePan: (UIPanGestureRecognizer *)rec{
CGPoint point = [rec translationInView:self.view];
scalef = (point.x*speedf+scalef);
//根据视图位置判断是左滑还是右边滑动
if (rec.view.frame.origin.x>=0){
rec.view.center = CGPointMake(rec.view.center.x + point.x*speedf,rec.view.center.y);
rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1-scalef/1000,1-scalef/1000);
[rec setTranslation:CGPointMake(0, 0) inView:self.view];
righControl.view.hidden = YES;
leftControl.view.hidden = NO;
}
else
{
rec.view.center = CGPointMake(rec.view.center.x + point.x*speedf,rec.view.center.y);
rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1+scalef/1000,1+scalef/1000);
[rec setTranslation:CGPointMake(0, 0) inView:self.view];
righControl.view.hidden = NO;
leftControl.view.hidden = YES;
}
//手势结束后修正位置
if (rec.state == UIGestureRecognizerStateEnded) {
if (scalef>140*speedf){
[self showLeftView];
}
else if (scalef<-140*speedf) {
[self showRighView]; }
else
{
[self showMainView];
scalef = 0;
}
}
}
#pragma mark - 单击手势
-(void)handeTap:(UITapGestureRecognizer *)tap{
if (tap.state == UIGestureRecognizerStateEnded) {
[UIView beginAnimations:nil context:nil];
tap.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
tap.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
[UIView commitAnimations];
scalef = 0;
}
}
#pragma mark - 修改视图位置
//恢复位置
-(void)showMainView{
[UIView beginAnimations:nil context:nil];
mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
mainControl.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
[UIView commitAnimations];
}
//显示左视图
-(void)showLeftView{
[UIView beginAnimations:nil context:nil];
mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
mainControl.view.center = CGPointMake(340,[UIScreen mainScreen].bounds.size.height/2);
[UIView commitAnimations];
}
//显示右视图
-(void)showRighView{
[UIView beginAnimations:nil context:nil];
mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
mainControl.view.center = CGPointMake(-60,[UIScreen mainScreen].bounds.size.height/2);
[UIView commitAnimations];
}
#warning 为了界面美观,所以隐藏了状态栏。如果需要显示则去掉此代码
- (BOOL)prefersStatusBarHidden
{
return YES; //返回NO表示要显示,返回YES将hiden
}
@end