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];
//這個代碼里我為了方便把坐標(biāo)都放在數(shù)組里了 就用比較笨的方法一點一點畫的 以后我再慢慢修改
[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];
}
}
實現(xiàn)效果如下
屏幕快照 2016-10-24 16.14.44.png
因為本人還是個技術(shù)小白,所以寫的非常low,希望多學(xué)習(xí)指教,在這里貼一個鏈接,查資料的時候看到的demo 對學(xué)習(xí)很有幫助
https://github.com/ole/OBShapedButton