uiscrollview - UIScrollView 的滑动问题(iOS 8)

标签 uiscrollview swift ios8

我想在屏幕底部设计一个可滚动的按钮(就像照片中的过滤器),它是许多页面的目录(目录)。我曾经在下面编写类似的代码(在 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)
    }

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

最佳答案

我不知道如何快速完成但我在 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;
}

关于uiscrollview - UIScrollView 的滑动问题(iOS 8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25394269/

相关文章:

uitableview - 当使用 AutoLayout 和 UITableView 的动画大小时,避免 UITableViewCell 内容的动画

objective-c - 无法在iOS中实现简单的 View 滚动

core-data - 水平 ScrollView 可滑动核心数据对象

ios7 - _adjustContentOffsetIfNecessary - UIScrollView 自动滚动到顶部

ios - @State破坏了我位于导航栏的TextField-SwiftUI

iOS 13 UIBarButtonItem 字体

ios - 如何选择接收到触摸事件的 UIView

ios - 快速对象映射

ios - 解析包含数组元素的对象

swift - 如何在 swift 中将 UIFont 定义为常量值