|
7 | 7 | //
|
8 | 8 |
|
9 | 9 | #import "TOPasscodeVariableInputField.h"
|
| 10 | +#import "TOPasscodeCircleImage.h" |
| 11 | + |
| 12 | +@interface TOPasscodeVariableInputField () |
| 13 | + |
| 14 | +@property (nonatomic, strong) UIImage *backgroundImage; // The outline image for this view |
| 15 | +@property (nonatomic, strong) UIImage *circleImage; // The circle image representing a single character |
| 16 | + |
| 17 | +@property (nonatomic, strong) NSMutableArray<UIImageView *> *circleViews; |
| 18 | + |
| 19 | +@end |
10 | 20 |
|
11 | 21 | @implementation TOPasscodeVariableInputField
|
12 | 22 |
|
| 23 | +#pragma mark - Class Creation - |
| 24 | + |
13 | 25 | - (instancetype)initWithFrame:(CGRect)frame
|
14 | 26 | {
|
15 | 27 | if (self = [super initWithFrame:frame]) {
|
16 |
| - _outlineThickness = 1.5f; |
17 |
| - _outlineCornerRadius = 15.0f; |
18 |
| - _circleDiameter = 13.0f; |
19 |
| - _circleSpacing = 10.0f; |
| 28 | + _outlineThickness = 1.0f; |
| 29 | + _outlineCornerRadius = 5.0f; |
| 30 | + _circleDiameter = 11.0f; |
| 31 | + _circleSpacing = 7.0f; |
20 | 32 | _outlinePadding = (CGSize){10,10};
|
21 |
| - _maximumVisibleLength = 10.0f; |
| 33 | + _maximumVisibleLength = 10; |
22 | 34 | }
|
23 | 35 |
|
24 | 36 | return self;
|
25 | 37 | }
|
26 | 38 |
|
| 39 | +#pragma mark - View Setup - |
| 40 | +- (void)setUpImageForCircleViews |
| 41 | +{ |
| 42 | + if (self.circleImage != nil) { return; } |
| 43 | + |
| 44 | + self.circleImage = [TOPasscodeCircleImage circleImageOfSize:_circleDiameter inset:0.0f padding:1.0f antialias:YES]; |
| 45 | + for (UIImageView *circleView in self.circleViews) { |
| 46 | + circleView.image = self.circleImage; |
| 47 | + [circleView sizeToFit]; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +- (void)setUpCircleViewsForLength:(NSInteger)length |
| 52 | +{ |
| 53 | + // Set up the number of circle views if needed |
| 54 | + if (self.circleViews.count == length) { return; } |
| 55 | + |
| 56 | + if (self.circleViews == nil) { |
| 57 | + self.circleViews = [NSMutableArray arrayWithCapacity:_maximumVisibleLength]; |
| 58 | + } |
| 59 | + |
| 60 | + // Reduce the number of views |
| 61 | + while (self.circleViews.count > length) { |
| 62 | + UIImageView *circleView = self.circleViews.lastObject; |
| 63 | + [circleView removeFromSuperview]; |
| 64 | + [self.circleViews removeLastObject]; |
| 65 | + } |
| 66 | + |
| 67 | + // Increase the number of views |
| 68 | + while (self.circleViews.count < length) { |
| 69 | + UIImageView *circleView = [[UIImageView alloc] initWithImage:self.circleImage]; |
| 70 | + circleView.alpha = 0.0f; |
| 71 | + [self addSubview:circleView]; |
| 72 | + [self.circleViews addObject:circleView]; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +- (void)setUpBackgroundImage |
| 77 | +{ |
| 78 | + if (self.backgroundImage != nil) { return; } |
| 79 | + |
| 80 | + self.backgroundImage = [[self class] backgroundImageWithThickness:_outlineThickness cornerRadius:_outlineCornerRadius]; |
| 81 | + self.image = self.backgroundImage; |
| 82 | +} |
| 83 | + |
| 84 | +#pragma mark - View Layout - |
| 85 | + |
| 86 | +- (void)sizeToFit |
| 87 | +{ |
| 88 | + CGRect frame = self.frame; |
| 89 | + |
| 90 | + // Calculate the width |
| 91 | + frame.size.width = self.outlineThickness * 2.0f; |
| 92 | + frame.size.width += (self.outlinePadding.width * 2.0f); |
| 93 | + frame.size.width += (self.maximumVisibleLength * (self.circleDiameter+2.0f)); // +2 for padding |
| 94 | + frame.size.width += (self.maximumVisibleLength - 1) * self.circleSpacing; |
| 95 | + |
| 96 | + // Height |
| 97 | + frame.size.height = self.outlineThickness * 2.0f; |
| 98 | + frame.size.height += self.outlinePadding.height * 2.0f; |
| 99 | + frame.size.height += self.circleDiameter; |
| 100 | + |
| 101 | + self.frame = frame; |
| 102 | +} |
| 103 | + |
| 104 | +- (void)layoutSubviews |
| 105 | +{ |
| 106 | + [super layoutSubviews]; |
| 107 | + |
| 108 | + // Genearate the background image if we don't have one yet |
| 109 | + [self setUpBackgroundImage]; |
| 110 | + |
| 111 | + // Set up the circle view image |
| 112 | + [self setUpImageForCircleViews]; |
| 113 | + |
| 114 | + // Set up the circle views |
| 115 | + [self setUpCircleViewsForLength:self.maximumVisibleLength]; |
| 116 | + |
| 117 | + // Layout the circle views for the current length |
| 118 | + CGRect frame = CGRectZero; |
| 119 | + frame.size = self.circleImage.size; |
| 120 | + frame.origin.y = CGRectGetMidY(self.bounds) - (frame.size.height * 0.5f); |
| 121 | + frame.origin.x = self.outlinePadding.width + self.outlineThickness; |
| 122 | + |
| 123 | + for (UIImageView *circleView in self.circleViews) { |
| 124 | + circleView.frame = frame; |
| 125 | + frame.origin.x += frame.size.width + self.circleSpacing; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +#pragma mark - Accessors - |
| 130 | + |
| 131 | +- (void)setOutlineThickness:(CGFloat)outlineThickness |
| 132 | +{ |
| 133 | + if (_outlineThickness == outlineThickness) { return; } |
| 134 | + _outlineThickness = outlineThickness; |
| 135 | + self.backgroundImage = nil; |
| 136 | + [self setNeedsLayout]; |
| 137 | +} |
| 138 | + |
| 139 | +- (void)setOutlineCornerRadius:(CGFloat)outlineCornerRadius |
| 140 | +{ |
| 141 | + if (_outlineCornerRadius == outlineCornerRadius) { return; } |
| 142 | + _outlineCornerRadius = outlineCornerRadius; |
| 143 | + self.backgroundImage = nil; |
| 144 | + [self setNeedsLayout]; |
| 145 | +} |
| 146 | + |
| 147 | +- (void)setCircleDiameter:(CGFloat)circleDiameter |
| 148 | +{ |
| 149 | + if (_circleDiameter == circleDiameter) { return; } |
| 150 | + _circleDiameter = circleDiameter; |
| 151 | + [self setUpImageForCircleViews]; |
| 152 | +} |
| 153 | + |
| 154 | +- (void)setLength:(NSInteger)length |
| 155 | +{ |
| 156 | + [self setLength:length animated:NO]; |
| 157 | +} |
| 158 | + |
| 159 | +- (void)setLength:(NSInteger)length animated:(BOOL)animated |
| 160 | +{ |
| 161 | + if (length == _length) { return; } |
| 162 | + |
| 163 | + void (^animationBlock)() = ^{ |
| 164 | + NSInteger i = 0; |
| 165 | + for (UIImageView *circleView in self.circleViews) { |
| 166 | + circleView.alpha = i < length ? 1.0f : 0.0f; |
| 167 | + } |
| 168 | + }; |
| 169 | + |
| 170 | + if (!animated) { |
| 171 | + animationBlock(); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + [UIView animateWithDuration:0.4f animations:animationBlock]; |
| 176 | +} |
| 177 | + |
| 178 | +#pragma mark - Image Creation - |
27 | 179 |
|
| 180 | ++ (UIImage *)backgroundImageWithThickness:(CGFloat)thickness cornerRadius:(CGFloat)radius |
| 181 | +{ |
| 182 | + CGFloat inset = thickness / 2.0f; |
| 183 | + CGFloat dimension = (radius * 2.0f) + 2.0f; |
| 184 | + |
| 185 | + CGRect frame = CGRectZero; |
| 186 | + frame.origin = CGPointMake(inset, inset); |
| 187 | + frame.size = CGSizeMake(dimension, dimension); |
| 188 | + |
| 189 | + CGSize canvasSize = frame.size; |
| 190 | + canvasSize.width += thickness; |
| 191 | + canvasSize.height += thickness; |
| 192 | + |
| 193 | + UIGraphicsBeginImageContextWithOptions(canvasSize, NO, 0.0f); |
| 194 | + { |
| 195 | + UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:frame cornerRadius:radius]; |
| 196 | + path.lineWidth = thickness; |
| 197 | + [[UIColor blackColor] setStroke]; |
| 198 | + [path stroke]; |
| 199 | + } |
| 200 | + |
| 201 | + UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); |
| 202 | + UIGraphicsEndImageContext(); |
| 203 | + |
| 204 | + UIEdgeInsets insets = UIEdgeInsetsMake(radius+1, radius+1, radius+1, radius+1); |
| 205 | + image = [image resizableImageWithCapInsets:insets]; |
| 206 | + return [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; |
| 207 | +} |
28 | 208 |
|
29 | 209 | @end
|
0 commit comments