【问题标题】:Button Not Firing Event Inside ScrollViewScrollView 内的按钮未触发事件
【发布时间】:2016-05-23 05:33:34
【问题描述】:

UIButtons 内的scrollview 没有触发事件。我尝试设置exclusiveTouch delaysTouchesEvent,但没有任何帮助。

class TestViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    var categoryArr = ["Jack","Mark","Down","Bill","Steve"]
    var buttonColors = [UIColor.greenColor(), UIColor.blueColor(), UIColor.blackColor(), UIColor.cyanColor(), UIColor.magentaColor()]

    override func viewDidLoad() {
        super.viewDidLoad()
        let scrollingView = colorButtonsView(CGSizeMake(150,scrollView.frame.size.height), buttonCount: 5)
        scrollView.contentSize = scrollingView.frame.size
        scrollView.addSubview(scrollingView)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.indicatorStyle = .Default
        scrollView.canCancelContentTouches = false
        scrollView.delaysContentTouches = false
        scrollView.exclusiveTouch = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func colorButtonsView(buttonSize:CGSize, buttonCount:Int) -> UIView  {
        let buttonView = UIView()
        buttonView.frame.origin = CGPointMake(0,0)
        let padding = CGSizeMake(10, 10)
        buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(buttonCount)
        var buttonPosition = CGPointMake(padding.width * 0.5, padding.height)
        let buttonIncrement = buttonSize.width + padding.width
        for i in 0...(buttonCount - 1)  {
            var button = UIButton.buttonWithType(.Custom) as! UIButton
            button.frame.size = buttonSize
            button.frame.origin = buttonPosition
            buttonPosition.x = buttonPosition.x + buttonIncrement
            button.setTitle(categoryArr[i], forState: UIControlState.Normal)
            button.addTarget(self, action: "showTheNextButton:", forControlEvents: UIControlEvents.TouchUpInside)
            button.backgroundColor = buttonColors[i]
            buttonView.addSubview(button)
        }
        return buttonView
    }

    func showTheNextButton(sender:UIButton){
        print("ok show the next button")
    }
}

extension TestViewController:UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

        let index = round(scrollView.contentOffset.x / scrollView.frame.size.width)
        print(index)
    }
}

【问题讨论】:

  • 控制台中没有错误信息吗?我最近在控件无响应时遇到了类似的问题,原因是约束配置不正确。
  • 没有什么...我在故事板中有滚动视图..我将发布它的屏幕截图
  • 您已经通过 IBOutlet 连接在 Storyboard 中使用了 UIScrollview,那么这条线的目的是什么? scrollView.addSubview(scrollingView)
  • 我还记得当我想创建可滚动视图时,我最终得到的解决方案是在滚动视图中使用另一个容器 UIView 以及该容器视图中的所有控件。
  • @DmitryKlochkov 我也在做同样的事情...scrollingView 是容器视图并添加到滚动视图...

标签: ios swift uiscrollview uibutton


【解决方案1】:

如果UIButton 位于其父视图之一的范围之外,则它不会接受触摸。确保所有视图的大小和位置都符合您的预期

【讨论】:

  • 请你说我错过了哪里......整个代码都在问题中......让我大吃一惊
  • @swift BUTCHER 尝试向视图添加可见背景以检查它们的边界。
  • 容器视图是这里的问题...我发现了
  • 容器视图是否太小?按钮是否位于容器视图的边界之外?
【解决方案2】:

请进行以下更改:

self.scrollView.canCancelContentTouches = true
self.scrollView.delaysContentTouches = true

在您的 for-loop 中,将您的 let buttonView = UIView() 移动到:

@IBOutlet weak var scrollView: UIScrollView!
var categoryArr = ["Jack","Mark","Down","Bill","Steve"]
var buttonColors = [UIColor.greenColor(), UIColor.blueColor(), UIColor.blackColor(), UIColor.cyanColor(), UIColor.magentaColor()]
let buttonView = UIView()

然后,在您的 for-loop 中再次更改以下内容:

将此行:buttonView.frame.origin = CGPointMake(0,0) 更改为

self.buttonView.frame = self.scrollView.bounds
self.buttonView.userInteractionEnabled = true

附录:

对此发表评论://self.scrollView.exclusiveTouch = true

我冒昧地对其进行了测试;触摸应该在按钮上注册,滚动也应该水平工作。享受吧。

【讨论】:

    猜你喜欢
    • 2013-07-02
    • 2016-08-02
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多