繁体   English   中英

在UIScrollView中,UIButton触摸会延迟

[英]UIButton touch is delayed when in UIScrollView

我在我的应用程序中遇到了一个小问题。

我基本上在UIScrollView添加了一系列UIButtons作为子视图,它是一个nib的一部分。 每次按下按钮,按钮突出显示之前都会有明显的延迟。 在按钮变暗并显示选中之前,我基本上必须按住它约半秒钟。

我假设这是因为UIScrollView需要确定触摸是否是滚动或者是否是针对子视图的触摸。

无论如何,我对如何进行有点不确定。 我只想让按钮在点击它时立即显示。

任何帮助表示赞赏!

编辑:

我已经尝试将delaysContentTouches设置为NO但滚动变得几乎不可能,因为我的大部分scrollView都填充了UIButtons

杰夫的解决方案并不适合我,但这个类似的解决方案: http//charlesharley.com/2013/programming/uibutton-in-uitableviewcell-has-no-highlight-state

除了在滚动视图子类中覆盖touchesShouldCancelInContentView之外,还需要将delaysContentTouches设置为false 最后,您需要为按钮返回true而不是false 以下是上述链接的修改示例。 正如评论者建议的那样,它会检查UIControl任何子类而不是UIButton因此这种行为适用于任何类型的控件。

Objective-C的:

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.delaysContentTouches = false;
    }

    return self;
}

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
    if ([view isKindOfClass:UIControl.class]) {
        return true;
    }

    return [super touchesShouldCancelInContentView:view];
}

斯威夫特4:

override func touchesShouldCancel(in view: UIView) -> Bool {
    if view is UIControl {
        return true
    }
    return super.touchesShouldCancel(in: view)
}

好的,我通过touchesShouldCancelInContentView UIScrollView并覆盖touchesShouldCancelInContentView解决了这个touchesShouldCancelInContentView

现在我标记为99的UIButton正确突出显示,我的滚动视图正在滚动!

myCustomScrollView.h

@interface myCustomScrollView : UIScrollView  {

}

@end

myCustomScrollView.m

@implementation myCustomScrollView

    - (BOOL)touchesShouldCancelInContentView:(UIView *)view
    {
        NSLog(@"touchesShouldCancelInContentView");

        if (view.tag == 99)
            return NO;
        else 
            return YES;
    }

尝试将UIScrollView delaysContentTouches属性设置为NO。

故事板解决方案:选择滚动视图,打开“属性检查器”并取消选中“延迟内容触摸”

在此输入图像描述

在Swift 3中:

import UIKit

class ScrollViewWithButtons: UIScrollView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        myInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        myInit()
    }

    private func myInit() {
        self.delaysContentTouches = false
    }

    override func touchesShouldCancel(in view: UIView) -> Bool {
        if view is UIButton {
            return true
        }
        return super.touchesShouldCancel(in: view)
    }
}

然后,您可以在IB或代码中使用此ScrollViewWithButtons

现有的解决方案都不适合我。 也许我的情况更独特。

我在UIScrollView有很多UIButtons 按下UIButton会向用户显示一个新的UIViewController 如果按下按钮并保持足够长的时间,该按钮将显示其按下状态。 我的客户抱怨说,如果你拍得太快,就不会出现压抑状态。

我的解决方案:在UIButtons的tap方法中,我加载新的UIViewController并将其呈现在屏幕上,我使用

[self performSelector:@selector(loadNextScreenWithOptions:) 
           withObject:options 
           afterDelay:0.]

这会在下一个事件循环中安排下一个UIViewController的加载。 留出时间让UIButton重绘。 UIButton现在在加载下一个UIViewController之前显示其压低状态。

斯威夫特3:

scrollView.delaysContentTouches = false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM