【发布时间】:2017-02-08 13:03:54
【问题描述】:
我正在创建一个继承自 UIControl 的自定义按钮类。 我正在使用 Bizer Path 创建具有风筝形状的按钮,如图所示。
代码
.h
进口
@interface CustomButton : UIControl
-(instancetype)initWithFrame:(CGRect)frame;
@end
.m
#import "CustomButton.h"
@implementation CustomButton
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
UIBezierPath *path = [UIBezierPath new];
// [path moveToPoint:(CGPoint){0, 0}];
[path moveToPoint:(CGPoint){100,50}];
[path addLineToPoint:(CGPoint){100, 100}];
[path addLineToPoint:(CGPoint){150, 100}];
[path addLineToPoint:(CGPoint){200, 0}];
[path addLineToPoint:(CGPoint){100, 50}];
// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = self.bounds;
mask.path = path.CGPath;
// Mask the imageView's layer with this shape
self.layer.mask = mask;
}
return self;
}
@end
VC .m
CustomButton *button = [[CustomButton alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside] ;
[self.view addSubview:button];
}
- (void)buttonPressed:(id)sender
{
NSLog(@"pressing");
}
图片
我的问题是,
当我点击按钮外部(红色部分)时,它给了我一个点击事件。 我不想这样做。 请帮助我,我该如何解决?
谢谢
【问题讨论】:
-
它给你,因为你的按钮形状是方形的。明白了。
-
是的,我明白了,但我只想要这种类型的按钮形状。我该怎么做?
标签: ios objective-c iphone uibutton