美文网首页
纯代码写“圆角tab页切换按钮”

纯代码写“圆角tab页切换按钮”

作者: watayouxiang | 来源:发表于2017-07-18 17:32 被阅读19次
Jietu20170718-173316@2x.jpg

#import "ViewController.h"

@interface ViewController ()

@property (strong, nonatomic) UIButton *redButton;
@property (strong, nonatomic) UIButton *jiaxiButton;

@property (strong, nonatomic) CAShapeLayer *redBorderLayer;
@property (strong, nonatomic) CAShapeLayer *jiaxiBorderLayer;

@end

#define screenWidth [UIScreen mainScreen].bounds.size.width

#define buttonBackgroundColor_click [self colorFromHexRGB:(@"F56738")]
#define buttonTitleColor_click [UIColor whiteColor]
#define buttonBorderColor_click [self colorFromHexRGB:(@"F56738")]

#define buttonBackgroundColor_default [UIColor whiteColor]
#define buttonTitleColor_default [self colorFromHexRGB:(@"4D4D4D")]
#define buttonBorderColor_default [self colorFromHexRGB:(@"F56738")]


@implementation ViewController

-(UIButton *)redButton{
    if (!_redButton) {
        
        // 初始化 button
        _redButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _redButton.frame = CGRectMake(screenWidth/2-135, 100, 135, 32);
        _redButton.backgroundColor = buttonBackgroundColor_click;
        [_redButton setTitle:@"红包" forState:UIControlStateNormal];
        [_redButton setTitleColor:buttonTitleColor_click forState:UIControlStateNormal];
        [_redButton addTarget:self action:@selector(clickRedButton:) forControlEvents:UIControlEventTouchUpInside];
        
        // 路径
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_redButton.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(16, 16)];
        
        // 创建图层1(button图层)
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _redButton.bounds;// frame
        maskLayer.path = maskPath.CGPath;// 设置路径
        _redButton.layer.mask = maskLayer;// 修改button的图层
        
        // 创建图层2(button边框图层)
        self.redBorderLayer = [CAShapeLayer layer];
        self.redBorderLayer.bounds = CGRectMake(0, 0, 135, 32);
        self.redBorderLayer.position = CGPointMake(CGRectGetMidX(_redButton.bounds), CGRectGetMidY(_redButton.bounds));
        self.redBorderLayer.path = maskPath.CGPath;// 设置路径
        self.redBorderLayer.lineWidth = 3.0 / [[UIScreen mainScreen] scale];// 线宽
        //self.redBorderLayer.lineDashPattern = @[@8, @8];// 边框模式:虚线
        self.redBorderLayer.lineDashPattern = nil;// 边框模式:实线
        self.redBorderLayer.strokeColor = buttonBorderColor_click.CGColor;// 路径颜色
        self.redBorderLayer.fillColor = [UIColor clearColor].CGColor;// 路径内部填充颜色
        [_redButton.layer addSublayer:self.redBorderLayer];// 添加到button的子图层中
        
    }
    return _redButton;
}

-(UIButton *)jiaxiButton{
    if (!_jiaxiButton) {
        
        _jiaxiButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _jiaxiButton.frame = CGRectMake(screenWidth/2, 100, 135, 32);
        _jiaxiButton.backgroundColor = buttonBackgroundColor_default;
        [_jiaxiButton setTitle:@"加息券" forState:UIControlStateNormal];
        [_jiaxiButton setTitleColor:buttonTitleColor_default forState:UIControlStateNormal];
        [_jiaxiButton addTarget:self action:@selector(clickJiaxiButton:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_jiaxiButton.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(16, 16)];
        
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _jiaxiButton.bounds;
        maskLayer.path = maskPath.CGPath;
        _jiaxiButton.layer.mask = maskLayer;
        
        self.jiaxiBorderLayer = [CAShapeLayer layer];
        self.jiaxiBorderLayer.bounds = CGRectMake(0, 0, 135, 32);
        self.jiaxiBorderLayer.position = CGPointMake(CGRectGetMidX(_jiaxiButton.bounds), CGRectGetMidY(_jiaxiButton.bounds));
        self.jiaxiBorderLayer.path = maskPath.CGPath;
        self.jiaxiBorderLayer.lineWidth = 3.0 / [[UIScreen mainScreen] scale];
        //self.redBorderLayer.lineDashPattern = @[@8, @8];
        self.jiaxiBorderLayer.lineDashPattern = nil;
        self.jiaxiBorderLayer.strokeColor = buttonBorderColor_default.CGColor;
        self.jiaxiBorderLayer.fillColor = [UIColor clearColor].CGColor;
        [_jiaxiButton.layer addSublayer:self.jiaxiBorderLayer];
        
    }
    return _jiaxiButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.redButton];
    [self.view addSubview:self.jiaxiButton];
}

-(void)clickRedButton:(UIButton *)btn{
    [UIView animateWithDuration:0 animations:^{
        [self.view bringSubviewToFront:self.redButton];
        
        self.redButton.backgroundColor = buttonBackgroundColor_click;
        [self.redButton setTitleColor:buttonTitleColor_click forState:UIControlStateNormal];
        self.redBorderLayer.strokeColor = buttonBorderColor_click.CGColor;//路径颜色
        
        self.jiaxiButton.backgroundColor = buttonBackgroundColor_default;
        [self.jiaxiButton setTitleColor:buttonTitleColor_default forState:UIControlStateNormal];
        self.jiaxiBorderLayer.strokeColor = buttonBorderColor_default.CGColor;//路径颜色
        
    } completion:^(BOOL finished) {
        
    }];
}

-(void)clickJiaxiButton:(UIButton *)btn{
    [UIView animateWithDuration:0 animations:^{
        [self.view bringSubviewToFront:self.jiaxiButton];
        
        self.jiaxiButton.backgroundColor = buttonBackgroundColor_click;
        [self.jiaxiButton setTitleColor:buttonTitleColor_click forState:UIControlStateNormal];
        self.jiaxiBorderLayer.strokeColor = buttonBorderColor_click.CGColor;
        
        self.redButton.backgroundColor = buttonBackgroundColor_default;
        [self.redButton setTitleColor:buttonTitleColor_default forState:UIControlStateNormal];
        self.redBorderLayer.strokeColor = buttonBorderColor_default.CGColor;
        
    } completion:^(BOOL finished) {
        
    }];
}

- (UIColor *)colorFromHexRGB:(NSString *)inColorString {
    UIColor *result = nil;
    unsigned int colorCode = 0;
    unsigned char redByte, greenByte, blueByte;
    
    if (nil != inColorString)
    {
        NSScanner *scanner = [NSScanner scannerWithString:inColorString];
        (void) [scanner scanHexInt:&colorCode]; // ignore error
    }
    redByte = (unsigned char) (colorCode >> 16);
    greenByte = (unsigned char) (colorCode >> 8);
    blueByte = (unsigned char) (colorCode); // masks off high bits
    result = [UIColor
              colorWithRed: (float)redByte / 0xff
              green: (float)greenByte/ 0xff
              blue: (float)blueByte / 0xff
              alpha:1.0];
    return result;
}




@end

相关文章

网友评论

      本文标题:纯代码写“圆角tab页切换按钮”

      本文链接:https://www.haomeiwen.com/subject/khmhkxtx.html