UIScrollView后面的UIButton

我有一个UIScrollView背后的UIButton。 滚动的背景是透明的,按钮位于右下角。 所以它是可见的。

问题在于UIButton在滚动下方时不会响应任何触摸事件。 在这种情况下,响应触摸事件需要什么?

谢谢


其实你可以做到这一点,我已经在Mac上完成了,不得不将事件传下去。 在网上搜索你应该能够找到一些指针。 我记得我花了一段时间才解决它。


您应该使用+ touchesBegan方法将触摸传递给下一个对象。
这里是你如何做到这一点的例子:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
printf("MyButton touch Begann");
[self.nextResponder touchesBegan:touches withEvent:event]; 
}

下面是关于响应链的更多信息:

  • 关于响应者链的一点点(http://iphonedevelopment.blogspot.com)

  • iOS的可可应用程序能力:响应者对象(http://developer.apple.com)


  • 今天我遇到了这个问题,经过很长时间的调查很难实现,因为您可能会失去拖动功能来换取按钮点击事件。

    但是,我最终得到了这个,一个UIScrollView子类,它接收它感兴趣的按钮或按钮来捕获事件。

    关键是使用[(UIControl *)查看sendActionsForControlEvents:UIControlEventTouchUpInside]; ,因为调用touchesBegan方法并不总是按预期工作。 这不会在您按下按钮时更改GUI,但您可以根据需要在自定义方法中实现该功能。

    @implementation ForwardScrollView
    
    @synthesize responders = _responders;
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            _touchesEnabled = YES;
        }
        return self;
    }
    
    
    - (id)initWithCoder:(NSCoder *)aDecoder {
        if ( self = [super initWithCoder: aDecoder]) {
            _touchesEnabled = YES;
        }
        return self;
    }
    
    
    - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
        return _touchesEnabled;
    }
    
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        _touchesEnabled = NO;
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        for(UITouch *touch in touches) {
            CGPoint point = [touch locationInView:self];
            point = [window convertPoint:point fromView:self];
            UIView *view = [window hitTest:point withEvent:event];
    
            UITouch* touch = [touches anyObject];
            UIGestureRecognizer* recognizer;
            if ([touch gestureRecognizers].count > 0) {
                recognizer = [touch gestureRecognizers][0];
            }
    
            if ( (!recognizer || ![recognizer isMemberOfClass:NSClassFromString(@"UIScrollViewPanGestureRecognizer")])  && [_responders containsObject: view]) {
                [(UIControl*)view sendActionsForControlEvents: UIControlEventTouchUpInside];
            }
    
        }
        _touchesEnabled = YES;
    
    }
    
    @end
    

    对gestureRecognizers的计数是为了避免在没有真正的敲击时可能触发按钮的平移手势。

    链接地址: http://www.djcxy.com/p/40527.html

    上一篇: UIButton behind UIScrollView

    下一篇: Touch Detection of UIButton added as SubView in UIScrollView