Skip to content

Commit 3503928

Browse files
committed
Broke up input view content into two subviews
1 parent 5886557 commit 3503928

File tree

10 files changed

+332
-71
lines changed

10 files changed

+332
-71
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// TOPasscodeFixedInputField.h
3+
// TOPasscodeViewControllerExample
4+
//
5+
// Created by Tim Oliver on 7/6/17.
6+
// Copyright © 2017 Timothy Oliver. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@class TOPasscodeCircleView;
12+
13+
@interface TOPasscodeFixedInputView : UIView
14+
15+
/* The size of each circle in this view (Default is 16) */
16+
@property (nonatomic, assign) CGFloat circleDiameter;
17+
18+
/* The spacing between each circle (Default is 25.0f) */
19+
@property (nonatomic, assign) CGFloat circleSpacing;
20+
21+
/* The number of circles in this view (Default is 4) */
22+
@property (nonatomic, assign) NSInteger length;
23+
24+
/* The number of highlighted circles */
25+
@property (nonatomic, assign) NSInteger highlightedLength;
26+
27+
/* The circle views managed by this view */
28+
@property (nonatomic, strong, readonly) NSArray<TOPasscodeCircleView *> *circleViews;
29+
30+
/* Init with a set number of circles */
31+
- (instancetype)initWithLength:(NSInteger)length;
32+
33+
/* Set the number of highlighted circles */
34+
- (void)setHighlightedLength:(NSInteger)highlightedLength animated:(BOOL)animated;
35+
36+
@end
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//
2+
// TOPasscodeFixedInputField.m
3+
// TOPasscodeViewControllerExample
4+
//
5+
// Created by Tim Oliver on 7/6/17.
6+
// Copyright © 2017 Timothy Oliver. All rights reserved.
7+
//
8+
9+
#import "TOPasscodeFixedInputView.h"
10+
#import "TOPasscodeCircleView.h"
11+
#import "TOPasscodeCircleImage.h"
12+
13+
@interface TOPasscodeFixedInputView ()
14+
15+
@property (nonatomic, strong, readwrite) NSArray<TOPasscodeCircleView *> *circleViews;
16+
@property (nonatomic, strong) UIImage *circleImage;
17+
@property (nonatomic, strong) UIImage *highlightedCircleImage;
18+
19+
@end
20+
21+
@implementation TOPasscodeFixedInputView
22+
23+
#pragma mark - Object Creation -
24+
25+
- (instancetype)initWithLength:(NSInteger)length
26+
{
27+
if (self = [self initWithFrame:CGRectZero]) {
28+
_length = length;
29+
}
30+
31+
return self;
32+
}
33+
34+
- (instancetype)initWithFrame:(CGRect)frame
35+
{
36+
if (self = [super initWithFrame:frame]) {
37+
_circleSpacing = 25.0f;
38+
_circleDiameter = 16.0f;
39+
_length = 4;
40+
}
41+
42+
return self;
43+
}
44+
45+
#pragma mark - View Configuration -
46+
47+
- (void)sizeToFit
48+
{
49+
// Resize the view to encompass the circles
50+
CGRect frame = self.frame;
51+
frame.size.width = (_circleDiameter * _length) + (_circleSpacing * (_length - 1)) + 2.0f;
52+
frame.size.height = _circleDiameter + 2.0f;
53+
self.frame = frame;
54+
}
55+
56+
- (void)layoutSubviews
57+
{
58+
CGRect frame = CGRectZero;
59+
frame.size = (CGSize){self.circleDiameter + 2.0f, self.circleDiameter + 2.0f};
60+
61+
for (TOPasscodeCircleView *circleView in self.circleViews) {
62+
circleView.frame = frame;
63+
frame.origin.x += self.circleDiameter + self.circleSpacing;
64+
}
65+
}
66+
67+
#pragma mark - State Configuration -
68+
69+
- (void)setHighlightedLength:(NSInteger)highlightedLength animated:(BOOL)animated
70+
{
71+
NSInteger i = 0;
72+
for (TOPasscodeCircleView *circleView in self.circleViews) {
73+
[circleView setHighlighted:(i < highlightedLength) animated:animated];
74+
i++;
75+
}
76+
}
77+
78+
#pragma mark - Circle View Configuration -
79+
80+
- (void)setCircleViewsForLength:(NSInteger)length
81+
{
82+
NSMutableArray *circleViews = [NSMutableArray array];
83+
if (self.circleViews) {
84+
[circleViews addObjectsFromArray:self.circleViews];
85+
}
86+
87+
while (circleViews.count != length) {
88+
// Remove any extra circle views
89+
if (circleViews.count > length) {
90+
TOPasscodeCircleView *lastCircle = circleViews.lastObject;
91+
[lastCircle removeFromSuperview];
92+
[circleViews removeLastObject];
93+
continue;
94+
}
95+
96+
// Add any new circle views
97+
TOPasscodeCircleView *newCircleView = [[TOPasscodeCircleView alloc] init];
98+
[self setImagesOfCircleView:newCircleView];
99+
[self addSubview:newCircleView];
100+
[circleViews addObject:newCircleView];
101+
}
102+
103+
self.circleViews = [NSArray arrayWithArray:circleViews];
104+
}
105+
106+
- (void)setCircleImagesForDiameter:(CGFloat)diameter
107+
{
108+
self.circleImage = [TOPasscodeCircleImage hollowCircleImageOfSize:diameter strokeWidth:1.0f padding:1.0f];
109+
self.highlightedCircleImage = [TOPasscodeCircleImage circleImageOfSize:diameter inset:0.5f padding:1.0f];
110+
111+
for (TOPasscodeCircleView *circleView in self.circleViews) {
112+
[self setImagesOfCircleView:circleView];
113+
}
114+
}
115+
116+
- (void)setImagesOfCircleView:(TOPasscodeCircleView *)circleView
117+
{
118+
circleView.circleImage = self.circleImage;
119+
circleView.highlightedCircleImage = self.highlightedCircleImage;
120+
}
121+
122+
#pragma mark - Accessors -
123+
124+
- (NSArray<TOPasscodeCircleView *> *)circleViews
125+
{
126+
if (_circleViews) { return _circleViews; }
127+
[self setCircleViewsForLength:self.length];
128+
[self setCircleImagesForDiameter:self.circleDiameter];
129+
return _circleViews;
130+
}
131+
132+
- (void)setCircleDiameter:(CGFloat)circleDiameter
133+
{
134+
if (circleDiameter == _circleDiameter) { return; }
135+
_circleDiameter = circleDiameter;
136+
[self setCircleImagesForDiameter:_circleDiameter];
137+
[self sizeToFit];
138+
}
139+
140+
- (void)setHighlightedLength:(NSInteger)highlightedLength
141+
{
142+
[self setHighlightedLength:highlightedLength animated:NO];
143+
}
144+
145+
@end

TOPasscodeViewController/Views/TOPasscodeNumberInputView.h renamed to TOPasscodeViewController/Views/TOPasscodeInputField.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,39 @@
1010

1111
NS_ASSUME_NONNULL_BEGIN
1212

13-
@interface TOPasscodeNumberInputView : UIVisualEffectView <UIKeyInput>
13+
typedef NS_ENUM(NSInteger, TOPasscodeInputFieldStyle) {
14+
TOPasscodeInputFieldStyleFixed, // The passcode explicitly requires a specific number of characters
15+
TOPasscodeInputFieldStyleVariable // The passcode can be any arbitrary numher of characters
16+
};
17+
18+
@interface TOPasscodeInputField : UIVisualEffectView <UIKeyInput>
19+
20+
/* The input style of this control */
21+
@property (nonatomic, assign) TOPasscodeInputFieldStyle style;
1422

1523
/* The size of each circle in this view (Default is 16) */
16-
@property (nonatomic, assign) CGFloat circleDiameter;
24+
@property (nonatomic, assign) CGFloat fixedCircleDiameter;
1725

1826
/* The spacing between each circle (Default is 25.0f) */
19-
@property (nonatomic, assign) CGFloat circleSpacing;
27+
@property (nonatomic, assign) CGFloat fixedCircleSpacing;
2028

2129
/* The number of circles in this view (Default is 4) */
22-
@property (nonatomic, assign) NSInteger requiredLength;
30+
@property (nonatomic, assign) NSInteger fixedLength;
2331

2432
/* The current passcode entered into this view */
2533
@property (nonatomic, copy, nullable) NSString *passcode;
2634

2735
/* If this view is directly receiving input, this can change the `UIKeyboard` appearance. */
2836
@property (nonatomic, assign) UIKeyboardAppearance keyboardAppearance;
2937

30-
/* The alpha value of the circle views in this view */
31-
@property (nonatomic, assign) CGFloat circleAlpha;
38+
/* The alpha value of the views in this view (For tranclucent styling) */
39+
@property (nonatomic, assign) CGFloat contentAlpha;
3240

3341
/** Called when the number of digits has been entered */
3442
@property (nonatomic, copy) void (^passcodeCompletedHandler)(NSString *code);
3543

3644
/* Init with the target length needed for this passcode */
37-
- (instancetype)initWithRequiredLength:(NSInteger)length;
45+
- (instancetype)initWithStyle:(TOPasscodeInputFieldStyle)style;
3846

3947
/* Replace the passcode with this one, and animate the transition */
4048
- (void)setPasscode:(nullable NSString *)passcode animated:(BOOL)animated;

0 commit comments

Comments
 (0)