-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHJThreeStateButton.m
228 lines (181 loc) · 6.58 KB
/
HJThreeStateButton.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
//
// HJThreeStateButton.m
// zzz-sliderManualTest
//
// Created by hasnainjafri on 6/27/14.
// Copyright (c) 2014 hasnainjafri. All rights reserved.
//
#import "HJThreeStateButton.h"
@implementation HJThreeStateButton
{
UIPanGestureRecognizer *panGesture;
}
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setAutoresizesSubviews:NO];
[self setClipsToBounds:NO];
[self createView];
return self;
}
-(void) createView
{
bgImgView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 21)];
bgImgView.image=[UIImage imageNamed:@"Neutral.png"];
[self addSubview:bgImgView];
//=======================================================================================================================================//
ONBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[ONBtn setFrame:CGRectMake(0, 0, 19, 21)];
[ONBtn setBackgroundColor:[UIColor clearColor]];
[ONBtn setTitle:@"" forState:UIControlStateNormal];
[ONBtn addTarget:self action:@selector(ONBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:ONBtn];
NeutralBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[NeutralBtn setFrame:CGRectMake(30, 0, 19, 21)];
[NeutralBtn setBackgroundColor:[UIColor clearColor]];
[NeutralBtn setTitle:@"" forState:UIControlStateNormal];
[NeutralBtn addTarget:self action:@selector(NeutralBtnBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:NeutralBtn];
OFFBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[OFFBtn setFrame:CGRectMake(61, 0, 19, 21)];
[OFFBtn setBackgroundColor:[UIColor clearColor]];
[OFFBtn setTitle:@"" forState:UIControlStateNormal];
[OFFBtn addTarget:self action:@selector(OFFBtnBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:OFFBtn];
//=======================================================================================================================================//
thumbBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[thumbBtn setFrame:CGRectMake(26, -3, 27, 28)];
[thumbBtn setBackgroundImage:[UIImage imageNamed:@"Selector"] forState:UIControlStateNormal];
[thumbBtn setBackgroundImage:[UIImage imageNamed:@"Selector"] forState:UIControlStateSelected];
[thumbBtn setBackgroundImage:[UIImage imageNamed:@"Selector"] forState:UIControlStateHighlighted];
[self addSubview:thumbBtn];
panGesture=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panning:)];
[thumbBtn addGestureRecognizer:panGesture];
}
-(void) set_Enabled:(BOOL)enabled
{
if(enabled==NO)
{
[ONBtn setEnabled:NO];
[OFFBtn setEnabled:NO];
[NeutralBtn setEnabled:NO];
}
else
{
[ONBtn setEnabled:YES];
[OFFBtn setEnabled:YES];
[NeutralBtn setEnabled:YES];
}
}
-(void) setOn:(BOOL) value
{
[self ONBtnPressed:nil];
}
-(void) setOff:(BOOL) value;
{
[self OFFBtnBtnPressed:nil];
}
-(void) setNeutral
{
[self NeutralBtnBtnPressed:nil];
}
-(void) ONBtnPressed:(id) sender
{
self.isOn=!self.isOn;
if(self.isOn)
{
self.isOFF=FALSE;
self.isNeutral=FALSE;
[UIView transitionWithView:bgImgView
duration:0.4f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
bgImgView.image=[UIImage imageNamed:@"ON.png"];
} completion:NULL];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[thumbBtn setFrame:CGRectMake(-3, -3, 27, 28)];
} completion:nil];
[delegate stateChangeForSwitch:self];
}
else
{
[self NeutralBtnBtnPressed:nil];
}
}
-(void) NeutralBtnBtnPressed:(id) sender
{
[UIView transitionWithView:bgImgView
duration:0.4f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
bgImgView.image=[UIImage imageNamed:@"Neutral.png"];
} completion:NULL];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[thumbBtn setFrame:CGRectMake(26, -3, 27, 28)];
} completion:nil];
self.isOn=FALSE;
self.isOFF=FALSE;
self.isNeutral=TRUE;
[delegate stateChangeForSwitch:self];
}
-(void) OFFBtnBtnPressed:(id) sender
{
self.isOFF=!self.isOFF;
if(self.isOFF)
{
self.isOn=FALSE;
self.isNeutral=FALSE;
[UIView transitionWithView:bgImgView
duration:0.4f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
bgImgView.image=[UIImage imageNamed:@"OFF.png"];
} completion:NULL];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
[thumbBtn setFrame:CGRectMake(58, -3, 27, 28)];
} completion:nil];
[delegate stateChangeForSwitch:self];
}
else
{
[self NeutralBtnBtnPressed:nil];
}
}
-(void) panning:(UIPanGestureRecognizer*) recoganizer
{
CGPoint translation = [recoganizer translationInView:self];
if(recoganizer.view.frame.origin.x>-4 && (recoganizer.view.frame.origin.x<(86-27))) //27 is width of thumb
{
recoganizer.view.center = CGPointMake(recoganizer.view.center.x+translation.x, recoganizer.view.center.y);
[recoganizer setTranslation:CGPointMake(0, 0) inView:self];
}
NSLog(@"%.2f",recoganizer.view.frame.origin.x);
if (recoganizer.state == UIGestureRecognizerStateEnded)
{
if(recoganizer.view.frame.origin.x<14)
{
[self ONBtnPressed:nil];
}
else if (recoganizer.view.frame.origin.x>3 && recoganizer.view.frame.origin.x<19)
{
[self NeutralBtnBtnPressed:nil];
}
else if (recoganizer.view.frame.origin.x>18 && recoganizer.view.frame.origin.x<46)
{
[self NeutralBtnBtnPressed:nil];
}
else if (recoganizer.view.frame.origin.x>45)
{
[self OFFBtnBtnPressed:nil];
}
}
}
-(void) dealloc
{
panGesture=nil;
}
@end