ios - 我可以打电话 - (void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event on subview

标签 ios objective-c uitouch uiresponder

我在我的 View 中识别触摸 - 如果某些情况发生我想用这个方法调用我的 subview 但它不起作用 - (如果我不覆盖 hittestWithEvent - 这个 View 将被返回)

我该怎么做?

编辑:

我想让原来的 SUBVIEW 得到“点击”,所以我尝试这样做:

-(void)didTap:(MyTapGesture *)tap
{
    // the original view that would have been returned by hit test. could be any arbitrary view
    UIView *theView = [super hitTest:[tap locationInView:self] withEvent:nil];
    NSSet *touchesBegan = tap.touchesBegan;
    NSSet *touchesEnded = tap.touchesEnded;
    UIEvent *eventBegan = tap.eventBegan;
    UIEvent *eventEnd = tap.eventEnd;

    NSSet *tbegan = [eventBegan touchesForView:theView];
    NSSet *tend = [eventEnd touchesForView:theView];
    // try to simulate the touch in the subview - not working   
    [theView touchesBegan:tbegan withEvent:eventBegan];
    [theView touchesEnded:tend withEvent:eventEnd];
}

@interface MyTapGesture : UITapGestureRecognizer

@property (nonatomic,strong) NSSet *touchesBegan;
@property (nonatomic,strong) NSSet *touchesEnded;
@property (nonatomic,strong) UIEvent *eventBegan;
@property (nonatomic,strong) UIEvent *eventEnd;
@end

@implementation MyTapGesture

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.touchesBegan = touches;
    self.eventBegan = event;
    [super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.touchesEnded = touches;
    self.eventEnd = event;
    [super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
}

@end

最佳答案

您可以在您的主视图上覆盖 touchesBegan:withEvent 并在条件发生时在您的 subview 上调用它:

//In your view
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (CONDITION)
    {
        //call your subview
        [yourSubview touchesBegan:touches withEvent:event];
    }
}

希望这就是您所追求的。

//扩展

您可以尝试向您的 subview 添加一个方法,该方法在满足条件时调用:

//In your subview
-(void)myMethod:(NSSet *)touches
{
    //Your code
}

并在你的 View 中调用它:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (CONDITION)
    {
        //call your subview
        [yourSubview myMethod:touches];
    }
}

关于ios - 我可以打电话 - (void)touchesBegan :(NSSet *)touches withEvent:(UIEvent *)event on subview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21968515/

相关文章:

ios - UITextfield成为第一响应者不起作用

iphone - iOS 应用程序窗口未初始化

ios - 通过兄弟 subview 传播触摸事件?

ios - 在 iOS 中检测未触摸 3 秒?

iOS 推送通知 - 我可以在一分钟内从我的服务器发送多少?

iphone - iOS iPhone如何处理 "User can use app from multiple devices"这样的要求?

iphone - 将手势识别器/ Action 方法附加到 View 会违反 Model View Controller 吗?

ios - 如何更改以编程方式在 IB 中创建的 AutoLayout 约束中的常量?

ios - UIRefreshControl 在弹出窗口中不适用于 UITableView

ios - 如何在屏幕上已经有触摸的情况下激活 UIGestureRecognizer?