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

我本质上有一系列 UIButtons 在一个子视图中添加 UIScrollView 这是笔尖的一部分。每次我点击按钮时,在突出显示按钮之前,都会有一个明显的延迟。从本质上讲,我必须将其持有大约半秒钟,然后才能选择。

我假设这是因为 UIScrollView 需要确定触摸是滚动的,还是为子视图的触摸。

无论如何,我有点不确定如何进行。我只是希望按一下按钮在我点击后立即出现。

任何帮助都将受到赞赏!

编辑:

我尝试了设置 delaysContentTouches 但是滚动几乎变得不可能,因为我的大多数卷轴都充满了 UIButtons.

有帮助吗?

解决方案 2

好的,我已经通过子类解决了 UIScrollView 和覆盖 touchesShouldCancelInContentView

现在我的 UIButton 它被标记为99个亮点,我的滚动浏览正在滚动!

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;
    }

其他提示

杰夫的解决方案对我来说并不完全有效,但是这种类似的解决方案也是如此: http://charlesharley.com/2013/programming/uibutton-in-utableviewcell-has-no-highlight-State

除了覆盖 touchesShouldCancelInContentView 在您的滚动视图子类中,您仍然需要设置 delaysContentTouchesfalse. 。最后,您需要返回 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];
}

Swift 4:

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

尝试设置UISCrollView delaysContentTouches 属性为否。

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

enter image description here

在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)
    }
}

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

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

我有很多 UIButtonsUIScrollView. 。当一个 UIButton 被压了一个新的 UIViewController 提交给用户。如果按下按钮并保持足够长的时间,该按钮将显示其抑郁状态。我的客户抱怨说,如果您点击得太快,则不会显示抑郁状态。

我的解决方案:内部 UIButtons'点击方法,我加载新的地方 UIViewController 并将其显示在屏幕上,我使用

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

这是安排下一个的加载 UIViewController 在下一个事件循环中。给时间花费时间 UIButton 重做。这 UIButton 现在在加载下一个之前显示其抑郁状态 UIViewController.

Swift 3:

scrollView.delaysContentTouches = false
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top