我需要使UIButton模式像下面的屏幕截图。我的问题是,如果我确实正常设置框架并将图像设置为按钮框架。触摸事件重叠。

我正在使用以下代码,

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,我已经阅读了,但老实说我无法理解其中的内容。

非常感谢您的帮助。

最佳答案

免费使用OBShapedButton库,将OBShapedButton.hOBShapedButton.m类拖到您的项目中。然后在VC.h文件中您想使用按钮的地方拖到OBShapedButton.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

10-05 20:20