【问题标题】:(iphone) uiscrollview touch. cancel content touch and begin scrolling(iphone) uiscrollview 触摸。取消内容触摸并开始滚动
【发布时间】:2011-02-11 09:19:04
【问题描述】:

UIScrollView 编程对我来说似乎是最困难的任务之一 :(

我在 UIScrollView 中有子视图。
我想让子视图首先获取触摸事件(并被拖动)。
当子视图认为它应该放弃触摸并开始滚动时,它应该能够告诉滚动视图。

我已经尝试过 touchesShouldCancelInContentView,但是当 scrollView 认为用户实际上想要“滚动”时,这个方法只会被调用一次。
我需要控制何时取消子视图的触摸事件,而不依赖于内部 uiscrollview 的实现。

到目前为止我最好的策略是

子类 ScrollView

- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
    SYSLOG(LOG_DEBUG, "MyScrollView touchesBegan");

    if (!self.dragging && self.isSubviewTouchable)
        [self.nextResponder touchesBegan: touches withEvent:event];

    [super touchesBegan: touches withEvent: event];
}

从scrollView的子视图

当subview需要释放触摸,让scrollView去接受触摸事件等等。

self.myScrollView.isSubviewTouchable = false;
[self touchesEnded: touches withEvent: event];

在 scrollView 的委托类中,我将 bool 值设置回来。

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{   
    self.myScrollView.isSubviewTouchable = true;
}

问题是,从子视图调用 touchesEnded 实际上并没有释放它看起来的触摸事件。(即使在 touchesEnded 调用之后子视图仍然被拖动) 此外,在我希望的 touchesEnded 调用之后,我没有看到 scrollView 获得任何触摸事件。

有什么想法吗?

谢谢

【问题讨论】:

    标签: iphone uiscrollview touch


    【解决方案1】:

    我曾经尝试使用 PanGesture 来解决这个问题。对于您希望可拖动的所有子视图,您可能可以添加一个 UIPanGestureRegognizer 并在它们的选择器中做任何您想做的事情。即使在 ScrollView 中也能正常工作。

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething:)];
    [panGesture setDelegate:self];
    [subviewtoMove addGestureRecognizer:panGesture];
    

    【讨论】:

    • 这适用于 ios 3.12 设备吗? (从 4.x 构建).. 看起来像。从 3.2 .. umm .. 虽然我不确定文档何时说 3.2 这意味着设备应该有 3.2+ 或者构建库应该是 3.2+ :(
    • 我认为 iOS 3.2+ 版本支持此功能,因此如果您使用的是 3.12 设备,它将无法正常工作。
    猜你喜欢
    • 2013-03-26
    • 2011-06-22
    • 2012-12-29
    • 2013-09-01
    • 1970-01-01
    • 2015-06-30
    • 2013-06-08
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多