如何在iOS应用程序中与UIButton的特定区域进行交互

时间:2013-11-06 08:02:43

标签: ios uibutton uiimage action area

我必须在点击按钮的特定区域时执行操作。实际上在只有粉红色可见的区域。不在该地区之外。请查看随附的图片以及此消息,如果我在任何时候都不清楚,请告诉我。

enter image description here

3 个答案:

答案 0 :(得分:0)

你应该对UIButton进行子类化并覆盖它的- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event区域几何的基础或访问按钮的图像并检查某个点的颜色。

看看这个:OBShapedButton

答案 1 :(得分:0)

您可以添加自定义选择器,然后在那里获取触摸点。然后检查点是否在您的特定区域内。

[pinkButton addTarget:self action:@selector(buttonTouched:withEvent:) forControlEvents: UIControlEventTouchDown];
//Define the area here or get the exact frame if it is already a UIView
CGRect frameOfSpecificArea = CGRectMake(10, 20, 30, 40);
- (void)buttonTouched:(UIButton *)sender withEvent:event {
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint tapPoint = [touch locationInView:sender];
    NSLog(@"Pink Button touched x : %f y : %f", tapPoint.x, tapPoint.y);
    BOOL pointIsInside = CGRectContainsPoint(frameOfSpecificArea, tapPoint);
    if(pointIsInside) {
       NSLog(@"Yeah, i'm a great iOS-Developer, i can check if a point is inside a CGRect!");
    }
}

答案 2 :(得分:0)

您需要创建自定义UIButton类并添加方法:

-(BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if(![super pointInside:point withEvent:event]) return NO;

    return [bezierPath containsPoint:point];
}

你按下的bazierPath可见区域。看看这个链接docs