将触摸事件重新发送到iOS中的另一个视图

时间:2013-01-06 17:55:51

标签: objective-c ios xcode

我在UIImageView之上(而非元素)UIScrollView。好吧,我已经将UIImageView子类化为检查触摸事件并覆盖了touchesBegan:withEvent:方法。在该方法中,我基本上在UIImageView元素后面发送了子类UIScrollView。 (我使用[self.super bringSubviewToFront: scrollView]来实现这一目标。)

我想要的是能够将UIImageView的当前触摸事件发送到UIScrollView元素,这样用户就不必抬起手指再次触摸滚动。我已经尝试在touchesBegan:withEvent:元素上调用UIScrollView,但它什么也没做。

有没有人知道该怎么做才能模拟UIImageViewUIScrollView元素的触摸?

以下是子类UIImageView中的重写方法:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     [super touchesBegan: touches withEvent: event];

     ViewController* controller = [Singleton getSingleton].controller;
     [super bringSubviewToFront: controller.scrollView]; //Now scroll view is in the front

     [controller.scrollView touchesBegan: touches withEvent: event]; //Does nothing
}

1 个答案:

答案 0 :(得分:3)

我需要覆盖hitTest:withEvent:子类中的UIImageView

(UIView*) hitTest: (CGPoint)point withEvent: (UIEvent*)event
{
    if([self pointInside:point withEvent:event]){
         ViewController* controller = [Singleton getSingleton].controller;
         [controller.view bringSubviewToFront: controller.scrollView];
         return controller.scrollView;
    }else{
         return [super hitTest:point withEvent: event];
    }
}

这样,当发出UIImageView上的触摸时,它会首先调用此方法,这会将UIScrollView置于前面,然后将UIScrollView作为对象返回应该处理这个事件。