在滚动视图中不触发事件

时间:2022-08-10 00:01:47

UIButtons inside scrollview are not firing event. I tried setting exclusiveTouch delaysTouchesEvent, but nothing helped.

滚动视图中的uibutton没有触发事件。我试着设置exclusive sivetouch 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)
    }
}

在滚动视图中不触发事件

2 个解决方案

#1


1  

Please make the following changes:

请做以下更改:

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

In your for-loop, move your let buttonView = UIView() to:

在for循环中,将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()

Then subsequently, change the following, again, in your for-loop:

然后,在你的for循环中,再次更改以下内容:

Change this line: buttonView.frame.origin = CGPointMake(0,0) to

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

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

Addendum:

附录:

Comment this out: //self.scrollView.exclusiveTouch = true

评论一下:/ / self.scrollView。exclusiveTouch = true

I took the liberty of testing it; touches should get registered on the buttons and scrolling should also work horizontally. Enjoy.

我冒昧地测试了一下;触摸应该在按钮上注册,滚动也应该是水平的。享受。

#2


1  

A UIButton will not accept touches if it lies outside the bounds of one of its superviews. Make sure all your views are the size and position you expect them to be

如果UIButton不在它的一个超视图的范围之内,它将不接受触摸。确保你所有的观点都是你所期望的大小和位置。

#1


1  

Please make the following changes:

请做以下更改:

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

In your for-loop, move your let buttonView = UIView() to:

在for循环中,将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()

Then subsequently, change the following, again, in your for-loop:

然后,在你的for循环中,再次更改以下内容:

Change this line: buttonView.frame.origin = CGPointMake(0,0) to

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

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

Addendum:

附录:

Comment this out: //self.scrollView.exclusiveTouch = true

评论一下:/ / self.scrollView。exclusiveTouch = true

I took the liberty of testing it; touches should get registered on the buttons and scrolling should also work horizontally. Enjoy.

我冒昧地测试了一下;触摸应该在按钮上注册,滚动也应该是水平的。享受。

#2


1  

A UIButton will not accept touches if it lies outside the bounds of one of its superviews. Make sure all your views are the size and position you expect them to be

如果UIButton不在它的一个超视图的范围之内,它将不接受触摸。确保你所有的观点都是你所期望的大小和位置。