简体   繁体   English

UIButton触摸事件重叠

[英]UIButton touch event overlap

I need to make UIButton pattern like follwing screenshot. 我需要使UIButton模式像下面的截图一样。 My problem is if i do set frame normally and set image to button frame. 我的问题是,如果我确实正常设置框架并将图像设置为按钮框架。 Touch event get overlapping. 触摸事件重叠。

I am using follwing code, 我正在使用以下代码,

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];

One my Senior is telling me, i need to use CALayer , I have read but honestely i am not able to understand there stuffs. 我的一位上司告诉我,我需要使用CALayer,我已经阅读了,但老实说我无法理解其中的内容。

Any help is very much appreciated. 很感谢任何形式的帮助。

Karn suggested OBShapedButton library worked for me in Objective C, Touch events works perfectly. Karn建议在Objective C中使用OBShapedButton库为我工作,T​​ouch事件可以完美工作。

I have Used OBShapedButton library & its free of cost. 我已经使用了OBShapedButton库及其免费版本。 Brilliant demo by Ole Begemann Ole Begemann的精彩演示

Download library add two folders(having OBShapedButton.h & OBShapedButton.m, UIImage+ColorAtPixel.h , UIImage+ColorAtPixel.m) called OBShapedButton & UIImage+ColorAtPixel to your project & Inside your viewcontroller on storyboard give class attribute instead UIButtons as OBShapedButton then drag the outlet to your YourViewController.h . 下载库添加两个文件夹名为(有OBShapedButton.h&OBShapedButton.m,UIImage的+ ColorAtPixel.h,UIImage的+ ColorAtPixel.m) OBShapedButtonUIImage+ColorAtPixel到您的项目和在里面的情节串连图板给的ViewController类属性,而不是作为UIButtons然后OBShapedButton将出口拖动到YourViewController.h

#import "OBShapedButton.h"

@property (weak, nonatomic) IBOutlet OBShapedButton *obsButtonTest;

then, 然后,

[obsButtonTest setImage:[UIImage imageNamed:@"image_name.png"] forState:UIControlStateNormal];

For swift you can use BridgingHeader 为了迅速,您可以使用BridgingHeader

And In bridging header you can import following two classes 在桥接标题中,您可以导入以下两个类

#import "OBShapedButton.h"
#import "UIImage+ColorAtPixel.h"

Drag outlet from storyboard to your viewcontroller.swift file 将出口从情节提要板拖到您的viewcontroller.swift文件

@IBOutlet weak var obsButtonTest: OBShapedButton!

Set image from stroyboard itself or set it programmatically, 从浮板本身设置图像或以编程方式设置图像,

obsButtonTest.setImage(.image_name, for: .normal)

三角形按钮屏幕

Use OBShapedButton library its free of cost, Drag OBShapedButton.h & OBShapedButton.m class in your project.& thereafter OBShapedButton.h in your VC.h file where you want use buttons, 免费使用OBShapedButton库,将OBShapedButton.hOBShapedButton.mOBShapedButton.m您的项目中,然后在VC.h文件中的OBShapedButton.h中拖动按钮,

#import "OBShapedButton.h"

@property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;

Objective-C Objective-C的

Drag & place UIButton's on storyboard of your matching pattern change each button Custom class UIButton to OBShapedButton in class identifier & create instance in the or drag outlet in the VC.h file. 将UIButton拖放到匹配模式的情节OBShapedButton板上,将每个按钮自定义类UIButton OBShapedButton为类标识符中的OBShapedButton在中创建实例,或在VC.h文件中拖动出口。

[yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];

& for swift you can use BridgeHeader 为了迅速,您可以使用BridgeHeader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM