用CG来实现UIButton的外观

直接上效果图:


红色

蓝色
选中状态

再上代码:
定义头文件

@interface QLMarkPenButton : UIButton

@property (nonatomic, strong) UIColor *markColor;

@end

实现文件:


#import "QLMarkPenButton.h"

//@implementation QLMarkPenButton
#define DebugMethod()  NSLog(@"%s",__func__)

@implementation QLMarkPenButton
{
    CAShapeLayer *shap;
    CAShapeLayer *shap2;
    
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

-(id)init{
    self = [super init];
    if (self) {
        [self commonInit];
    }
    return self;
}
-(void)commonInit{
    DebugMethod();
    _markColor = [UIColor blackColor];
    self.backgroundColor = [UIColor whiteColor];
}

-(void)setMarkColor:(UIColor *)markColor {
    _markColor = markColor;
    
    [self setNeedsDisplay];
}
//-(void)setStateBtn:(BtnState)stateBtn{
//    _stateBtn = stateBtn;
//    [self setNeedsDisplay];
//}

- (void)drawRect:(CGRect)rect {
    // Drawing code
    DebugMethod();
    
    CGFloat height = rect.size.height;
    CGFloat width = rect.size.width;
    
    CGFloat offset = self.selected?0:+height/4.0;
   
    if (shap) {
        [shap removeFromSuperlayer];
    }
    if (shap2) {
        [shap2 removeFromSuperlayer];
    }
    
    if (1) {
        
    
        UIBezierPath *path = [UIBezierPath bezierPath];
        [path setLineWidth:2];
        
        [path moveToPoint:CGPointMake(width/3.0, height+offset)];
        [path addLineToPoint:CGPointMake(width/3.0, height/2+height/12+offset)];
        
        [path addArcWithCenter:CGPointMake(width/3.0+width/12, height/2+width/12+offset) radius:width/12 startAngle:2/2.0*M_PI endAngle:3/2.0*M_PI clockwise:YES];
        [path addLineToPoint:CGPointMake(width/3.0+width/12, height/2-width/6+offset)];
        [path addLineToPoint:CGPointMake(width- (width/3.0+width/12), height/2-width/6+offset)];
        
        [path addArcWithCenter:CGPointMake(width- (width/3.0+width/12), height/2+width/12+offset) radius:width/12 startAngle:3/2.0*M_PI endAngle:0 clockwise:YES];
        [path addLineToPoint:CGPointMake(width- width/3.0, height+offset)];
        
//        [path addLineToPoint:CGPointMake(50, 50/2.0)];

//        [path addLineToPoint:CGPointMake(width/2-width/12, height/2.0+offset)];
//        [path addLineToPoint:CGPointMake(width/2+width/12, height/2.0+offset)];
//        [path addLineToPoint:CGPointMake(width- width/3.0, height+offset)];
//        [path closePath];
        [path stroke];
        
        shap = [CAShapeLayer new];
        shap.path = path.CGPath;
        shap.fillColor = [UIColor whiteColor].CGColor;
        shap.strokeColor = [UIColor blackColor].CGColor;
        [self.layer addSublayer:shap];
        
        
        
        
        
        UIBezierPath *path2 = [UIBezierPath bezierPath];
        [path2 setLineWidth:0.1];
        
//        [path2 moveToPoint:CGPointMake(width/3.0, height)];
        //        [path addLineToPoint:CGPointMake(50, 50/2.0)];
        [path2 moveToPoint:CGPointMake(width/3.0+width/12+2, height/2-width/6+offset)];
        [path2 addLineToPoint:CGPointMake(width/3.0+width/12+2, height/2-width/6-width/12+offset)];
        [path2 addLineToPoint:CGPointMake(width- (width/3.0+width/12+2), height/2-width/6-width/6+offset)];
        [path2 addLineToPoint:CGPointMake(width- (width/3.0+width/12+2),  height/2-width/6+offset)];
        
        
//
//        [path2 moveToPoint:CGPointMake(width/2-width/12+2, height/2.0-3+offset)];
//        [path2 addLineToPoint:CGPointMake(width/2, height/4.0+offset)];
//        [path2 addLineToPoint:CGPointMake(width/2+width/12-2, height/2.0-3+offset)];
        
        
        [path2 closePath];
//        [path2 stroke];
        [path2 fill];
        
        shap2 = [CAShapeLayer new];
        shap2.path = path2.CGPath;
        shap2.fillColor = _markColor.CGColor;
        shap2.strokeColor = [UIColor clearColor].CGColor;// _penColor.CGColor;
        [self.layer addSublayer:shap2];
                
                
        
        
        
    }else{
         
        CGContextRef contextRf  = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(contextRf, [UIColor blackColor].CGColor);
        
        
        CGContextSetLineWidth(contextRf, 2);
        //        CGContextMoveToPoint(contextRf, 100/3.0, 50);
        
        CGContextMoveToPoint(contextRf, width/3.0, height);
        CGContextAddLineToPoint(contextRf, width/2-width/12.0, height/2.0);
        CGContextAddLineToPoint(contextRf, width/2+width/12.0, height/2.0);
        CGContextAddLineToPoint(contextRf, width-width/3.0, height);
        
        CGContextStrokePath(contextRf);
        
        
        //    return;
        //第二条线
        CGContextSaveGState(contextRf); //   ----- 看这里
        
        CGContextBeginPath(contextRf);
        CGContextSetStrokeColorWithColor(contextRf, _markColor.CGColor);
        CGContextSetLineWidth(contextRf, 0.1);
        CGContextSetLineCap(contextRf, kCGLineCapRound);
        CGContextMoveToPoint(contextRf, width/2-width/12.0-1, height/2.0-1);
        CGContextAddLineToPoint(contextRf, width/2.0,height/4.0);
        CGContextAddLineToPoint(contextRf, width/2+width/12.0-1, height/2.0-1);
        CGContextAddLineToPoint(contextRf, width/2-width/12.0-1, height/2.0-1);
        CGContextClosePath(contextRf);
        CGContextStrokePath(contextRf);
        
        CGContextSetFillColorWithColor(contextRf, [_markColor CGColor]);
        CGContextFillPath(contextRf);
        
        
        CGContextRestoreGState(contextRf); // ---- 看这里
    }
    
    
    
    
    self.layer.masksToBounds = YES;
    
}



@end

全部评论

相关推荐

2025-12-12 19:58
哔哩哔哩_产品运营
跟同事聊天时候,同事说“你刚来时候blabla”,突然意识到自己已经正式工作一年多了!就这么从脆皮内耗大学生逐渐磨练成厚血条(厚脸皮)工位主理人。秋招简历当然也是投了不少份,但总有一些机会要留给自己的白月光,比如阿B,说说我秋招选择阿B的理由吧:1. “为爱发电”:说来兴趣真的是初心,阿B在手机陪我看了那么多番剧vlog学习视频,当然想和它距离更近一些。来了之后发现,B站重要活动要专门走内宣是有原因的,身边的六级大佬绝对不在少数。2. 实习体验感拉满:嗯对其实等不到正式工作就先来实习体验了。实习期在一个非常好的组,大家都很年轻氛围超好,做事情讲背景、讲逻辑不会只丢脏活累活。平时聊得来,工作起来也能快速打配合,项目完成时候所有人都成就感满满。再说说来正式工作之后的体验感:1. 校招生mentor文化很需要:在阿B每个校招生入职都是会有一位mentor的,不会让大家有刚工作人生地不熟就孤苦一人挑大梁的感觉。很幸运我的mt人真的超好,耐心温柔业务能力又很强。常常在对需求听她帮我说话时看着她身上闪耀的光芒想要流泪。有mt的话landing期会顺畅很多。公司也会安排一些活动帮助mentor和mentee增进感情。2.小动物们和各类活动是回血剂:工作起来当然难免遇到一些磕磕磨磨,但是压力大时候转头看到想悄悄溜过的小猫摸上一把,真的会治愈不少。还有节假日的各种活动和扫楼活动,真的会给上班增加动力。最后上图!没有任何工作会让人一直开心吧,但阿B你在照顾员工心情这一块儿做得真的很不错。
哔哩哔哩公司福利 915人发布
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务