123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- //
- // 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
|