Skip to content

Commit aeeb477

Browse files
committed
Adding more logig for horizontal rotation
1 parent 2c81fc3 commit aeeb477

File tree

7 files changed

+83
-38
lines changed

7 files changed

+83
-38
lines changed

TOPasscodeViewController/Models/TOPasscodeViewContentLayout.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414
/* The width of the PIN view in which this layout object is sizing the content to fit. */
1515
@property (nonatomic, assign) CGFloat viewWidth;
1616

17-
/* The title View at the very top */
17+
/* The title view at the very top */
1818
@property (nonatomic, assign) CGFloat titleViewBottomSpacing; // Space from the bottom of the title view to the title label
1919

20-
/* The Title Label Explaining the PIN View */
21-
@property (nonatomic, assign) CGFloat titleLabelBottomSpacing; // Space from the title label to the circles row
22-
@property (nonatomic, strong) UIFont *titleLabelFont; // The font of the title label
20+
/* The Title Label Explaining the Passcode View */
21+
@property (nonatomic, assign) CGFloat titleLabelBottomSpacing; // Space from the title label to the input view
22+
@property (nonatomic, strong) UIFont *titleLabelFont; // The font of the title label
23+
24+
/* Title Label properties when the view is laid out horizontally */
25+
@property (nonatomic, assign) CGFloat titleHorizontalLayoutWidth; // When laid out horizontally, the width of the title view
26+
@property (nonatomic, assign) CGFloat titleHorizontalLayoutSpacing; // The amount of spacing between the title label and the passcode keypad
2327

2428
/* Circle Row Configuration */
2529
@property (nonatomic, assign) CGFloat circleRowDiameter; // The diameter of each circle representing a PIN number
@@ -51,6 +55,6 @@
5155
/* Default layout configurations for the various sizes */
5256
+ (TOPasscodeViewContentLayout *)defaultScreenContentLayout; /* Default layout values. Designed for iPhone 6 Plus and above. */
5357
+ (TOPasscodeViewContentLayout *)mediumScreenContentLayout; /* For medium screen sizes, like iPhone 6, or 1/4 view on iPad Pro. */
54-
+ (TOPasscodeViewContentLayout *)smallScreenContentLayout; /* For the smallest screens, like iPhone SE, and 1/4 on standard size iPads/ */
58+
+ (TOPasscodeViewContentLayout *)smallScreenContentLayout; /* For the smallest screens, like iPhone SE, and 1/4 on standard size iPads/ */
5559

5660
@end

TOPasscodeViewController/Models/TOPasscodeViewContentLayout.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ + (TOPasscodeViewContentLayout *)defaultScreenContentLayout
2424
contentLayout.titleLabelBottomSpacing = 34.0f;
2525
contentLayout.titleLabelFont = [UIFont systemFontOfSize: 22.0f];
2626

27+
/* Horizontal title constraints */
28+
contentLayout.titleHorizontalLayoutWidth = 185.0f;
29+
contentLayout.titleHorizontalLayoutSpacing = 16.0f;
30+
2731
/* Circle Row Configuration */
2832
contentLayout.circleRowDiameter = 15.5f;
2933
contentLayout.circleRowSpacing = 30.0f;
@@ -68,6 +72,10 @@ + (TOPasscodeViewContentLayout *)mediumScreenContentLayout
6872
contentLayout.titleLabelBottomSpacing = 27.0f;
6973
contentLayout.titleLabelFont = [UIFont systemFontOfSize: 20.0f];
7074

75+
/* Horizontal title constraints */
76+
contentLayout.titleHorizontalLayoutWidth = 185.0f;
77+
contentLayout.titleHorizontalLayoutSpacing = 16.0f;
78+
7179
/* Circle Row Configuration */
7280
contentLayout.circleRowDiameter = 12.5f;
7381
contentLayout.circleRowSpacing = 26.0f;
@@ -110,6 +118,10 @@ + (TOPasscodeViewContentLayout *)smallScreenContentLayout
110118
contentLayout.titleLabelBottomSpacing = 23.0f;
111119
contentLayout.titleLabelFont = [UIFont systemFontOfSize: 17.0f];
112120

121+
/* Horizontal title constraints */
122+
contentLayout.titleHorizontalLayoutWidth = 185.0f;
123+
contentLayout.titleHorizontalLayoutSpacing = 16.0f;
124+
113125
/* Circle Row Configuration */
114126
contentLayout.circleRowDiameter = 12.5f;
115127
contentLayout.circleRowSpacing = 22.0f;

TOPasscodeViewController/TOPasscodeViewController.m

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,21 @@ - (void)viewDidAppear:(BOOL)animated
202202
- (void)viewDidLayoutSubviews
203203
{
204204
CGSize bounds = self.view.bounds.size;
205+
CGFloat width = bounds.width;
206+
207+
// If on an iPhone (and we're potentially rotated), work out the minimum width we can be
208+
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
209+
width = MIN(bounds.width, bounds.height);
210+
}
205211

206212
// Update the accessory button sizes
207-
[self updateAccessoryButtonFontsForWidth:bounds.width];
213+
[self updateAccessoryButtonFontsForWidth:width];
208214

209215
// Re-layout the accessory buttons
210-
[self layoutAccessoryButtonsForWidth:bounds.width];
216+
[self layoutAccessoryButtonsForWidth:width];
211217

212218
// Resize the pin view to scale to the new size
213-
[self.passcodeView sizeToFitWidth:bounds.width];
219+
[self.passcodeView sizeToFitWidth:width];
214220

215221
// Re-center the pin view
216222
CGRect frame = self.passcodeView.frame;
@@ -230,7 +236,7 @@ - (void)viewWillAppear:(BOOL)animated
230236
[self.view layoutIfNeeded];
231237
}];
232238

233-
// Show the keyboard if we're
239+
// Show the keyboard if we're entering alphanumeric characters
234240
if (self.passcodeType == TOPasscodeTypeCustomAlphanumeric) {
235241
[self.passcodeView.inputField becomeFirstResponder];
236242
}
@@ -251,6 +257,22 @@ - (UIStatusBarStyle)preferredStatusBarStyle
251257
return TOPasscodeViewStyleIsDark(self.style) ? UIStatusBarStyleLightContent : UIStatusBarStyleDefault;
252258
}
253259

260+
#pragma mark - View Rotations -
261+
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
262+
{
263+
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
264+
265+
// We don't need to do anything special on iPad
266+
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { return; }
267+
268+
// Work out if we need to transition to horizontal
269+
BOOL horizontalLayout = size.height < size.width;
270+
271+
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
272+
[self.passcodeView setHorizontalLayout:horizontalLayout animated:YES duration:context.transitionDuration];
273+
} completion:nil];
274+
}
275+
254276
#pragma mark - View Styling -
255277
- (void)applyThemeForStyle:(TOPasscodeViewStyle)style
256278
{

TOPasscodeViewController/Views/Main/TOPasscodeKeypadView.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,10 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
@class TOPasscodeCircleButton;
1414

15-
/* The direction this keypad is laid out.
16-
When horizontal, the '0' button is on the right side */
17-
typedef NS_ENUM(NSInteger, TOPasscodeKeypadLayout) {
18-
TOPasscodeKeypadLayoutVertical,
19-
TOPasscodeKeypadLayoutHorizontal
20-
};
21-
2215
@interface TOPasscodeKeypadView : UIView
2316

2417
/** The type of layout for the buttons (Default is vertical) */
25-
@property (nonatomic, assign) TOPasscodeKeypadLayout layout;
18+
@property (nonatomic, assign) BOOL horizontalLayout;
2619

2720
/** The vibrancy effect to be applied to each button background */
2821
@property (nonatomic, strong, nullable) UIVibrancyEffect *vibrancyEffect;
@@ -77,7 +70,7 @@ typedef NS_ENUM(NSInteger, TOPasscodeKeypadLayout) {
7770
@property (nonatomic, copy) void (^buttonTappedHandler)(NSInteger buttonNumber);
7871

7972
/* Perform an animation of a set duration to the new layout */
80-
- (void)setLayout:(TOPasscodeKeypadLayout)layout animated:(BOOL)animated duration:(CGFloat)duration;
73+
- (void)setHorizontalLayout:(BOOL)horizontalLayout animated:(BOOL)animated duration:(CGFloat)duration;
8174

8275
@end
8376

TOPasscodeViewController/Views/Main/TOPasscodeKeypadView.m

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ - (void)setUpButtons
9898
self.verticalZeroButton = circleButton;
9999

100100
// Hide the button if it's not vertically laid out
101-
if (self.layout != TOPasscodeKeypadLayoutVertical) {
101+
if (self.horizontalLayout) {
102102
self.verticalZeroButton.contentAlpha = 0.0f;
103103
self.verticalZeroButton.hidden = YES;
104104
}
@@ -107,7 +107,7 @@ - (void)setUpButtons
107107
self.horizontalZeroButton = circleButton;
108108

109109
// Hide the button if it's not horizontally laid out
110-
if (self.layout != TOPasscodeKeypadLayoutHorizontal) {
110+
if (!self.horizontalLayout) {
111111
self.horizontalZeroButton.contentAlpha = 0.0f;
112112
self.horizontalZeroButton.hidden = YES;
113113
}
@@ -122,7 +122,7 @@ - (void)sizeToFit
122122
CGFloat padding = 2.0f;
123123

124124
CGRect frame = self.frame;
125-
if (self.layout == TOPasscodeKeypadLayoutHorizontal) {
125+
if (self.horizontalLayout) {
126126
frame.size.width = ((self.buttonDiameter + padding) * 4) + (self.buttonSpacing.width * 3);
127127
frame.size.height = ((self.buttonDiameter + padding) * 3) + (self.buttonSpacing.height * 2);
128128
}
@@ -228,20 +228,18 @@ - (UIImage *)tappedButtonImage
228228

229229
#pragma mark - Public Layout Setters -
230230

231-
- (void)setLayout:(TOPasscodeKeypadLayout)layout
231+
- (void)setHorizontalLayout:(BOOL)horizontalLayout
232232
{
233-
[self setLayout:layout animated:NO duration:0.0f];
233+
[self setHorizontalLayout:horizontalLayout animated:NO duration:0.0f];
234234
}
235235

236-
- (void)setLayout:(TOPasscodeKeypadLayout)layout animated:(BOOL)animated duration:(CGFloat)duration
236+
- (void)setHorizontalLayout:(BOOL)horizontalLayout animated:(BOOL)animated duration:(CGFloat)duration
237237
{
238-
if (layout == _layout) {
238+
if (horizontalLayout== _horizontalLayout) {
239239
return;
240240
}
241241

242-
_layout = layout;
243-
244-
BOOL toHorizontal = (layout == TOPasscodeKeypadLayoutHorizontal);
242+
_horizontalLayout = horizontalLayout;
245243

246244
// Resize itself now so the frame value is up to date externally
247245
[self sizeToFit];
@@ -250,17 +248,17 @@ - (void)setLayout:(TOPasscodeKeypadLayout)layout animated:(BOOL)animated duratio
250248
self.verticalZeroButton.hidden = NO;
251249
self.horizontalZeroButton.hidden = NO;
252250

253-
self.verticalZeroButton.contentAlpha = toHorizontal ? 1.0f : 0.0f;
254-
self.horizontalZeroButton.contentAlpha = toHorizontal ? 0.0f : 1.0f;
251+
self.verticalZeroButton.contentAlpha = _horizontalLayout ? 1.0f : 0.0f;
252+
self.horizontalZeroButton.contentAlpha = _horizontalLayout ? 0.0f : 1.0f;
255253

256254
void (^animationBlock)() = ^{
257-
self.verticalZeroButton.contentAlpha = toHorizontal ? 0.0f : 1.0f;
258-
self.horizontalZeroButton.contentAlpha = toHorizontal ? 1.0f : 0.0f;
255+
self.verticalZeroButton.contentAlpha = _horizontalLayout ? 0.0f : 1.0f;
256+
self.horizontalZeroButton.contentAlpha = _horizontalLayout ? 1.0f : 0.0f;
259257
};
260258

261259
void (^completionBlock)(BOOL) = ^(BOOL complete) {
262-
self.verticalZeroButton.hidden = toHorizontal;
263-
self.horizontalZeroButton.hidden = !toHorizontal;
260+
self.verticalZeroButton.hidden = _horizontalLayout;
261+
self.horizontalZeroButton.hidden = !_horizontalLayout;
264262
};
265263

266264
// Don't animate if not needed
@@ -394,8 +392,8 @@ - (void)setContentAlpha:(CGFloat)contentAlpha
394392

395393
for (TOPasscodeCircleButton *button in self.keypadButtons) {
396394
// Skip whichever '0' button is not presently being used
397-
if ((self.layout == TOPasscodeKeypadLayoutHorizontal && button == self.verticalZeroButton) ||
398-
(self.layout == TOPasscodeKeypadLayoutVertical && button == self.horizontalZeroButton))
395+
if ((self.horizontalLayout && button == self.verticalZeroButton) ||
396+
(!self.horizontalLayout && button == self.horizontalZeroButton))
399397
{
400398
continue;
401399
}

TOPasscodeViewController/Views/Main/TOPasscodeView.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ NS_ASSUME_NONNULL_BEGIN
2424
/* The type of passcode being managed by it */
2525
@property (nonatomic, readonly) TOPasscodeType passcodeType;
2626

27+
/* Whether the content is laid out vertically or horizontally (iPhone only) */
28+
@property (nonatomic, assign) BOOL horizontalLayout;
29+
2730
/* The text in the title view (Default is 'Enter Passcode') */
2831
@property (nonatomic, copy) NSString *titleText;
2932

@@ -78,6 +81,9 @@ NS_ASSUME_NONNULL_BEGIN
7881
/* Delete the last character from the passcode */
7982
- (void)deleteLastPasscodeCharacterAnimated:(BOOL)animated;
8083

84+
/* Animate the transition between horizontal and vertical layouts */
85+
- (void)setHorizontalLayout:(BOOL)horizontalLayout animated:(BOOL)animated duration:(CGFloat)duration;
86+
8187
@end
8288

8389
NS_ASSUME_NONNULL_END

TOPasscodeViewController/Views/Main/TOPasscodeView.m

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,6 @@ - (void)setUpViewForType:(TOPasscodeType)type
270270
}
271271
};
272272
[self addSubview:self.keypadView];
273-
274-
self.keypadView.layout = TOPasscodeKeypadLayoutHorizontal;
275273
}
276274
else {
277275
[self.keypadView removeFromSuperview];
@@ -402,6 +400,18 @@ - (UIBlurEffect *)blurEffectForStyle:(TOPasscodeViewStyle)style
402400
}
403401

404402
#pragma mark - Accessors -
403+
- (void)setHorizontalLayout:(BOOL)horizontalLayout
404+
{
405+
[self setHorizontalLayout:horizontalLayout animated:NO duration:0.0f];
406+
}
407+
408+
- (void)setHorizontalLayout:(BOOL)horizontalLayout animated:(BOOL)animated duration:(CGFloat)duration
409+
{
410+
if (horizontalLayout == _horizontalLayout) { return; }
411+
_horizontalLayout = horizontalLayout;
412+
[self.keypadView setHorizontalLayout:horizontalLayout animated:animated duration:duration];
413+
}
414+
405415
- (void)setDefaultContentLayout:(TOPasscodeViewContentLayout *)defaultContentLayout
406416
{
407417
if (defaultContentLayout == _defaultContentLayout) { return; }

0 commit comments

Comments
 (0)