Repository navigation
Expand file tree
/
Copy pathPanToBack.m
More file actions
170 lines (129 loc) · 4.62 KB
/
Copy pathPanToBack.m
File metadata and controls
170 lines (129 loc) · 4.62 KB
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
//
// SwipeToBack.m
// travelogue
//
// Created by soleaf on 13. 11. 7..
// Copyright (c) 2013년 soleaf. All rights reserved.
//
#import "PanToBack.h"
@interface PanToBack () <UIGestureRecognizerDelegate>
{
UIPanGestureRecognizer *panGestureRec;
UIView *targetView;
BOOL backSwipeReversed;
CGFloat backSwipeBegan;
}
@end
@implementation PanToBack
#pragma mark - Init
-(void)linkView:(UIView *)newTargetView;
{
targetView = newTargetView;;
panGestureRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
panGestureRec.delegate = self;
[newTargetView addGestureRecognizer:panGestureRec];
if (!self.backText) self.backText = @"Back";
self.backText = self.backText;
[self makeBackIndicator];
}
-(void) makeBackIndicator
{
if (self.ui_backIndicator) return;
// Indicator Make
self.ui_backIndicator = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 80, 80)];
self.ui_backIndicator.font = [UIFont boldSystemFontOfSize:17];
self.ui_backIndicator.textColor = [UIColor whiteColor];
self.ui_backIndicator.textAlignment = NSTextAlignmentCenter;
self.ui_backIndicator.text = self.backText;
float statusbarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
[self move:self.ui_backIndicator
toY:((targetView.frame.size.height/2) - (self.ui_backIndicator.frame.size.height/2)-statusbarHeight)];
self.ui_backIndicator.layer.cornerRadius = self.ui_backIndicator.frame.size.width/2;
[targetView addSubview:self.ui_backIndicator];
}
#pragma mark - Gesture
- (void)handlePanGesture:(UIPanGestureRecognizer *)panGesture //Your action method
{
if (self.isDissabled) return;
self.ui_backIndicator.alpha = 1.;
float center = targetView.frame.size.width/2 - self.ui_backIndicator.frame.size.width/2;
switch(panGesture.state) {
case UIGestureRecognizerStateChanged:
{
if (self.ui_backIndicator.frame.origin.x == -80 && [panGesture velocityInView:targetView].x <=0) return;
/*
Define Backer Location
*/
float moveTo = 0;
// Direction ->
if ([panGesture velocityInView:targetView].x >= 0 && !backSwipeReversed){
moveTo = [panGesture locationInView:targetView].x - backSwipeBegan - 80;
}
// Direction <-
else{
moveTo = [panGesture locationInView:targetView].x;
backSwipeReversed = YES;
}
/*
Animation
*/
// Arrived to center
if (moveTo > center){
[UIView animateWithDuration:0.3 animations:^{
[self move:self.ui_backIndicator toX:center];
}];
self.ui_backIndicator.backgroundColor = [UIColor colorWithRed:1.00 green:0.47 blue:0.00 alpha:0.8];
}
// Moving...
else{
[UIView animateWithDuration:0.3 animations:^{
[self move:self.ui_backIndicator toX:moveTo];
}];
self.ui_backIndicator.backgroundColor = [UIColor colorWithRed:0.00 green:0.00 blue:0.00 alpha:0.8];
}
}
case UIGestureRecognizerStateBegan:
// Saving Begin position
if (backSwipeBegan <= 0)
backSwipeBegan = [panGesture locationInView:targetView].x;
break;
case UIGestureRecognizerStateEnded:{
backSwipeBegan = 0;
backSwipeReversed = NO;
// Action!
if (self.ui_backIndicator.frame.origin.x >= center){
[self back];
}
// Canceled
else{
[UIView animateWithDuration:.3 animations:^{
[self move:self.ui_backIndicator toX:-80];
} completion:^(BOOL finished) {
[self move:self.ui_backIndicator toX:-80];
}];
}
}
break;
default:
break;
}
}
#pragma mark - Assist for moving UIView
- (void) move:(UIView*)view toX:(CGFloat)x
{
CGRect frame = view.frame;
frame.origin.x = x;
view.frame = frame;
}
- (void) move:(UIView*)view toY:(CGFloat)y
{
CGRect frame = view.frame;
frame.origin.y = y;
view.frame = frame;
}
#pragma mark - Action
- (void) back
{
[self.delegate back];
}
@end