【问题标题】:Can I call - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event on subview我可以在子视图上调用 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
【发布时间】:2014-02-23 12:58:02
【问题描述】:

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

我该怎么做?

编辑:

我希望原始的 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

【问题讨论】:

  • 你的视图和子视图是什么子类(是UIView)?请发布一些代码。
  • 两者都是自定义视图 - 每个都包含一些控件

标签: ios objective-c uitouch uiresponder


【解决方案1】:

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

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

希望这就是你所追求的。

//扩展

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

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

并在你的视图中调用它:

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

【讨论】:

  • 我试过了 - 但由于某种原因,我的手势识别器停止工作,并且子视图无法触摸
  • 查看我的扩展帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多