forked from material-components/material-components-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathButtonsShapesExampleViewController.m
230 lines (189 loc) · 9.09 KB
/
ButtonsShapesExampleViewController.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "MaterialButtons+Theming.h"
#import "MaterialButtons.h"
#import "MaterialContainerScheme.h"
#import "MaterialShapeLibrary.h"
#import "MaterialShapes.h"
#import "MaterialTypography.h"
#import "supplemental/ButtonsTypicalUseSupplemental.h"
@interface ButtonsShapesExampleViewController ()
@property(nonatomic, strong) MDCFloatingButton *floatingButton;
@end
@implementation ButtonsShapesExampleViewController
- (id)init {
self = [super init];
if (self) {
self.colorScheme =
[[MDCSemanticColorScheme alloc] initWithDefaults:MDCColorSchemeDefaultsMaterial201804];
self.typographyScheme =
[[MDCTypographyScheme alloc] initWithDefaults:MDCTypographySchemeDefaultsMaterial201804];
self.containerScheme = [[MDCContainerScheme alloc] init];
self.containerScheme.colorScheme = self.colorScheme;
self.containerScheme.shapeScheme =
[[MDCShapeScheme alloc] initWithDefaults:MDCShapeSchemeDefaultsMaterial201809];
self.containerScheme.typographyScheme = self.typographyScheme;
}
return self;
}
- (MDCButton *)buildCustomOutlinedButton {
MDCButton *button = [[MDCButton alloc] init];
[button setBorderWidth:1.0 forState:UIControlStateNormal];
[button setBorderColor:[UIColor colorWithWhite:(CGFloat)0.1 alpha:1]
forState:UIControlStateNormal];
return button;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:(CGFloat)0.9 alpha:1];
UIColor *titleColor = [UIColor whiteColor];
// Raised button
MDCButton *containedButton = [[MDCButton alloc] init];
[containedButton setTitle:@"Add To Cart" forState:UIControlStateNormal];
[containedButton applyContainedThemeWithScheme:self.containerScheme];
UIImage *plusImage = [UIImage imageNamed:@"Plus"];
plusImage = [plusImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[containedButton setImage:plusImage forState:UIControlStateNormal];
MDCRectangleShapeGenerator *raisedShapeGenerator = [[MDCRectangleShapeGenerator alloc] init];
[raisedShapeGenerator setCorners:[[MDCCutCornerTreatment alloc] initWithCut:8]];
containedButton.shapeGenerator = raisedShapeGenerator;
[containedButton sizeToFit];
[containedButton addTarget:self
action:@selector(didTap:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:containedButton];
// Disabled raised button
MDCButton *disabledContainedButton = [[MDCButton alloc] init];
[disabledContainedButton setTitle:@"Disabled" forState:UIControlStateNormal];
[disabledContainedButton applyContainedThemeWithScheme:self.containerScheme];
MDCRectangleShapeGenerator *disabledRaisedShapeGenerator =
[[MDCRectangleShapeGenerator alloc] init];
MDCCurvedCornerTreatment *curvedCorners = [[MDCCurvedCornerTreatment alloc] init];
curvedCorners.size = CGSizeMake(10, 30);
[disabledRaisedShapeGenerator setCorners:curvedCorners];
disabledContainedButton.shapeGenerator = disabledRaisedShapeGenerator;
[disabledContainedButton sizeToFit];
[disabledContainedButton addTarget:self
action:@selector(didTap:)
forControlEvents:UIControlEventTouchUpInside];
[disabledContainedButton setEnabled:NO];
[self.view addSubview:disabledContainedButton];
// Flat button
MDCButton *flatButton = [[MDCButton alloc] init];
[flatButton setTitle:@"Oval Flat" forState:UIControlStateNormal];
[flatButton applyTextThemeWithScheme:self.containerScheme];
MDCPillShapeGenerator *flatShapeGenerator = [[MDCPillShapeGenerator alloc] init];
flatButton.shapeGenerator = flatShapeGenerator;
[flatButton sizeToFit];
[flatButton addTarget:self
action:@selector(didTap:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:flatButton];
// Custom outlined button
MDCButton *outlinedButton = [self buildCustomOutlinedButton];
[outlinedButton setTitle:@"Button" forState:UIControlStateNormal];
[outlinedButton applyOutlinedThemeWithScheme:self.containerScheme];
MDCSlantedRectShapeGenerator *outlinedShapeGenerator = [[MDCSlantedRectShapeGenerator alloc] init];
outlinedShapeGenerator.slant = 10;
outlinedButton.shapeGenerator = outlinedShapeGenerator;
[outlinedButton sizeToFit];
[outlinedButton addTarget:self
action:@selector(didTap:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:outlinedButton];
// Disabled custom outlined button
MDCButton *disabledOutlinedButton = [self buildCustomOutlinedButton];
[disabledOutlinedButton setTitle:@"Button" forState:UIControlStateNormal];
[disabledOutlinedButton applyOutlinedThemeWithScheme:self.containerScheme];
MDCRectangleShapeGenerator *disabledOutlinedShapeGenerator =
[[MDCRectangleShapeGenerator alloc] init];
[disabledOutlinedShapeGenerator
setTopEdge:[[MDCTriangleEdgeTreatment alloc] initWithSize:5 style:MDCTriangleEdgeStyleCut]];
[disabledOutlinedShapeGenerator setTopLeftCorner:[[MDCCutCornerTreatment alloc] initWithCut:10]];
[disabledOutlinedShapeGenerator
setTopRightCorner:[[MDCCurvedCornerTreatment alloc] initWithSize:CGSizeMake(5, 20)]];
[disabledOutlinedShapeGenerator
setBottomEdge:[[MDCTriangleEdgeTreatment alloc] initWithSize:5
style:MDCTriangleEdgeStyleHandle]];
[disabledOutlinedShapeGenerator
setBottomRightCorner:[[MDCCutCornerTreatment alloc] initWithCut:5]];
[disabledOutlinedShapeGenerator
setBottomLeftCorner:[[MDCCurvedCornerTreatment alloc] initWithSize:CGSizeMake(10, 5)]];
disabledOutlinedButton.shapeGenerator = disabledOutlinedShapeGenerator;
[disabledOutlinedButton sizeToFit];
[disabledOutlinedButton addTarget:self
action:@selector(didTap:)
forControlEvents:UIControlEventTouchUpInside];
[disabledOutlinedButton setEnabled:NO];
[self.view addSubview:disabledOutlinedButton];
// Floating action button
self.floatingButton = [[MDCFloatingButton alloc] init];
[self.floatingButton setTitleColor:titleColor forState:UIControlStateNormal];
[self.floatingButton setImageTintColor:UIColor.whiteColor forState:UIControlStateNormal];
[self.floatingButton setImage:plusImage forState:UIControlStateNormal];
[self.floatingButton sizeToFit];
MDCRectangleShapeGenerator *floatingShapeGenerator = [[MDCRectangleShapeGenerator alloc] init];
[floatingShapeGenerator
setCorners:[[MDCCutCornerTreatment alloc]
initWithCut:CGRectGetWidth(self.floatingButton.bounds) / 2]];
self.floatingButton.shapeGenerator = floatingShapeGenerator;
[self.floatingButton applySecondaryThemeWithScheme:self.containerScheme];
[self.floatingButton addTarget:self
action:@selector(didTap:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.floatingButton];
self.buttons = @[
containedButton, disabledContainedButton, flatButton, outlinedButton, disabledOutlinedButton,
self.floatingButton
];
[self setupShapeExampleViews];
}
- (void)setupShapeExampleViews {
UILabel *raisedButtonLabel = [self addLabelWithText:@"Contained: Cut Corners"];
UILabel *disabledRaisedButtonLabel = [self addLabelWithText:@"Disabled Contained: Curved Cut"];
UILabel *flatButtonLabel = [self addLabelWithText:@"Flat: Oval Ink"];
UILabel *outlinedButtonLabel = [self addLabelWithText:@"Outlined: Triangular"];
UILabel *disabledOutlinedButtonLabel = [self addLabelWithText:@"Outlined Disabled: Freeform"];
UILabel *floatingDiamondLabel = [self addLabelWithText:@"Floating Action: Diamond"];
self.labels = @[
raisedButtonLabel, disabledRaisedButtonLabel, flatButtonLabel, outlinedButtonLabel,
disabledOutlinedButtonLabel, floatingDiamondLabel
];
}
- (void)didTap:(id)sender {
NSLog(@"%@ was tapped.", NSStringFromClass([sender class]));
if (sender == self.floatingButton) {
[self.floatingButton
collapse:YES
completion:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[self.floatingButton expand:YES completion:nil];
});
}];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (animated) {
[self.floatingButton collapse:NO completion:nil];
}
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (animated) {
[self.floatingButton expand:YES completion:nil];
}
}
@end