美文网首页
用CG来实现UIButton的外观

用CG来实现UIButton的外观

作者: LV大树 | 来源:发表于2020-09-07 11:13 被阅读0次

    直接上效果图:


    红色
    蓝色 选中状态

    再上代码:
    定义头文件

    @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
    
    

    相关文章

      网友评论

          本文标题:用CG来实现UIButton的外观

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