Ios 如何在UI按钮上触发不同的操作
我已经创建了两个UIButton,分别命名为btnFirst和btnSecond。我想根据屏幕把它变成三角形,所以我用bezire路径把它变成三角形。我成功了。但当我点击任何一个按钮时,两个按钮都会触发动作 请帮我在按钮上触发两个不同的动作Ios 如何在UI按钮上触发不同的操作,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我已经创建了两个UIButton,分别命名为btnFirst和btnSecond。我想根据屏幕把它变成三角形,所以我用bezire路径把它变成三角形。我成功了。但当我点击任何一个按钮时,两个按钮都会触发动作 请帮我在按钮上触发两个不同的动作 // Build a triangular path UIBezierPath *path = [UIBezierPath new]; [path moveToPoint:(CGPoint){0, 0}]; [path addLine
// Build a triangular path
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:(CGPoint){0, 0}];
[path addLineToPoint:(CGPoint){0, screenHeight}];
[path addLineToPoint:(CGPoint){screenWidth, 0}];
[path addLineToPoint:(CGPoint){screenHeight, 0}];
// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = btnFirst.bounds;
mask.path = path.CGPath;
// Mask the imageView's layer with this shape
btnFirst.layer.mask = mask;
UIBezierPath *path1 = [UIBezierPath new];
[path1 moveToPoint:(CGPoint){0, screenHeight}];
[path1 addLineToPoint:(CGPoint){screenWidth, 0}];
[path1 addLineToPoint:(CGPoint){screenWidth, screenHeight}];
// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask1 = [CAShapeLayer new];
mask1.frame = btnSecond.bounds;
mask1.path = path1.CGPath;
// Mask the imageView's layer with this shape
btnSecond.layer.mask = mask1;
- (IBAction)firstBtnClick:(id)sender {
NSLog(@"first");
}
- (IBAction)secondBtnClick:(id)sender {
NSLog(@"second");
}
和按钮上的操作
// Build a triangular path
UIBezierPath *path = [UIBezierPath new];
[path moveToPoint:(CGPoint){0, 0}];
[path addLineToPoint:(CGPoint){0, screenHeight}];
[path addLineToPoint:(CGPoint){screenWidth, 0}];
[path addLineToPoint:(CGPoint){screenHeight, 0}];
// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask = [CAShapeLayer new];
mask.frame = btnFirst.bounds;
mask.path = path.CGPath;
// Mask the imageView's layer with this shape
btnFirst.layer.mask = mask;
UIBezierPath *path1 = [UIBezierPath new];
[path1 moveToPoint:(CGPoint){0, screenHeight}];
[path1 addLineToPoint:(CGPoint){screenWidth, 0}];
[path1 addLineToPoint:(CGPoint){screenWidth, screenHeight}];
// Create a CAShapeLayer with this triangular path
// Same size as the original imageView
CAShapeLayer *mask1 = [CAShapeLayer new];
mask1.frame = btnSecond.bounds;
mask1.path = path1.CGPath;
// Mask the imageView's layer with this shape
btnSecond.layer.mask = mask1;
- (IBAction)firstBtnClick:(id)sender {
NSLog(@"first");
}
- (IBAction)secondBtnClick:(id)sender {
NSLog(@"second");
}
第一个按钮是灰色的,第二个是绿色的。
问题是,最终所有元素都是某种矩形。因此,在本例中,您创建了两个重叠的矩形,它们显示非重叠的三角形 幸运的是,你不是第一个遇到这个挑战的人:这是一个类似问题的答案。虽然它是针对MacOS的,但它应该为您指明正确的方向 编辑: 由于您无论如何都是使用贝塞尔路径绘制三角形,因此有一个比我之前发布的链接中所述的检查alpha更容易的解决方案 您需要创建自己的UIButton类并覆盖
hitTest(:with:)
方法
返回视图层次结构中接收器的最远子体
(包括自身)包含指定点的
该类可以如下所示:
类路径按钮:UIButton{
覆盖func hitTest(uPoint:CGPoint,带有事件:UIEvent?->UIView{
让hitView=super.hitTest(点,带:事件)
//确保掩码的类型为CAShapeLayer,否则将无法访问path属性
guard let mask=self.layer.mask as?CAShapeLayer else{return hitView}
//确保路径已设置
guard let path=mask.path else{return hitView}
//验证当前触摸是否在路径内
返回路径。包含(点)?hitView:nil
}
}
只有当触摸在其路径内时,才会触发自定义按钮的操作。如果没有定义,则每次触摸都会触发按钮的动作。请尝试clipToBounds=YES。@christianmini不工作。@christianmini
clipToBounds
将子视图剪辑到其父视图的绑定,而不是相反。如果需要进一步帮助,请告诉我。我不知道如何执行此操作。您可以帮助我,或者您有任何示例代码我现在时间不多,但我可以提供一些示例代码later@Yatendra抱歉这么晚才回答。我最近很忙,但这让我有时间为你的问题想出一个更简单的解决方案。谢谢你的回答。我会尝试实施并让你知道。