检测 UIScrollView 之外的点击 [英] Detecting clicks outside of UIScrollView

查看:23
本文介绍了检测 UIScrollView 之外的点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据这种技术实现了分页滚动( iOS开发.如何扩展UIScrollView的滚动事件响应区域? ) 并且它按预期工作.

I've implemented a paged scroll according to this technique ( iOS develop. How to extend UIScrollView's scroll event responding area? ) and it works just as intended.

我正在滚动的视图包含几个按钮,我希望不仅能够单击居中/分页到滚动视图中的那些按钮,而且还能够单击其左侧和右侧的那些按钮.我找不到任何方法来解决这个问题,但我还不是一个真正的 iOS 绝地,但希望你们中的一个人是 :)

The view that I'm scrolling is containing a couple of buttons and I want to be able to click not only those that are centered/paged into the scrollview but also those to the left and to the right of it. I cannot find any way to solve this but I'm not really an iOS-Jedi yet, hoping one of you are though :)

从屏幕上可以看出,UIScrollView 大约是窗口宽度的三分之一,UIScrollView 的内容大小要大得多:大约 1500 像素,并且包含许多以编程方式添加的按钮.这个解决方案很酷的事情,以及实际工作的部分,是按钮:1) 被分页到滚动视图中2) 在滚动视图外部可见(因为滚动视图未选中剪辑子视图") 3)当在 uiscrollview 内部可见时,按钮是可点击的.

So as you can see from the screenie the UIScrollView is about a third of the width of the window, the contentsize of the UIScrollView is much larger: about 1500px and contains a lot of buttons added programmatically. The cool thing with this solution, and the part that actually works, is that the buttons: 1) are paged into the scrollview 2) are visible outside the scrollview (since "clip subviews" is unchecked for the scrollview) 3) the buttons are clickable when visible inside the uiscrollview.

但行不通的是:- 当前位于窗口外的按钮在点击它们时不会收到他们的"点击,而是将事件转发到底层(窗口的白色部分)视图.

BUT what doesn't work is simply this: - the buttons currently being outside of the window does not receive "their" clicks when clicking on them, the events are instead forwarded to the underlaying (the white part of the window) view.

推荐答案

所以,

我终于设法解决了这个难题,解决方案分为两个部分.问题是,正如你所记得的,点击事件没有传播到UIScrollView 之外(可见)的按钮.事实证明,点击是由底层视图捕获的,并且可以通过稍微改变关于谁被击中的规则来操纵他们找到目标的方式,从而欺骗事件让他们通过你想要的地方.不确定这是否是应该完成的方式,但它解决了我的问题......:)

I finally managed to solve this puzzle and the solution is divided into two parts. The problem was, as you way recall, that the click events did not travel to the buttons that were(visible) outside the UIScrollView. It turned out that the clicks were captured by the underlying view and that it is possible to manipulate their way to finding their target by bending the rules a bit regarding who got hit and thereby tricking the events to get passed where you want them. Not really sure if this is how it should be done but it solved my problem.. . :)

1) 第一个必须在底视图中重写以下方法以便它在适当的时候返回滚动视图而不是它本身.

1) First one must override the following method in the bottom view so that it returns the scrollview instead of itself when appropriate.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{
    UIView *view = [super hitTest:point withEvent:event];

    if (view == self) 
        return [self scrollView];

    return view;
}

2) scrollView 必须覆盖两个方法才能将点击交给其包含的对象.

2) The scrollView must override TWO methods to hand over the clicks to its contained objects.

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
    {
       UIView *view = [super hitTest:point withEvent:event];
       // Always return us.
       return view ;    
     }

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    // We want EVERYTHING!
    return YES;
}

非常感谢您的评论和愿意提供帮助.呵呵

Thanks a lot for you comments and willingness to help. I ho

这篇关于检测 UIScrollView 之外的点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆