Skip to content

Commit 9f6ba6e

Browse files
committed
Added vertical layout to input view
1 parent 152c9da commit 9f6ba6e

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

TOPasscodeViewController/Views/Main/TOPasscodeView.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ - (void)setHorizontalLayout:(BOOL)horizontalLayout animated:(BOOL)animated durat
501501
if (horizontalLayout == _horizontalLayout) { return; }
502502
_horizontalLayout = horizontalLayout;
503503
[self.keypadView setHorizontalLayout:horizontalLayout animated:animated duration:duration];
504+
[self.inputField setHorizontalLayout:horizontalLayout animated:animated duration:duration];
504505
}
505506

506507
- (void)setDefaultContentLayout:(TOPasscodeViewContentLayout *)defaultContentLayout

TOPasscodeViewController/Views/Shared/TOPasscodeInputField.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ typedef NS_ENUM(NSInteger, TOPasscodeInputFieldStyle) {
3939
/* The amount of spacing between the 'OK' button and the passcode field */
4040
@property (nonatomic, assign) CGFloat submitButtonSpacing;
4141

42+
/* The amount of spacing between the 'OK' button and the passcode field */
43+
@property (nonatomic, assign) CGFloat submitButtonVerticalSpacing;
44+
4245
/* The font size of the submit button */
4346
@property (nonatomic, assign) CGFloat submitButtonFontSize;
4447

@@ -60,6 +63,9 @@ typedef NS_ENUM(NSInteger, TOPasscodeInputFieldStyle) {
6063
/** Called when the number of digits has been entered, or the user tapped 'Done' on the keyboard */
6164
@property (nonatomic, copy) void (^passcodeCompletedHandler)(NSString *code);
6265

66+
/** Horizontal layout. The 'OK' button will be placed under the text field */
67+
@property (nonatomic, assign) BOOL horizontalLayout;
68+
6369
/* Init with the target length needed for this passcode */
6470
- (instancetype)initWithStyle:(TOPasscodeInputFieldStyle)style;
6571

@@ -75,6 +81,9 @@ typedef NS_ENUM(NSInteger, TOPasscodeInputFieldStyle) {
7581
/* Plays a shaking animation and resets the passcode back to empty */
7682
- (void)resetPasscodeAnimated:(BOOL)animated playImpact:(BOOL)impact;
7783

84+
/* Animates the OK button changing location */
85+
- (void)setHorizontalLayout:(BOOL)horizontalLayout animated:(BOOL)animated duration:(CGFloat)duration;
86+
7887
@end
7988

8089
NS_ASSUME_NONNULL_END

TOPasscodeViewController/Views/Shared/TOPasscodeInputField.m

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ - (instancetype)initWithStyle:(TOPasscodeInputFieldStyle)style
5252

5353
- (void)setUp
5454
{
55+
self.backgroundColor = [UIColor clearColor];
5556
_submitButtonSpacing = 4.0f;
57+
_submitButtonVerticalSpacing = 5.0f;
5658
}
5759

5860
- (void)setUpForStyle:(TOPasscodeInputFieldStyle)style
@@ -86,6 +88,9 @@ - (void)sizeToFit
8688
CGRect frame = self.frame;
8789
[self.inputField sizeToFit];
8890
frame.size = self.inputField.frame.size;
91+
if (self.horizontalLayout) {
92+
frame.size.height += self.submitButtonVerticalSpacing + CGRectGetHeight(self.submitButton.frame);
93+
}
8994
self.frame = CGRectIntegral(frame);
9095
}
9196

@@ -97,8 +102,14 @@ - (void)layoutSubviews
97102

98103
[self.submitButton sizeToFit];
99104
CGRect frame = self.submitButton.frame;
100-
frame.origin.x = CGRectGetMaxX(self.bounds) + self.submitButtonSpacing;
101-
frame.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(frame)) * 0.5f;
105+
if (!self.horizontalLayout) {
106+
frame.origin.x = CGRectGetMaxX(self.bounds) + self.submitButtonSpacing;
107+
frame.origin.y = (CGRectGetHeight(self.bounds) - CGRectGetHeight(frame)) * 0.5f;
108+
}
109+
else {
110+
frame.origin.x = (CGRectGetWidth(self.frame) - frame.size.width) * 0.5f;
111+
frame.origin.y = CGRectGetMaxY(self.inputField.frame) + self.submitButtonVerticalSpacing;
112+
}
102113
self.submitButton.frame = CGRectIntegral(frame);
103114
}
104115

@@ -124,6 +135,7 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
124135
{
125136
CGRect frame = self.bounds;
126137
frame.size.width += self.submitButton.frame.size.width + (self.submitButtonSpacing * 2.0f);
138+
frame.size.height += self.submitButtonVerticalSpacing;
127139

128140
if (CGRectContainsPoint(frame, point)) {
129141
return YES;
@@ -340,4 +352,48 @@ - (void)setContentAlpha:(CGFloat)contentAlpha
340352
self.submitButton.alpha = contentAlpha;
341353
}
342354

355+
- (void)setHorizontalLayout:(BOOL)horizontalLayout
356+
{
357+
[self setHorizontalLayout:horizontalLayout animated:NO duration:0.0f];
358+
}
359+
360+
- (void)setHorizontalLayout:(BOOL)horizontalLayout animated:(BOOL)animated duration:(CGFloat)duration
361+
{
362+
if (_horizontalLayout == horizontalLayout) {
363+
return;
364+
}
365+
366+
UIView *snapshotView = nil;
367+
368+
if (self.submitButton.hidden == NO && animated) {
369+
snapshotView = [self.submitButton snapshotViewAfterScreenUpdates:NO];
370+
snapshotView.frame = self.submitButton.frame;
371+
[self addSubview:snapshotView];
372+
}
373+
374+
_horizontalLayout = horizontalLayout;
375+
376+
if (!animated) {
377+
[self sizeToFit];
378+
[self setNeedsLayout];
379+
return;
380+
}
381+
382+
self.submitButton.alpha = 0.0f;
383+
[self setNeedsLayout];
384+
[self layoutIfNeeded];
385+
386+
id animationBlock = ^{
387+
self.submitButton.alpha = 1.0f;
388+
snapshotView.alpha = 0.0f;
389+
};
390+
391+
id completionBlock = ^(BOOL complete) {
392+
[snapshotView removeFromSuperview];
393+
[self bringSubviewToFront:self.submitButton];
394+
};
395+
396+
[UIView animateWithDuration:duration animations:animationBlock completion:completionBlock];
397+
}
398+
343399
@end

0 commit comments

Comments
 (0)