UIScrollView(iOS 8)的滑动问题

3

我想设计一个可滚动的按钮,位于屏幕底部(就像照片中的过滤器),它是许多页面的目录(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)
    }

一切都很好,除了有时候滑动不起作用。当我尝试滑动时,有一半的时间似乎下面的按钮会拦截滑动,使得滑动没有发生。只有当我非常快地滑动时,滑动才有可能实现。我希望能够检测到正确的滑动或点击操作。有人遇到类似的问题吗?

1个回答

1
我不知道如何用Swift实现,但是我在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。 - Lim Thye Chean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接