|
| 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 |
0 commit comments