#import <UIKit/UIKit.h>
/**
* 按钮类型
*/
typedef NS_ENUM(NSInteger, CardLoanBaseButtonStyle) {
// 深蓝色
CardLoanBaseButtonStyleBlue,
};
/**
2.0 基类button
*/
@interface CardLoanBaseButton : UIButton
/**
Button样式
*/
@property (nonatomic, assign) CardLoanBaseButtonStyle style;
/**
Button
*/
@property (nonatomic, strong) UIColor *maskColor;
/**
* 初始化 button
*/
+ (instancetype)buttonWithFrame:(CGRect)frame buttonStyle:(CardLoanBaseButtonStyle)buttonStyle;
@end
@interface CardLoanBaseButton ()
/**
Masklayer
*/
@property (nonatomic, strong) CAShapeLayer *maskLayer;
@end
@implementation CardLoanBaseButton
+ (instancetype)buttonWithFrame:(CGRect)frame buttonStyle:(CardLoanBaseButtonStyle)buttonStyle {
CardLoanBaseButton *button = [self buttonWithType:UIButtonTypeCustom];
button.frame = frame;
button.style = buttonStyle;
return button;
}
- (void)setStyle:(CardLoanBaseButtonStyle)style {
_style = style;
switch (_style) {
case CardLoanBaseButtonStyleBlue: {
self.maskColor = UIColorFromRGB(0x574ef2);
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:1 alpha:.2] forState:UIControlStateDisabled];
self.titleEdgeInsets = UIEdgeInsetsMake(0, 0, self.height * .2, 0);
}
break;
default:
break;
}
}
- (void)layoutSubviews {
[super layoutSubviews];
[self setShandowAndCurveRect];
}
/**
添加圆角和阴影
*/
- (void)setShandowAndCurveRect {
CGFloat radius = self.height * .5;
UIBezierPath *path = [UIBezierPath bezierPath];
[path setLineWidth:.5];
[path moveToPoint:CGPointMake(radius, 0)];
[path addLineToPoint:CGPointMake(self.width - radius, 0)];
[path addArcWithCenter:CGPointMake(self.width - radius, radius) radius:radius startAngle:- M_PI_2 endAngle:M_PI_2 clockwise:YES];
[path addLineToPoint:CGPointMake(radius, self.height)];
[path addArcWithCenter:CGPointMake(radius, radius) radius:radius startAngle:M_PI_2 endAngle:M_PI_2 * 3 clockwise:YES];
[path closePath];
self.maskLayer.frame = self.bounds;
self.maskLayer.strokeColor = self.maskColor.CGColor;
self.maskLayer.fillColor = self.maskColor.CGColor;
self.maskLayer.path = path.CGPath;
[self.layer insertSublayer:self.maskLayer atIndex:0];
self.maskLayer.shadowOffset = CGSizeMake(0, self.height * .1);
}
- (CALayer *)maskLayer {
if (_maskLayer == nil) {
_maskLayer = [CAShapeLayer layer];
self.maskLayer.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.8].CGColor;
self.maskLayer.shadowOpacity = 0.8;
}
return _maskLayer;
}
@end
网友评论