[英]UIButton touch event overlap
我需要使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,我已經閱讀了,但老實說我無法理解其中的內容。
很感謝任何形式的幫助。
Karn建議在Objective C中使用OBShapedButton庫為我工作,Touch事件可以完美工作。
我已經使用了OBShapedButton庫及其免費版本。 Ole Begemann的精彩演示
下載庫添加兩個文件夾名為(有OBShapedButton.h&OBShapedButton.m,UIImage的+ ColorAtPixel.h,UIImage的+ ColorAtPixel.m)
OBShapedButton
及UIImage+ColorAtPixel
到您的項目和在里面的情節串連圖板給的ViewController類屬性,而不是作為UIButtons然后OBShapedButton將出口拖動到YourViewController.h
。
#import "OBShapedButton.h"
@property (weak, nonatomic) IBOutlet OBShapedButton *obsButtonTest;
然后,
[obsButtonTest setImage:[UIImage imageNamed:@"image_name.png"] forState:UIControlStateNormal];
為了迅速,您可以使用BridgingHeader
在橋接標題中,您可以導入以下兩個類
#import "OBShapedButton.h"
#import "UIImage+ColorAtPixel.h"
將出口從情節提要板拖到您的viewcontroller.swift文件
@IBOutlet weak var obsButtonTest: OBShapedButton!
從浮板本身設置圖像或以編程方式設置圖像,
obsButtonTest.setImage(.image_name, for: .normal)
免費使用OBShapedButton
庫,將OBShapedButton.h
和OBShapedButton.m
類OBShapedButton.m
您的項目中,然后在VC.h文件中的OBShapedButton.h
中拖動按鈕,
#import "OBShapedButton.h"
@property (weak, nonatomic) IBOutlet OBShapedButton *yourCustomButton;
Objective-C的
將UIButton拖放到匹配模式的情節OBShapedButton
板上,將每個按鈕自定義類UIButton
OBShapedButton
為類標識符中的OBShapedButton
在中創建實例,或在VC.h文件中拖動出口。
[yourCustomButton setImage:[UIImage imageNamed:@"customImage.png"] forState:UIControlStateNormal];
為了迅速,您可以使用BridgeHeader
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.