|
| 1 | +// |
| 2 | +// RootViewController.m |
| 3 | +// UIPasscodeViewControllerDemo |
| 4 | +// |
| 5 | +// Created by Lasha Dolidze on 7/7/10. |
| 6 | +// Copyright Picktek LLC 2010. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "UIPasscodeViewController.h" |
| 10 | + |
| 11 | +/////////////////////////////////////////////////////////////////////////////////// |
| 12 | +// Private Methods |
| 13 | +@interface UIPasscodeViewController (Private) |
| 14 | +- (UITextField*) createPasscodeEntry:(CGRect)textFieldFrame tag:(NSInteger)tag; |
| 15 | +- (UIView*) createPanel:(CGRect)rect tagIndex:(NSInteger)tagIndex; |
| 16 | +- (void) switchPanel:(NSInteger)panelTag; |
| 17 | +@end |
| 18 | + |
| 19 | +@implementation UIPasscodeViewController |
| 20 | + |
| 21 | +@synthesize currentPanel; |
| 22 | +@synthesize delegate = _delegate; |
| 23 | + |
| 24 | +#pragma mark - |
| 25 | +#pragma mark View lifecycle |
| 26 | + |
| 27 | +-(id)initWithDelegate:(id)delegate { |
| 28 | + if ((self = [super initWithStyle:UITableViewStyleGrouped])) { |
| 29 | + _delegate = delegate; |
| 30 | + } |
| 31 | + return self; |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +- (UITextField*) createPasscodeEntry:(CGRect)textFieldFrame tag:(NSInteger)tag |
| 36 | +{ |
| 37 | + //CGRect textFieldFrame = CGRectMake(40.0, 60.0, 60.0, 60.0); |
| 38 | + UITextField *textField = [[UITextField alloc] initWithFrame:textFieldFrame]; |
| 39 | + [textField setBorderStyle:UITextBorderStyleBezel]; |
| 40 | + [textField setTextColor:[UIColor blackColor]]; |
| 41 | + [textField setTextAlignment:UITextAlignmentCenter]; |
| 42 | + [textField setFont:[UIFont systemFontOfSize:41]]; |
| 43 | + [textField setTag:tag]; |
| 44 | + [textField setSecureTextEntry:YES]; |
| 45 | + [textField setBackgroundColor:[UIColor whiteColor]]; |
| 46 | + textField.keyboardType = UIKeyboardTypeNumberPad; |
| 47 | + |
| 48 | + return textField; |
| 49 | +} |
| 50 | + |
| 51 | +- (UIView*) createPanel:(CGRect)rect tagIndex:(NSInteger)tagIndex |
| 52 | +{ |
| 53 | + UIView *panelView = [[UILabel alloc] initWithFrame:rect]; |
| 54 | + [panelView setTag:tagIndex]; |
| 55 | + [panelView setBounds:rect]; |
| 56 | + [panelView setBackgroundColor:[UIColor clearColor]]; |
| 57 | + |
| 58 | + UITextField *textField = [self createPasscodeEntry:CGRectMake(rect.origin.x + 25.0, 60.0, kPasscodeEntryWidth, kPasscodeEntryHeight) tag:tagIndex + 1]; |
| 59 | + [panelView addSubview:textField]; |
| 60 | + [textField release]; |
| 61 | + |
| 62 | + textField = [self createPasscodeEntry:CGRectMake(rect.origin.x + 95.0, 60.0, kPasscodeEntryWidth, kPasscodeEntryHeight) tag:tagIndex + 2]; |
| 63 | + [panelView addSubview:textField]; |
| 64 | + [textField release]; |
| 65 | + |
| 66 | + textField = [self createPasscodeEntry:CGRectMake(rect.origin.x + 165.0, 60.0, kPasscodeEntryWidth, kPasscodeEntryHeight) tag:tagIndex + 3]; |
| 67 | + [panelView addSubview:textField]; |
| 68 | + [textField release]; |
| 69 | + |
| 70 | + textField = [self createPasscodeEntry:CGRectMake(rect.origin.x + 235.0, 60.0, kPasscodeEntryWidth, kPasscodeEntryHeight) tag:tagIndex + 4]; |
| 71 | + [panelView addSubview:textField]; |
| 72 | + [textField release]; |
| 73 | + |
| 74 | + |
| 75 | + // Create title |
| 76 | + CGRect labelFrame = CGRectMake(rect.origin.x + 25.0, 22.0, kPasscodeEntryWidth * 4 + 30, 30.0); |
| 77 | + UILabel *label = [[UILabel alloc] initWithFrame:labelFrame]; |
| 78 | + [label setTag:kPasscodePanelTitleTag]; |
| 79 | + [label setFont:[UIFont boldSystemFontOfSize:15]]; |
| 80 | + [label setTextAlignment:UITextAlignmentCenter]; |
| 81 | + [label setTextColor:[UIColor colorWithRed:66.0/255.0 green:85.0/255.0 blue:102.0/255.0 alpha:1.0]]; |
| 82 | + [label setBackgroundColor:[UIColor clearColor]]; |
| 83 | + [panelView addSubview:label]; |
| 84 | + [label release]; |
| 85 | + |
| 86 | + // Create summary |
| 87 | + labelFrame = CGRectMake(rect.origin.x + 25.0, 130.0, kPasscodeEntryWidth * 4 + 30, 40.0); |
| 88 | + label = [[UILabel alloc] initWithFrame:labelFrame]; |
| 89 | + [label setTag:kPasscodePanelSummaryTag]; |
| 90 | + [label setFont:[UIFont boldSystemFontOfSize:12]]; |
| 91 | + [label setNumberOfLines:0]; |
| 92 | + [label setBaselineAdjustment:UIBaselineAdjustmentNone]; |
| 93 | + [label setTextAlignment:UITextAlignmentCenter]; |
| 94 | + [label setTextColor:[UIColor colorWithRed:66.0/255.0 green:85.0/255.0 blue:102.0/255.0 alpha:1.0]]; |
| 95 | + [label setBackgroundColor:[UIColor clearColor]]; |
| 96 | + [panelView addSubview:label]; |
| 97 | + [label release]; |
| 98 | + |
| 99 | + return panelView; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +- (void) switchPanel:(NSInteger)panelTag |
| 104 | +{ |
| 105 | + CGRect rect = _scrollView.frame; |
| 106 | + |
| 107 | + [UIView beginAnimations:nil context:nil]; |
| 108 | + [UIView setAnimationDuration:0.2]; |
| 109 | + [_scrollView setFrame:CGRectMake(panelTag, rect.origin.y, rect.size.width, rect.size.height)]; |
| 110 | + [UIView commitAnimations]; |
| 111 | + |
| 112 | + currentPanel = (UIView*)[_scrollView viewWithTag:panelTag]; |
| 113 | + |
| 114 | + if ([self.delegate respondsToSelector:@selector(didShowPasscodePanel:panelView:)]) { |
| 115 | + [self.delegate didShowPasscodePanel:self panelView:currentPanel]; |
| 116 | + } |
| 117 | + |
| 118 | + [self clearPanel]; |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +- (void)viewDidLoad { |
| 123 | + |
| 124 | + [super viewDidLoad]; |
| 125 | + |
| 126 | + CGRect frm = CGRectMake(0.0, 0.0, kPasscodePanelWidth * kPasscodePanelCount, kPasscodePanelHeight); |
| 127 | + _scrollView = [[UILabel alloc] initWithFrame:frm]; |
| 128 | + |
| 129 | + [_scrollView setBackgroundColor:[UIColor clearColor]]; |
| 130 | + [self.view addSubview:_scrollView]; |
| 131 | + |
| 132 | + // Create Panel One |
| 133 | + UIView *panelView = [self createPanel:CGRectMake(0.0, 0.0, kPasscodePanelWidth, kPasscodePanelHeight) tagIndex:kPasscodePanelOne]; |
| 134 | + [_scrollView addSubview:panelView]; |
| 135 | + [self switchPanel:kPasscodePanelOne]; |
| 136 | + [panelView release]; |
| 137 | + // ------------------------------------------- |
| 138 | + |
| 139 | + // Create Panel Two |
| 140 | + panelView = [self createPanel:CGRectMake(kPasscodePanelWidth, 0.0, kPasscodePanelWidth, kPasscodePanelHeight) tagIndex:kPasscodePanelTwo]; |
| 141 | + [_scrollView addSubview:panelView]; |
| 142 | + [panelView release]; |
| 143 | + // ------------------------------------------- |
| 144 | + |
| 145 | + // Create Panel Three |
| 146 | + panelView = [self createPanel:CGRectMake(kPasscodePanelWidth * 2, 0.0, kPasscodePanelWidth, kPasscodePanelHeight) tagIndex:kPasscodePanelThree]; |
| 147 | + [_scrollView addSubview:panelView]; |
| 148 | + [panelView release]; |
| 149 | + // ------------------------------------------- |
| 150 | + |
| 151 | + // Create frake input text |
| 152 | + UITextField *textField = [[UITextField alloc] initWithFrame:CGRectZero]; |
| 153 | + [textField setDelegate:self]; |
| 154 | + [textField setTag:kPasscodeFakeTextField]; |
| 155 | + [textField setHidden:YES]; |
| 156 | + textField.keyboardType = UIKeyboardTypeNumberPad; |
| 157 | + [self.view addSubview:textField]; |
| 158 | + [textField release]; |
| 159 | + [textField becomeFirstResponder]; |
| 160 | + // ------------------------------------------- |
| 161 | + |
| 162 | + |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +#pragma mark Public methods |
| 167 | + |
| 168 | +- (void) clearPanel |
| 169 | +{ |
| 170 | + NSInteger panelTag = [currentPanel tag]; |
| 171 | + |
| 172 | + UITextField *entryText = (UITextField*)[currentPanel viewWithTag:panelTag + 1]; |
| 173 | + [entryText setText:@""]; |
| 174 | + |
| 175 | + entryText = (UITextField*)[currentPanel viewWithTag:panelTag + 2]; |
| 176 | + [entryText setText:@""]; |
| 177 | + |
| 178 | + entryText = (UITextField*)[currentPanel viewWithTag:panelTag + 3]; |
| 179 | + [entryText setText:@""]; |
| 180 | + |
| 181 | + entryText = (UITextField*)[currentPanel viewWithTag:panelTag + 4]; |
| 182 | + [entryText setText:@""]; |
| 183 | + |
| 184 | + // Clear fake text field |
| 185 | + UITextField *textField = (UITextField*)[self.view viewWithTag:kPasscodeFakeTextField]; |
| 186 | + [textField setText:@""]; |
| 187 | +} |
| 188 | + |
| 189 | +-(UILabel*)titleLabel { |
| 190 | + return (UILabel*)[currentPanel viewWithTag:kPasscodePanelTitleTag]; |
| 191 | +} |
| 192 | + |
| 193 | +-(UILabel*)summaryLabel { |
| 194 | + return (UILabel*)[currentPanel viewWithTag:kPasscodePanelSummaryTag]; |
| 195 | +} |
| 196 | + |
| 197 | +-(BOOL)prevPanel |
| 198 | +{ |
| 199 | + NSInteger tag = [currentPanel tag]; |
| 200 | + if(tag != 0) { |
| 201 | + [self switchPanel:tag + kPasscodePanelWidth]; |
| 202 | + return TRUE; |
| 203 | + } |
| 204 | + return FALSE; |
| 205 | +} |
| 206 | + |
| 207 | +-(BOOL)nextPanel |
| 208 | +{ |
| 209 | + NSInteger tag = [currentPanel tag]; |
| 210 | + if(tag != -640) { |
| 211 | + [self switchPanel:tag - kPasscodePanelWidth]; |
| 212 | + return TRUE; |
| 213 | + } |
| 214 | + return FALSE; |
| 215 | +} |
| 216 | + |
| 217 | +- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string |
| 218 | +{ |
| 219 | + BOOL ret = FALSE; |
| 220 | + |
| 221 | + NSString *passcode = [textField text]; |
| 222 | + passcode = [passcode stringByReplacingCharactersInRange:range withString:string]; |
| 223 | + |
| 224 | + //NSLog(@"passcode - %@", passcode); |
| 225 | + |
| 226 | + NSInteger index = [passcode length]; |
| 227 | + if([string length] == 0) { |
| 228 | + index++; |
| 229 | + } |
| 230 | + |
| 231 | + if(index <= 4) { |
| 232 | + NSInteger tag = [currentPanel tag]; |
| 233 | + UITextField *tf = (UITextField*)[currentPanel viewWithTag:tag + index]; |
| 234 | + // NSLog(@"%@ - %@ - %@, %d, %d", string, [textField text], passcode, tag, index); |
| 235 | + |
| 236 | + if ([self.delegate respondsToSelector:@selector(shouldChangePasscode:panelView:passCode:lastNumber:)]) { |
| 237 | + if([self.delegate shouldChangePasscode:self panelView:currentPanel passCode:[passcode intValue] lastNumber:[string intValue]]) { |
| 238 | + [tf setText:string]; |
| 239 | + ret = TRUE; |
| 240 | + } |
| 241 | + } else { |
| 242 | + [tf setText:string]; |
| 243 | + ret = TRUE; |
| 244 | + } |
| 245 | + |
| 246 | + // Did end passcode |
| 247 | + if(index == 4 && ret) { |
| 248 | + if ([self.delegate respondsToSelector:@selector(didEndPasscodeEditing:panelView:passCode:)]) { |
| 249 | + return [self.delegate didEndPasscodeEditing:self panelView:currentPanel passCode:[passcode intValue]]; |
| 250 | + //ret = FALSE; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return ret; |
| 255 | + } |
| 256 | + |
| 257 | + return ret; |
| 258 | +} |
| 259 | + |
| 260 | +/* |
| 261 | +- (void)viewWillAppear:(BOOL)animated { |
| 262 | + [super viewWillAppear:animated]; |
| 263 | +} |
| 264 | +*/ |
| 265 | +/* |
| 266 | +- (void)viewDidAppear:(BOOL)animated { |
| 267 | + [super viewDidAppear:animated]; |
| 268 | +} |
| 269 | +*/ |
| 270 | +/* |
| 271 | +- (void)viewWillDisappear:(BOOL)animated { |
| 272 | + [super viewWillDisappear:animated]; |
| 273 | +} |
| 274 | +*/ |
| 275 | +/* |
| 276 | +- (void)viewDidDisappear:(BOOL)animated { |
| 277 | + [super viewDidDisappear:animated]; |
| 278 | +} |
| 279 | +*/ |
| 280 | + |
| 281 | +/* |
| 282 | + // Override to allow orientations other than the default portrait orientation. |
| 283 | +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { |
| 284 | + // Return YES for supported orientations. |
| 285 | + return (interfaceOrientation == UIInterfaceOrientationPortrait); |
| 286 | +} |
| 287 | + */ |
| 288 | + |
| 289 | + |
| 290 | + |
| 291 | +#pragma mark - |
| 292 | +#pragma mark Memory management |
| 293 | + |
| 294 | +- (void)didReceiveMemoryWarning { |
| 295 | + // Releases the view if it doesn't have a superview. |
| 296 | + [super didReceiveMemoryWarning]; |
| 297 | + |
| 298 | + // Relinquish ownership any cached data, images, etc that aren't in use. |
| 299 | +} |
| 300 | + |
| 301 | +- (void)viewDidUnload { |
| 302 | + // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. |
| 303 | + // For example: self.myOutlet = nil; |
| 304 | +} |
| 305 | + |
| 306 | + |
| 307 | +- (void)dealloc { |
| 308 | + [_scrollView release]; |
| 309 | + |
| 310 | + [super dealloc]; |
| 311 | +} |
| 312 | + |
| 313 | + |
| 314 | +@end |
| 315 | + |
0 commit comments