记iOS不规则图形按钮点击事件--(类似圆饼其中一个)

1 新建一个继承UIButton的类,

#import "specialShapeView.h"
@interface specialShapeView : UIButton

@property (strong ,nonatomic) NSMutableArray *pathPoints;//起点/终点集合

@property (strong ,nonatomic) UIBezierPath *tempPath;

在specialShapeView.m文件中

-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    
    if (self) {
        self.userInteractionEnabled = YES;
    }
    return self;
}
-(void)setMask{

    self.tempPath = [[UIBezierPath alloc] init];
    //这个代码里我为了方便把坐标都放在数组里了 就用比较笨的方法一点一点画的 以后我再慢慢修改
    [self.tempPath moveToPoint:[self.pathPoints[0] CGPointValue]];
    [self.tempPath addQuadCurveToPoint:[self.pathPoints[2] CGPointValue] controlPoint:[self.pathPoints[1] CGPointValue]];
    [self.tempPath setLineWidth:3];
    [self.tempPath addLineToPoint:[self.pathPoints[3] CGPointValue]];   
    [self.tempPath addQuadCurveToPoint:[self.pathPoints[5] CGPointValue]  controlPoint:[self.pathPoints[4] CGPointValue]];
    [self.tempPath addLineToPoint:[self.pathPoints[0] CGPointValue]];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = [self.tempPath CGPath];
    layer.fillColor = [[UIColor whiteColor] CGColor];
    layer.frame = self.frame;
    self.layer.mask = maskLayer;

//    CAShapeLayer *maskBorderLayer = [CAShapeLayer layer];
//    maskBorderLayer.path = [self.tempPath CGPath];
//    maskBorderLayer.fillColor = [[UIColor clearColor] CGColor];
//    maskBorderLayer.strokeColor = [UIColor  yellowColor].CGColor;
//    maskBorderLayer.lineWidth = 3;
//    [self.layer addSublayer:maskBorderLayer];
    
    
    
}

在ViewContr里

{
 NSArray *tempArr  = @[[NSValue valueWithCGPoint:CGPointMake(60, 80)],[NSValue valueWithCGPoint:CGPointMake(180, 0)],[NSValue valueWithCGPoint:CGPointMake(300, 80)],[NSValue valueWithCGPoint:CGPointMake(240, 180)],[NSValue valueWithCGPoint:CGPointMake(180, 160)],[NSValue valueWithCGPoint:CGPointMake(120, 180)]];
    
    specialShapeView *specialView = [[specialShapeView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

 [specialView setTitle:@"zss" forState:(UIControlStateNormal)];
    
     specialView.titleEdgeInsets = UIEdgeInsetsMake(-self.view.frame.size.height*3/4, 100, 0, 60);
    
    [specialView setImage:[UIImage imageNamed:@"知识点视频"] forState:(UIControlStateNormal)];
    
    specialView.imageEdgeInsets = UIEdgeInsetsMake(-self.view.frame.size.height*3/4, 100, 0, 60);
    
    [specialView setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

 self.specView = specialView;
    
    specialView.backgroundColor = [UIColor orangeColor];
    
    specialView.pathPoints = [NSMutableArray arrayWithArray:tempArr];
    
    [self.view addSubview:specialView];
    
    [specialView setMask];

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
    
    [specialView addGestureRecognizer:tap];
}
- (void)tapImage:(UITapGestureRecognizer *)tapGestrue{
    CGPoint tapPoint = [tapGestrue locationInView:tapGestrue.view];
    if ([self.specView.tempPath containsPoint:tapPoint]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"点击"
                                                       delegate:self
                                              cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

实现效果如下

记iOS不规则图形按钮点击事件--(类似圆饼其中一个)_第1张图片
屏幕快照 2016-10-24 16.14.44.png

因为本人还是个技术小白,所以写的非常low,希望多学习指教,在这里贴一个链接,查资料的时候看到的demo 对学习很有帮助
https://github.com/ole/OBShapedButton

你可能感兴趣的:(记iOS不规则图形按钮点击事件--(类似圆饼其中一个))