// // APRoundedButton.m // jiaPei // // Created by EchoShacolee on 2018/6/14. // Copyright © 2018年 JCZ. All rights reserved. // #import "APRoundedButton.h" @interface APRoundedButton() { UIBezierPath *maskPath; } @end @implementation APRoundedButton - (void)makeCorner { UIRectCorner corners; switch ( self.style ) { case 0: corners = UIRectCornerBottomLeft; break; case 1: corners = UIRectCornerBottomRight; break; case 2: corners = UIRectCornerTopLeft; break; case 3: corners = UIRectCornerTopRight; break; case 4: corners = UIRectCornerBottomLeft | UIRectCornerBottomRight; break; case 5: corners = UIRectCornerTopLeft | UIRectCornerTopRight; break; case 6: corners = UIRectCornerBottomLeft | UIRectCornerTopLeft; break; case 7: corners = UIRectCornerBottomRight | UIRectCornerTopRight; break; case 8: corners = UIRectCornerBottomRight | UIRectCornerTopRight | UIRectCornerTopLeft; break; case 9: corners = UIRectCornerBottomRight | UIRectCornerTopRight | UIRectCornerBottomLeft; break; case 10: corners = -1; break; case 11: corners = -2; break; default: corners = UIRectCornerAllCorners; break; } if (corners == -1) { maskPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, self.bounds.size.height) radius:self.bounds.size.height-_nj_cornerRaduous startAngle:0 endAngle:M_PI clockwise:NO]; self.titleLabel.y += _nj_cornerRaduous; }else if (corners == -2) { maskPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width/2, 0) radius:self.bounds.size.height-_nj_cornerRaduous startAngle:M_PI endAngle:2*M_PI clockwise:NO]; self.titleLabel.y -= _nj_cornerRaduous; }else{ _nj_cornerRaduous = _nj_cornerRaduous ?: 10.0; maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(_nj_cornerRaduous, _nj_cornerRaduous)]; } CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; [self.layer setMasksToBounds:YES]; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setupUIOnce]; } return self; } - (instancetype)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; if (self) { [self setupUIOnce]; } return self; } - (void)setupUIOnce { [self makeCorner]; } - (void)layoutSubviews { [super layoutSubviews]; [self makeCorner]; } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{ BOOL res = [super pointInside:point withEvent:event]; if (res) { if ([maskPath containsPoint:point]) { return YES; } } return NO; } @end