記iOS不規(guī)則圖形按鈕點(diǎn)擊事件--(類(lèi)似圓餅其中一個(gè))

1 新建一個(gè)繼承UIButton的類(lèi),

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

@property (strong ,nonatomic) NSMutableArray *pathPoints;//起點(diǎn)/終點(diǎn)集合

@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];
    //這個(gè)代碼里我為了方便把坐標(biāo)都放在數(shù)組里了 就用比較笨的方法一點(diǎn)一點(diǎn)畫(huà)的 以后我再慢慢修改
    [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:@"知識(shí)點(diǎn)視頻"] 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:@"點(diǎn)擊"
                                                       delegate:self
                                              cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}

實(shí)現(xiàn)效果如下

屏幕快照 2016-10-24 16.14.44.png

因?yàn)楸救诉€是個(gè)技術(shù)小白,所以寫(xiě)的非常low,希望多學(xué)習(xí)指教,在這里貼一個(gè)鏈接,查資料的時(shí)候看到的demo 對(duì)學(xué)習(xí)很有幫助
https://github.com/ole/OBShapedButton

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容