-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUIButton+Common.m
executable file
·214 lines (195 loc) · 8.12 KB
/
UIButton+Common.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
//
// UIButton+Common.m
// Coding_iOS
//
// Created by 王 原闯 on 14-8-5.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "UIButton+Common.h"
#import "Login.h"
#import <POP+MCAnimate/POP+MCAnimate.h>
#import <math.h>
@interface UIButton ()
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
@end
@implementation UIButton (Common)
+ (UIButton *)buttonWithTitle:(NSString *)title titleColor:(UIColor *)color{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor clearColor];
[btn setTitleColor:color forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightTextColor] forState:UIControlStateHighlighted];
[btn.titleLabel setFont:[UIFont systemFontOfSize:kBackButtonFontSize]];
[btn.titleLabel setMinimumScaleFactor:0.5];
CGFloat titleWidth = [title getWidthWithFont:btn.titleLabel.font constrainedToSize:CGSizeMake(kScreen_Width, 30)] +20;
btn.frame = CGRectMake(0, 0, titleWidth, 30);
[btn setTitle:title forState:UIControlStateNormal];
return btn;
}
+ (UIButton *)buttonWithTitle_ForNav:(NSString *)title{
return [UIButton buttonWithTitle:title titleColor:kColorBrandBlue];
}
+ (UIButton *)buttonWithUserStyle{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn userNameStyle];
return btn;
}
- (void)frameToFitTitle{
CGRect frame = self.frame;
CGFloat titleWidth = [self.titleLabel.text getWidthWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(kScreen_Width, frame.size.height)];
frame.size.width = titleWidth;
[self setFrame:frame];
}
- (void)userNameStyle{
self.layer.masksToBounds = YES;
self.layer.cornerRadius = 2.0;
self.titleLabel.font = [UIFont systemFontOfSize:17];
// [self setTitleColor:kColor222 forState:UIControlStateNormal];
[self setTitleColor:kColorDark3 forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateHighlighted];
}
- (void)setUserTitle:(NSString *)aUserName{
[self setTitle:aUserName forState:UIControlStateNormal];
[self frameToFitTitle];
}
- (void)setUserTitle:(NSString *)aUserName font:(UIFont *)font maxWidth:(CGFloat)maxWidth{
[self setTitle:aUserName forState:UIControlStateNormal];
CGRect frame = self.frame;
CGFloat titleWidth = [self.titleLabel.text getWidthWithFont:font constrainedToSize:CGSizeMake(CGFLOAT_MAX, frame.size.height)];
if (titleWidth > maxWidth) {
titleWidth = maxWidth;
// self.titleLabel.minimumScaleFactor = 0.5;
// self.titleLabel.adjustsFontSizeToFitWidth = YES;
}
[self setWidth:titleWidth];
[self.titleLabel setWidth:titleWidth];
}
- (void)configFollowBtnWithUser:(User *)curUser fromCell:(BOOL)fromCell{
// 对于自己,要隐藏
if ([Login isLoginUserGlobalKey:curUser.global_key]) {
self.hidden = YES;
return;
}else{
self.hidden = NO;
}
NSString *imageName;
if (curUser.followed.boolValue) {
if (curUser.follow.boolValue) {
imageName = @"btn_followed_both";
}else{
imageName = @"btn_followed_yes";
}
}else{
imageName = @"btn_followed_not";
}
// if (fromCell) {
// imageName = [imageName stringByAppendingString:@"_cell"];
// }
[self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
+ (UIButton *)btnFollowWithUser:(User *)curUser{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 32)];
[btn configFollowBtnWithUser:curUser fromCell:NO];
return btn;
}
- (void)configPriMsgBtnWithUser:(User *)curUser fromCell:(BOOL)fromCell{
// 对于自己,要隐藏
if ([Login isLoginUserGlobalKey:curUser.global_key]) {
self.hidden = YES;
return;
}else{
self.hidden = NO;
}
NSString *imageName;
if (curUser.followed.boolValue && !fromCell) {
imageName = @"btn_privateMsg_friend";
}else{
imageName = @"btn_privateMsg_stranger";
}
[self setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}
+ (UIButton *)btnPriMsgWithUser:(User *)curUser{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 32)];
[btn configPriMsgBtnWithUser:curUser fromCell:NO];
return btn;
}
+ (UIButton *)tweetBtnWithFrame:(CGRect)frame alignmentLeft:(BOOL)alignmentLeft{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
button.titleLabel.font = [UIFont systemFontOfSize:12];
[button setTitleColor:kColorDark7 forState:UIControlStateNormal];
if (alignmentLeft) {
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5);
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10);
}else{
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
button.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
button.titleEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 5);
}
return button;
}
- (void)animateToImage:(NSString *)imageName{
UIImage *image = [UIImage imageNamed:imageName];
if (!image) {
return;
}
[self setImage:image forState:UIControlStateNormal];
if ([self superview]) {
UIView *superV = [self superview];
UIImageView *imageV = [[UIImageView alloc] initWithImage:image];
CGRect vFrame = [self convertRect:self.imageView.frame toView:superV];
imageV.frame = vFrame;
[superV addSubview:imageV];
//animate
CGAffineTransform fromTransform = imageV.transform;
CGPoint fromCenter = imageV.center;
CGFloat dx = CGRectGetWidth(self.frame) /2;
CGFloat dy = CGRectGetHeight(self.frame) *3;
CGFloat dScale = 3.0;
CGFloat dRotation = M_PI_4;
NSTimeInterval moveDurarion = 1.0;
POPCustomAnimation *moveA = [POPCustomAnimation animationWithBlock:^BOOL(id target, POPCustomAnimation *animation) {
float time_percent = (animation.currentTime - animation.beginTime)/moveDurarion;
UIView *view = (UIView *)target;
CGPoint toCenter = CGPointMake(fromCenter.x + time_percent * dx, fromCenter.y - (dy * time_percent * (2 - time_percent)));//抛物线移动
view.center = toCenter;
CGAffineTransform toTransform = fromTransform;
toTransform = CGAffineTransformTranslate(toTransform, 50, -50);
CGFloat toScale = 1 + time_percent *(dScale - 1);//线性放大
toTransform = CGAffineTransformMakeScale(toScale, toScale);
CGFloat toRotation = dRotation * (1- cosf(time_percent * M_PI_2));//cos曲线旋转(先慢后快)
toTransform = CGAffineTransformRotate(toTransform, toRotation);
view.transform = toTransform;
view.alpha = 1 - time_percent;
return time_percent < 1.0;
}];
[imageV pop_addAnimation:moveA forKey:@"animateToImage"];
}
}
#pragma mark -
//开始请求时,UIActivityIndicatorView 提示
static char EAActivityIndicatorKey;
- (void)setActivityIndicator:(UIActivityIndicatorView *)activityIndicator{
objc_setAssociatedObject(self, &EAActivityIndicatorKey, activityIndicator, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIActivityIndicatorView *)activityIndicator{
return objc_getAssociatedObject(self, &EAActivityIndicatorKey);
}
- (void)startQueryAnimate{
if (!self.activityIndicator) {
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.activityIndicator.hidesWhenStopped = YES;
[self addSubview:self.activityIndicator];
[self.activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
}];
}
[self.activityIndicator startAnimating];
self.enabled = NO;
}
- (void)stopQueryAnimate{
[self.activityIndicator stopAnimating];
self.enabled = YES;
}
@end