-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraToolBar.m
171 lines (81 loc) · 5.5 KB
/
CameraToolBar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// CameraToolBar.m
// Blocstagram_V3
//
// Created by Diego Aguirre on 6/16/15.
// Copyright (c) 2015 Diego Aguirre. All rights reserved.
//
#import "CameraToolBar.h"
@interface CameraToolBar ()
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *cameraButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UIView *whiteView;
@property (nonatomic, strong) UIView *purpleView;
@end
@implementation CameraToolBar
- (instancetype) initWithImageNames:(NSArray *)imageNames{
self = [super init];
if (self) {
self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.cameraButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.leftButton addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.cameraButton addTarget:self action:@selector(cameraButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.rightButton addTarget:self action:@selector(rightButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.leftButton setImage:[UIImage imageNamed:imageNames.firstObject] forState:UIControlStateNormal];
[self.rightButton setImage:[UIImage imageNamed:imageNames.lastObject] forState:UIControlStateNormal];
[self.cameraButton setImage:[UIImage imageNamed:@"camera"] forState:UIControlStateNormal];
[self.cameraButton setContentEdgeInsets:UIEdgeInsetsMake(10, 10, 15, 10)];
self.whiteView = [UIView new];
self.whiteView.backgroundColor = [UIColor whiteColor];
self.purpleView = [UIView new];
self.purpleView.backgroundColor = [UIColor colorWithRed:0.345 green:0.318 blue:0.424 alpha:1]; /*#58516c*/
for (UIView *view in @[self.whiteView, self.purpleView, self.leftButton, self.cameraButton, self.rightButton]){
[self addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
}
[self createConstraints];
}
return self;
}
- (void) createConstraints{
NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(_leftButton, _cameraButton, _rightButton, _whiteView, _purpleView);
// The three buttons have equal widths and are distributed across the whole view
NSArray *allButtonsHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_leftButton][_cameraButton(==_leftButton)][_rightButton(==_leftButton)]|" options:kNilOptions metrics:nil views:viewDictionary];
// The left and right buttons have 10 points spacing from the top. All three buttons are aligned with the bottom of the view.
NSArray *leftButtonVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_leftButton]" options:kNilOptions metrics:nil views:viewDictionary];
NSArray *cameraButtonVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_cameraButton]|" options:kNilOptions metrics:nil views:viewDictionary];
NSArray *rightButtonVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_rightButton]|" options:kNilOptions metrics:nil views:viewDictionary];
// The white view goes behind all the buttons, 10 points from the top.
NSArray *whiteViewHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_whiteView]|" options:kNilOptions metrics:nil views:viewDictionary];
NSArray *whiteViewVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10-[_whiteView]|" options:kNilOptions metrics:nil views:viewDictionary];
// The purple view is positioned identically to the camera button
NSArray *purpleViewHorizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"[_leftButton][_purpleView][_rightButton]" options:NSLayoutFormatAlignAllBottom metrics:nil views:viewDictionary];
NSArray *purpleViewVerticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_purpleView]|" options:kNilOptions metrics:nil views:viewDictionary];
NSArray *allConstraintArrays = @[allButtonsHorizontalConstraints, leftButtonVerticalConstraints, cameraButtonVerticalConstraints, rightButtonVerticalConstraints, whiteViewHorizontalConstraints, whiteViewVerticalConstraints, purpleViewHorizontalConstraints, purpleViewVerticalConstraints];
for (NSArray *constraintsArray in allConstraintArrays){
for (NSLayoutConstraint *constraint in constraintsArray){
[self addConstraint:constraint];
}
}
}
-(void)layoutSubviews{
[super layoutSubviews];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.purpleView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.purpleView.bounds;
maskLayer.path = maskPath.CGPath;
self.purpleView.layer.mask = maskLayer;
}
#pragma mark - Button Handlers
- (void) leftButtonPressed:(UIButton *)sender{
[self.delegate leftButtonPressedOnToolbar:self];
}
- (void) rightButtonPressed:(UIButton *)sender{
[self.delegate rightButtonPressedOnToolbar:self];
}
- (void) cameraButtonPressed:(UIButton *)sender{
[self.delegate cameraButtonPressedOnToolbar:self];
}
@end