我创建了两个名为btnFirst和btnSecond的UIButton。我想将三角形叠加到屏幕上,所以我使用三角笔画三角指向男性UIButton。我成功做到了。但是,当我单击任何按钮时,两个UIbutton动作都会触发。
请帮助我在按钮上触发两个不同的操作
// 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");
}
问题是,最终所有元素都是某种矩形。因此,在您的情况下,您创建了两个重叠的矩形,这些矩形显示不重叠的三角形。
[幸运的是,您不是第一个遇到此挑战的人:here是对类似问题的答案。尽管它适用于MacOS,但它应该为您指明正确的方向。