UIButton触摸事件重叠
问题描述:
我需要制作像follwing截图一样的UIButton模式。我的问题是如果我正常设置框架并将图像设置为按钮框架。触摸事件重叠。UIButton触摸事件重叠
我使用follwing代码,
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageWithName:@"Triangle1.png"]];
button1.frame = CGRectMake(100.0, 100.0, 150.0, 150.0);
[view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self
action:@selector(aMethod1:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button2.frame = CGRectMake(175.0, 100.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button2];
UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
[button3 addTarget:self
action:@selector(aMethod2:)
forControlEvents:UIControlEventTouchUpInside];
[button3 setImage:[UIImage imageWithName:@"Triangle2.png"]];
button3.frame = CGRectMake(150.0, 260.0, 150.0, 150.0);// if set X to 250 then pattern not seems
[view addSubview: button3];
我的一位资深告诉我,我需要使用的CALayer,我看过,但honestely我不能够理解的东西存在。
任何帮助是非常赞赏。
答
使用OBShapedButton
库其免费的,将OBShapedButton.h
& OBShapedButton.m
类项目。 &此后OBShapedButton.h
在您希望使用的按钮你VC.h文件,
#import "OBShapedButton.h"
@property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;
Objective-C的
将&地方的UIButton对您的匹配模式的故事板改变每个按键的自定义类UIButton
到OBShapedButton
在类标识符&在VC.h文件的或拖出口中创建实例。
[yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];
&为迅速,您可以使用BridgeHeader
你为什么不设置图像按钮标题形象??? –
看到这个http://*.com/questions/30315337/how-to-draw-a-triangle-shaped-uibutton –
先生你的意思是我必须设置titleEdgeInset?但仍触及甚至重叠 –