将滚动事件从 uibutton 传递到 uiscrollview

问题描述 投票:0回答:5

我有水平的

UIScrollView
,它是从
UIScrollView
延伸出来的,并且我水平地添加了
UIButtons
。我只能滚动到按钮区域之外,但如果我想滚动到任何按钮,就会触发
UIControlEventTouchUpInside
事件。我不想要这个。如果我点击,我想触发
UIControlEventTouchUpInside
动作,如果我滚动,我想滚动。

那么我如何将滚动事件从

UIButton
传递到
UIScrollView

iphone scroll uiscrollview uibutton
5个回答
4
投票

UIScrollView子类,在

- touchesShouldCancelInContentView:
方法中返回YES。


3
投票

该功能已内置。当您将 UIControl 元素作为滚动视图的子视图并且检测到触摸事件时,它最初会传递到 UIScrollView。如果一两分钟后触摸事件没有足够的移动,它就会传递到按钮。


0
投票

如果您继承 NSButton 并制作该类型的按钮并覆盖子类中的以下内容,您可以将事件传递回父视图,在您的情况下是滚动视图

以下将使按钮永远不会触发事件,而所有事件都将由父视图处理:

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

- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
    [self.nextResponder touchesMoved:touches withEvent:event];  
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent*)event 
{
    [self.nextResponder touchesEnded:touches withEvent:event];  
}

如果您想让按钮处理这些事件之一,请使用

[super touchesEnded:touches withEvent:event];

0
投票

试试这个:

self.button.exclusiveTouch = YES

对我来说就像一个魅力!


0
投票

确保您的滚动视图具有: .delaysContentTouches = true

© www.soinside.com 2019 - 2024. All rights reserved.