处理包含 UIButton 的 UIView 的触摸事件



我无法从具有UIButton子视图的UIView捕获触摸事件。 UIButton可能会阻止touchesBegantouchesEnd事件到达外部视图。

如何处理这些事件?我尝试将UserInteractionEnabled设置为 false。触摸事件到达外部视图,但UIButton不会在此解决方案中转换为突出显示状态。

有没有办法在不丢失按钮自然行为的情况下完成此操作?

(附言我不想使用手势识别器等,因为我需要获取事件的实际时间戳(

如果我理解了你的问题,你可以试试这种方法:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    if (CGRectContainsPoint(yourbutton.frame, [touch locationInView:self])) { //if touch is beginning on Button
    } 
}

最新更新