Permalink
Browse files

first commit

  • Loading branch information...
Johan Kool
Johan Kool committed Mar 17, 2011
0 parents commit ee1088d173e6a0cf322ec959d1482faa5206760e
@@ -0,0 +1,82 @@
+//
+// KVPasscodeViewController.h
+// Koolistov
+//
+// Created by Johan Kool on 3/17/11.
+// Copyright 2011 Koolistov. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other materials
+// provided with the distribution.
+// * Neither the name of KOOLISTOV nor the names of its contributors may be used to
+// endorse or promote products derived from this software without specific prior written
+// permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+//
+// Known issues:
+// - KVPasscodeAnimationStyleConfirm animation not quite perfect (bullets visible in incoming field)
+//
+
+#import <UIKit/UIKit.h>
+
+@class KVPasscodeViewController;
+
+@protocol KVPasscodeViewControllerDelegate <NSObject>
+
+- (void)passcodeController:(KVPasscodeViewController *)controller passcodeEntered:(NSString *)passCode;
+
+@end
+
+typedef enum {
+ KVPasscodeAnimationStyleNone,
+ KVPasscodeAnimationStyleInvalid,
+ KVPasscodeAnimationStyleConfirm
+} KVPasscodeAnimationStyle;
+
+@interface KVPasscodeViewController : UIViewController <UITextFieldDelegate> {
+ id <KVPasscodeViewControllerDelegate> delegate;
+
+ IBOutlet UIView *animationView;
+
+ IBOutlet UILabel *titleLabel;
+ IBOutlet UILabel *instructionLabel;
+
+ IBOutlet UITextField *bulletField0;
+ IBOutlet UITextField *bulletField1;
+ IBOutlet UITextField *bulletField2;
+ IBOutlet UITextField *bulletField3;
+
+ UITextField *fakeField;
+}
+
+@property (nonatomic, assign) id <KVPasscodeViewControllerDelegate> delegate;
+
+@property (nonatomic, retain) IBOutlet UIView *animationView;
+
+@property (nonatomic, retain) IBOutlet UILabel *titleLabel;
+@property (nonatomic, retain) IBOutlet UILabel *instructionLabel;
+
+@property (nonatomic, retain) IBOutlet UITextField *bulletField0;
+@property (nonatomic, retain) IBOutlet UITextField *bulletField1;
+@property (nonatomic, retain) IBOutlet UITextField *bulletField2;
+@property (nonatomic, retain) IBOutlet UITextField *bulletField3;
+
+- (void)resetWithAnimation:(KVPasscodeAnimationStyle)animationStyle;
+
+@end
@@ -0,0 +1,220 @@
+//
+// KVPasscodeViewController.m
+// Koolistov
+//
+// Created by Johan Kool on 3/17/11.
+// Copyright 2011 Koolistov. All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without modification, are
+// permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright notice, this list of
+// conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright notice, this list
+// of conditions and the following disclaimer in the documentation and/or other materials
+// provided with the distribution.
+// * Neither the name of KOOLISTOV nor the names of its contributors may be used to
+// endorse or promote products derived from this software without specific prior written
+// permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
+// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+// THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+//
+
+#import "KVPasscodeViewController.h"
+
+#import <QuartzCore/QuartzCore.h>
+
+@interface KVPasscodeViewController ()
+
+- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
+
+@end
+
+@implementation KVPasscodeViewController
+
+@synthesize delegate;
+
+@synthesize animationView;
+
+@synthesize titleLabel;
+@synthesize instructionLabel;
+
+@synthesize bulletField0;
+@synthesize bulletField1;
+@synthesize bulletField2;
+@synthesize bulletField3;
+
+- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
+ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
+ if (self) {
+ // Custom initialization
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [animationView release], animationView = nil;
+
+ [titleLabel release], titleLabel = nil;
+ [instructionLabel release], instructionLabel = nil;
+
+ [bulletField0 release], bulletField0 = nil;
+ [bulletField1 release], bulletField1 = nil;
+ [bulletField2 release], bulletField2 = nil;
+ [bulletField3 release], bulletField3 = nil;
+ [super dealloc];
+}
+
+- (void)didReceiveMemoryWarning {
+ // Releases the view if it doesn't have a superview.
+ [super didReceiveMemoryWarning];
+
+ // Release any cached data, images, etc that aren't in use.
+}
+
+#pragma mark - View lifecycle
+
+- (void)viewDidLoad {
+ [super viewDidLoad];
+
+ fakeField = [[UITextField alloc] initWithFrame:CGRectZero];
+ fakeField.delegate = self;
+ fakeField.keyboardType = UIKeyboardTypeNumberPad;
+ fakeField.text = @"";
+ [fakeField becomeFirstResponder];
+ [self.view addSubview:fakeField];
+ [fakeField release];
+
+ // Do any additional setup after loading the view from its nib.
+ self.navigationItem.title = NSLocalizedString(@"Passcode", @"");
+}
+
+- (void)viewDidUnload {
+ [super viewDidUnload];
+ // Release any retained subviews of the main view.
+ // e.g. self.myOutlet = nil;
+ fakeField = nil;
+
+ self.animationView = nil;
+
+ self.titleLabel = nil;
+ self.instructionLabel = nil;
+
+ self.bulletField0 = nil;
+ self.bulletField1 = nil;
+ self.bulletField2 = nil;
+ self.bulletField3 = nil;
+}
+
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
+ // Return YES for supported orientations
+ return (interfaceOrientation == UIInterfaceOrientationPortrait);
+}
+
+- (void)resetWithAnimation:(KVPasscodeAnimationStyle)animationStyle {
+ switch (animationStyle) {
+ case KVPasscodeAnimationStyleInvalid:
+ ;
+ CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
+ [animation setDelegate:self];
+ [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
+ [animation setDuration:0.025];
+ [animation setRepeatCount:8];
+ [animation setAutoreverses:YES];
+ [animation setFromValue:[NSValue valueWithCGPoint:
+ CGPointMake([animationView center].x - 14.0f, [animationView center].y)]];
+ [animation setToValue:[NSValue valueWithCGPoint:
+ CGPointMake([animationView center].x + 14.0f, [animationView center].y)]];
+ [[animationView layer] addAnimation:animation forKey:@"position"];
+ break;
+ case KVPasscodeAnimationStyleConfirm:
+ ;
+ CATransition *transition = [CATransition animation];
+ [transition setDelegate:self];
+ [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:)];
+ [transition setType:kCATransitionPush];
+ [transition setSubtype:kCATransitionFromRight];
+ [transition setDuration:0.5f];
+ [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
+ [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
+ [[animationView layer] addAnimation:transition forKey:@"swipe"];
+ break;
+ case KVPasscodeAnimationStyleNone:
+ default:
+ self.bulletField0.text = nil;
+ self.bulletField1.text = nil;
+ self.bulletField2.text = nil;
+ self.bulletField3.text = nil;
+
+ fakeField.text = @"";
+ break;
+ }
+}
+
+#pragma mark - CAAnimationDelegate
+- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
+ self.bulletField0.text = nil;
+ self.bulletField1.text = nil;
+ self.bulletField2.text = nil;
+ self.bulletField3.text = nil;
+
+ fakeField.text = @"";
+}
+
+#pragma mark - UITextFieldDelegate
+- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
+ NSString *passcode = [textField text];
+ passcode = [passcode stringByReplacingCharactersInRange:range withString:string];
+
+ switch ([passcode length]) {
+ case 0:
+ self.bulletField0.text = nil;
+ self.bulletField1.text = nil;
+ self.bulletField2.text = nil;
+ self.bulletField3.text = nil;
+ break;
+ case 1:
+ self.bulletField0.text = @"*";
+ self.bulletField1.text = nil;
+ self.bulletField2.text = nil;
+ self.bulletField3.text = nil;
+ break;
+ case 2:
+ self.bulletField0.text = @"*";
+ self.bulletField1.text = @"*";
+ self.bulletField2.text = nil;
+ self.bulletField3.text = nil;
+ break;
+ case 3:
+ self.bulletField0.text = @"*";
+ self.bulletField1.text = @"*";
+ self.bulletField2.text = @"*";
+ self.bulletField3.text = nil;
+ break;
+ case 4:
+ self.bulletField0.text = @"*";
+ self.bulletField1.text = @"*";
+ self.bulletField2.text = @"*";
+ self.bulletField3.text = @"*";
+
+ [self.delegate passcodeController:self passcodeEntered:passcode];
+ textField.text = @"";
+ return NO;
+
+ break;
+ default:
+ break;
+ }
+
+ return YES;
+}
+
+@end
Oops, something went wrong.

0 comments on commit ee1088d

Please sign in to comment.