-
Notifications
You must be signed in to change notification settings - Fork 0
/
JBSignatureController.m
270 lines (215 loc) · 9.02 KB
/
JBSignatureController.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// JBSignatureController.m
// JBSignatureController
//
// Created by Jesse Bunch on 12/10/11.
// Copyright (c) 2011 Jesse Bunch. All rights reserved.
//
#import "JBSignatureController.h"
#import "JBSignatureView.h"
#pragma mark - *** Private Interface ***
@interface JBSignatureController() {
@private
__strong JBSignatureView *signatureView_;
__strong UIImageView *signaturePanelBackgroundImageView_;
__strong UIImage *portraitBackgroundImage_, *landscapeBackgroundImage_;
__strong UIButton *confirmButton_, *cancelButton_;
__weak id<JBSignatureControllerDelegate> delegate_;
}
// The view responsible for handling signature sketching
@property(nonatomic,strong) JBSignatureView *signatureView;
// The background image underneathe the sketch
@property(nonatomic,strong) UIImageView *signaturePanelBackgroundImageView;
// Private Methods
-(void)didTapConfirmButton;
-(void)didTapCancelButton;
@end
@implementation JBSignatureController
@synthesize
signaturePanelBackgroundImageView = signaturePanelBackgroundImageView_,
signatureView = signatureView_,
portraitBackgroundImage = portraitBackgroundImage_,
landscapeBackgroundImage = landscapeBackgroundImage_,
confirmButton = confirmButton_,
cancelButton = cancelButton_,
delegate = delegate_,
firstNameField = _firstNameField,
lastNameField = _lastNameField,
emailAddressField = _emailAddressField;
#pragma mark - *** Initializers ***
/**
* Designated initializer
* @author Jesse Bunch
**/
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
/**
* Initializer
* @author Jesse Bunch
**/
-(id)init {
return [self initWithNibName:nil bundle:nil];
}
#pragma mark - *** View Lifecycle ***
/**
* Since we're not using a nib. We need to load our views manually.
* @author Jesse Bunch
**/
-(void)loadView {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Background images
self.portraitBackgroundImage = [UIImage imageNamed:@"bg-signature-portrait"];
self.landscapeBackgroundImage = [UIImage imageNamed:@"bg-signature-landscape"];
self.signaturePanelBackgroundImageView = [[UIImageView alloc] initWithImage:self.portraitBackgroundImage];
// Signature view
self.signatureView = [[JBSignatureView alloc] init];
// Confirm
self.confirmButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.confirmButton setTitle:@"Confirm" forState:UIControlStateNormal];
[self.confirmButton sizeToFit];
[self.confirmButton setFrame:CGRectMake(self.view.frame.size.width - self.confirmButton.frame.size.width - 10.0f,
10.0f,
self.confirmButton.frame.size.width,
self.confirmButton.frame.size.height)];
[self.confirmButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
// Cancel
self.cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[self.cancelButton sizeToFit];
[self.cancelButton setFrame:CGRectMake(10.0f,
10.0f,
self.cancelButton.frame.size.width,
self.cancelButton.frame.size.height)];
[self.cancelButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
// AI specfic fields
_firstNameField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, (cancelButton_.frame.origin.y + cancelButton_.frame.size.height) + 20, self.view.frame.size.width / 2, 35.0f)];
_firstNameField.placeholder = @"First Name";
[_firstNameField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[_firstNameField setBorderStyle:UITextBorderStyleRoundedRect];
[_firstNameField setFont:[UIFont systemFontOfSize:18.0]];
[_firstNameField setAutocorrectionType:UITextAutocorrectionTypeNo];
[_firstNameField setReturnKeyType:UIReturnKeyNext];
_firstNameField.delegate = self;
_lastNameField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, (_firstNameField.frame.origin.y + _firstNameField.frame.size.height) + 10, self.view.frame.size.width / 2, 35.0f)];
_lastNameField.placeholder = @"Last Name";
[_lastNameField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[_lastNameField setBorderStyle:UITextBorderStyleRoundedRect];
[_lastNameField setFont:[UIFont systemFontOfSize:18.0]];
[_lastNameField setAutocorrectionType:UITextAutocorrectionTypeNo];
[_lastNameField setReturnKeyType:UIReturnKeyNext];
_lastNameField.delegate = self;
_emailAddressField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, (_lastNameField.frame.origin.y + _lastNameField.frame.size.height) + 10, self.view.frame.size.width / 2, 35.0f)];
_emailAddressField.placeholder = @"Email Address";
[_emailAddressField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[_emailAddressField setBorderStyle:UITextBorderStyleRoundedRect];
[_emailAddressField setTextColor:[UIColor blueColor]];
[_emailAddressField setFont:[UIFont systemFontOfSize:18.0]];
[_emailAddressField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[_emailAddressField setAutocorrectionType:UITextAutocorrectionTypeNo];
[_emailAddressField setKeyboardType:UIKeyboardTypeEmailAddress];
[_emailAddressField setReturnKeyType:UIReturnKeyGo];
_emailAddressField.delegate = self;
[_firstNameField becomeFirstResponder];
}
/**
* Setup the view heirarchy
* @author Jesse Bunch
**/
-(void)viewDidLoad {
// Background Image
[self.signaturePanelBackgroundImageView setFrame:self.view.bounds];
[self.signaturePanelBackgroundImageView setContentMode:UIViewContentModeTopLeft];
[self.view addSubview:self.signaturePanelBackgroundImageView];
// Signature View
[self.signatureView setFrame:self.view.bounds];
[self.view addSubview:self.signatureView];
// Buttons
[self.view addSubview:self.cancelButton];
[self.view addSubview:self.confirmButton];
// Button actions
[self.confirmButton addTarget:self action:@selector(didTapConfirmButton) forControlEvents:UIControlEventTouchUpInside];
[self.cancelButton addTarget:self action:@selector(didTapCancelButton) forControlEvents:UIControlEventTouchUpInside];
// AI specfic fields
[self.view addSubview:_firstNameField];
[self.view addSubview:_lastNameField];
[self.view addSubview:_emailAddressField];
}
/**
* Support for different orientations
* @author Jesse Bunch
**/
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
/**
* Upon rotation, switch out the background image
* @author Jesse Bunch
**/
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[self.signaturePanelBackgroundImageView setImage:self.landscapeBackgroundImage];
} else {
[self.signaturePanelBackgroundImageView setImage:self.portraitBackgroundImage];
}
}
/**
* After rotation, we need to adjust the signature view's frame to fill.
* @author Jesse Bunch
**/
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self.signatureView setFrame:self.view.bounds];
[self.signatureView setNeedsDisplay];
}
#pragma mark - *** Actions ***
/**
* Upon confirmation, message the delegate with the image of the signature.
* @author Jesse Bunch
**/
-(void)didTapConfirmButton {
if (self.delegate && [self.delegate respondsToSelector:@selector(signatureConfirmed:signatureController:)]) {
UIImage *signatureImage = [self.signatureView getSignatureImage];
[self.delegate signatureConfirmed:signatureImage signatureController:self];
}
}
/**
* Upon cancellation, message the delegate.
* @author Jesse Bunch
**/
-(void)didTapCancelButton {
if (self.delegate && [self.delegate respondsToSelector:@selector(signatureCancelled:)]) {
[self.delegate signatureCancelled:self];
}
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark - *** Public Methods ***
/**
* Clears the signature from the signature view. If the delegate is subscribed,
* this method also messages the delegate with the image before it's cleared.
* @author Jesse Bunch
**/
-(void)clearSignature {
if (self.delegate && [self.delegate respondsToSelector:@selector(signatureCleared:signatureController:)]) {
UIImage *signatureImage = [self.signatureView getSignatureImage];
[self.delegate signatureCleared:signatureImage signatureController:self];
}
[self.signatureView clearSignature];
}
#pragma mark - textFieldDelegate methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField ==_firstNameField){
[_lastNameField becomeFirstResponder];
} else if(textField == _lastNameField){
[_emailAddressField becomeFirstResponder];
} else if(textField == _emailAddressField) {
[_emailAddressField resignFirstResponder];
}
return YES;
}
- (BOOL)disablesAutomaticKeyboardDismissal {
return NO;
}
@end