【问题标题】:Swipe issue for UIScrollView (iOS 8)UIScrollView (iOS 8) 的滑动问题
【发布时间】:2014-08-19 22:57:24
【问题描述】:

我想在屏幕底部设计一个可滚动的按钮(如照片中的过滤器),它是许多页面的 TOC(目录)。我曾经在下面写过类似的代码(在 Objective-C 中)在 iOS 7 中运行良好,所以不确定这是我的 Swift 转换还是 iOS 8 的问题。

    tocView = UIScrollView(frame:CGRect(x:0, y:self.view.frame.size.height, width:self.view.frame.size.width, height:thumbHeight))

    for i in 0 ..< pages.count {
        let image = UIImage(named:pages[i].name)
        var button = UIButton.buttonWithType(.Custom) as UIButton
        button.setImage(resizeImage(image, size:CGSize(width:thumbWidth, height:thumbHeight)), forState:.Normal)

        button.frame = CGRect(x:thumbWidth * CGFloat(i), y:0, width:thumbWidth, height:thumbHeight)
        button.addTarget(self, action:"selectPage:", forControlEvents:.TouchUpInside)
        button.tag = i

        tocView.addSubview(button)
    }

一切正常 - 除了滑动并不总是有效。在我尝试刷卡的一半时间里,似乎下面的按钮会带走刷卡并且刷卡没有发生。只有当我快速滑动时才会发生滑动。我希望能够检测到正确的滑动或点击。有人遇到类似问题吗?

【问题讨论】:

    标签: uiscrollview swift ios8


    【解决方案1】:

    我不知道如何快速完成,但我在 objC 中遇到了同样的问题,所以我的解决方案具有 UIScrollView 的子类并添加以下内容:

    - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
    {
        UITouch *touch = [touches anyObject];
    
        if(touch.phase == UITouchPhaseMoved)
        {
            return NO;
        }
        else
        {
            return [super touchesShouldBegin:touches withEvent:event inContentView:view];
        }
    }
    

    编辑

    经过几次测试我发现了一个问题,有时 UIButton 操作没有被调用...所以我将方法替换为 touchesShouldCancelInContentView 并且效果更好...您可以尝试

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

    【讨论】:

    • 我已将其转换为 Swift。
    猜你喜欢
    • 2014-11-09
    • 2015-10-23
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    相关资源
    最近更新 更多