4

我在 UIScrollView 中有一个 UIView,在 UIView 里面是一个按钮。问题是当我按下该按钮时,按住它(在这种情况下按钮状态被按下)并尝试滚动,我的滚动视图不会滚动。它应该在其中。UIView 中有一个手势识别器,如果我的手按下 UIButton 并滚动,我正在尝试使用它的一个委托来允许滚动滚动视图。我该怎么做呢?

基本上总结一下,如果按下/按住按钮,我需要将触摸事件传递给滚动视图。如果它是来自按钮的触摸事件,那么显然它应该触发按钮的动作而不是滚动。

4

4 回答 4

5

老问题,但我刚遇到这个问题,并认为人们会从答案中受益。如果在 UIScrollView 中有 UIControl,默认情况下滚动不会取消触摸事件。解决方案是像这样子类化 UIScrollView:

@implementation PaginationScrollView {}

- (id)init {
    self = [super init];
    if (self) {
        self.canCancelContentTouches = YES;
    }
    return self;
}

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    return YES;
}

@end

如果视图是 UIControl,则 touchesShouldCancelInContentView 的默认实现返回 NO。

于 2012-08-23T20:58:15.450 回答
3

确保设置

yourScrollView.canCancelContentTouches = YES;

还是行不通?因为它只取消触摸而不是UIControlEventTouchUpInsideUIControlEvents

怎么解决?将此添加到.m文件顶部

@implementation UIScrollView (TouchesShouldCancelInContentView)

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    return YES;
}

@end
于 2013-11-21T16:01:33.217 回答
0

您不需要为 UIButton 的简单 TouchUpInside 操作添加 UIGestureRecognizer,只需执行以下操作:

[button addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];

然后创建选择器:

-(IBAction)buttonSelect:(id)sender{//do stuff here}
于 2012-07-16T15:34:21.403 回答
0

您可以尝试使 UIButton 不可交互:

button.userInteractionEnabled = NO;

然后将 UITapGestureRecognizer 添加到按钮:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(buttonPressed)];
[button addGestureRecognizer:recognizer];

这样,如果点击按钮,按钮只会对触摸事件做出反应,所有其他事件都将转到滚动视图。

设置 userInteractionEnabled = NO 可能会阻止 UITapGestureRecognizer 触发其事件,在这种情况下,您可以将按钮设置为 UIView 或 UIImageView。

于 2012-07-16T17:49:23.613 回答