|
| 1 | +// |
| 2 | +// RFKeyboardToolbar.m |
| 3 | +// |
| 4 | +// Created by Rudd Fawcett on 12/3/13. |
| 5 | +// Copyright (c) 2013 Rudd Fawcett. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +#import "RFKeyboardToolbar.h" |
| 9 | + |
| 10 | +@interface RFKeyboardToolbar () |
| 11 | + |
| 12 | +/** |
| 13 | + * The toolbar view. |
| 14 | + */ |
| 15 | +@property (nonatomic,strong) UIView *toolbarView; |
| 16 | +/** |
| 17 | + * The scroll view that's faked to look like a toolbar. |
| 18 | + */ |
| 19 | +@property (nonatomic,strong) UIScrollView *scrollView; |
| 20 | +/** |
| 21 | + * The fake top border to replicate the toolbar. |
| 22 | + */ |
| 23 | +@property (nonatomic,strong) CALayer *topBorder; |
| 24 | + |
| 25 | +@end |
| 26 | + |
| 27 | +@implementation RFKeyboardToolbar |
| 28 | + |
| 29 | ++ (instancetype)toolbarWithButtons:(NSArray *)buttons { |
| 30 | + return [[RFKeyboardToolbar alloc] initWithButtons:buttons]; |
| 31 | +} |
| 32 | + |
| 33 | ++ (instancetype)toolbarViewWithButtons:(NSArray *)buttons { |
| 34 | + return [[RFKeyboardToolbar alloc] initWithButtons:buttons]; |
| 35 | +} |
| 36 | + |
| 37 | +- (id)initWithButtons:(NSArray *)buttons { |
| 38 | + self = [super initWithFrame:CGRectMake(0, 0, self.window.rootViewController.view.bounds.size.width, 40)]; |
| 39 | + if (self) { |
| 40 | + _buttons = [buttons copy]; |
| 41 | + |
| 42 | + self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| 43 | + [self addSubview:[self inputAccessoryView]]; |
| 44 | + } |
| 45 | + return self; |
| 46 | +} |
| 47 | + |
| 48 | +- (void)layoutSubviews { |
| 49 | + CGRect frame = _toolbarView.bounds; |
| 50 | + frame.size.height = 0.5f; |
| 51 | + |
| 52 | + _topBorder.frame = frame; |
| 53 | +} |
| 54 | + |
| 55 | +- (UIView *)inputAccessoryView { |
| 56 | + _toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)]; |
| 57 | + _toolbarView.backgroundColor = [UIColor colorWithWhite:0.973 alpha:1.0]; |
| 58 | + _toolbarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| 59 | + |
| 60 | + _topBorder = [CALayer layer]; |
| 61 | + _topBorder.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width, 0.5f); |
| 62 | + _topBorder.backgroundColor = [UIColor colorWithWhite:0.678 alpha:1.0].CGColor; |
| 63 | + |
| 64 | + [_toolbarView.layer addSublayer:_topBorder]; |
| 65 | + [_toolbarView addSubview:[self fakeToolbar]]; |
| 66 | + |
| 67 | + return _toolbarView; |
| 68 | +} |
| 69 | + |
| 70 | +- (UIScrollView *)fakeToolbar { |
| 71 | + _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)]; |
| 72 | + _scrollView.backgroundColor = [UIColor clearColor]; |
| 73 | + _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; |
| 74 | + _scrollView.showsHorizontalScrollIndicator = NO; |
| 75 | + _scrollView.contentInset = UIEdgeInsetsMake(6.0f, 0.0f, 8.0f, 6.0f); |
| 76 | + |
| 77 | + [self addButtons]; |
| 78 | + |
| 79 | + return _scrollView; |
| 80 | +} |
| 81 | + |
| 82 | +- (void)addButtons { |
| 83 | + NSUInteger index = 0; |
| 84 | + NSUInteger originX = 8; |
| 85 | + |
| 86 | + CGRect originFrame; |
| 87 | + |
| 88 | + for (RFToolbarButton *eachButton in _buttons) { |
| 89 | + originFrame = CGRectMake(originX, 0, eachButton.frame.size.width, eachButton.frame.size.height); |
| 90 | + eachButton.frame = originFrame; |
| 91 | + |
| 92 | + [_scrollView addSubview:eachButton]; |
| 93 | + |
| 94 | + originX = originX + eachButton.bounds.size.width + 8; |
| 95 | + index++; |
| 96 | + } |
| 97 | + |
| 98 | + CGSize contentSize = _scrollView.contentSize; |
| 99 | + contentSize.width = originX - 8; |
| 100 | + _scrollView.contentSize = contentSize; |
| 101 | +} |
| 102 | + |
| 103 | +- (void)setButtons:(NSArray *)buttons { |
| 104 | + [_buttons makeObjectsPerformSelector:@selector(removeFromSuperview)]; |
| 105 | + _buttons = [buttons copy]; |
| 106 | + [self addButtons]; |
| 107 | +} |
| 108 | + |
| 109 | +- (void)setButtons:(NSArray *)buttons animated:(BOOL)animated { |
| 110 | + if (!animated) { |
| 111 | + self.buttons = buttons; |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + NSMutableSet *removeButtons = [NSMutableSet setWithArray:_buttons]; |
| 116 | + [removeButtons minusSet:[NSSet setWithArray:buttons]]; |
| 117 | + NSMutableSet *addButtons = [NSMutableSet setWithArray:buttons]; |
| 118 | + [addButtons minusSet:[NSSet setWithArray:_buttons]]; |
| 119 | + _buttons = [buttons copy]; |
| 120 | + |
| 121 | + // calculate end frames |
| 122 | + NSUInteger originX = 8; |
| 123 | + NSUInteger index = 0; |
| 124 | + NSMutableArray *buttonFrames = [NSMutableArray arrayWithCapacity:_buttons.count]; |
| 125 | + |
| 126 | + for (RFToolbarButton *button in _buttons) { |
| 127 | + CGRect frame = CGRectMake(originX, 0, button.frame.size.width, button.frame.size.height); |
| 128 | + [buttonFrames addObject:[NSValue valueWithCGRect:frame]]; |
| 129 | + |
| 130 | + originX += button.bounds.size.width + 8; |
| 131 | + index++; |
| 132 | + } |
| 133 | + |
| 134 | + CGSize contentSize = _scrollView.contentSize; |
| 135 | + contentSize.width = originX - 8; |
| 136 | + if (contentSize.width > _scrollView.contentSize.width) { |
| 137 | + _scrollView.contentSize = contentSize; |
| 138 | + } |
| 139 | + |
| 140 | + // make added buttons appear from the right |
| 141 | + [addButtons enumerateObjectsUsingBlock:^(RFToolbarButton *button, BOOL *stop) { |
| 142 | + button.frame = CGRectMake(originX, 0, button.frame.size.width, button.frame.size.height); |
| 143 | + [_scrollView addSubview:button]; |
| 144 | + }]; |
| 145 | + |
| 146 | + // animate |
| 147 | + [UIView animateWithDuration:0.2 animations:^{ |
| 148 | + [removeButtons enumerateObjectsUsingBlock:^(RFToolbarButton *button, BOOL *stop) { |
| 149 | + button.alpha = 0; |
| 150 | + }]; |
| 151 | + |
| 152 | + [_buttons enumerateObjectsUsingBlock:^(RFToolbarButton *button, NSUInteger idx, BOOL *stop) { |
| 153 | + button.frame = [buttonFrames[idx] CGRectValue]; |
| 154 | + }]; |
| 155 | + |
| 156 | + _scrollView.contentSize = contentSize; |
| 157 | + } completion:^(BOOL finished) { |
| 158 | + [removeButtons makeObjectsPerformSelector:@selector(removeFromSuperview)]; |
| 159 | + }]; |
| 160 | +} |
| 161 | + |
| 162 | +@end |
0 commit comments