Swift:按钮在 UIScrollView 中不起作用

Posted

技术标签:

【中文标题】Swift:按钮在 UIScrollView 中不起作用【英文标题】:Swift: Buttons not working inside UIScrollView 【发布时间】:2016-02-06 16:42:25 【问题描述】:

我有一个 ScrollView。您可以滚动,但按钮不允许您按下它们(当您按下它们时,它们不会做任何事情)。你能帮忙解决这个问题吗?这是我当前的代码:

PS:我会定期检查,如果您有任何问题,我会回答。

class ViewController: UIViewController 





    @IBOutlet weak var categoryScrollView: UIScrollView!




    var categoryArr = ["Button1","Button2","Button3","Button4","Button5", "Button 6", "Button 7", "Button 8", "Button 9", "Button 10", "Button 11", "Button 12"]
    var buttonColors = [UIColor.greenColor(), UIColor.blueColor(), UIColor.blackColor(), UIColor.cyanColor(), UIColor.magentaColor(), UIColor.greenColor(), UIColor.blueColor(), UIColor.blackColor(), UIColor.cyanColor(), UIColor.magentaColor(), UIColor.blackColor(), UIColor.brownColor()]
    var buttonImages = [UIImage(named: "One"), UIImage(named: "Two"), UIImage(named: "Three"), UIImage(named: "PlayButtonImage"), UIImage(named: "Triangle"), UIImage(named: "PlayButtonImage"), UIImage(named: "Triangle"), UIImage(named: "PlayButtonImage"), UIImage(named: "Triangle"), UIImage(named: "PlayButtonImage"), UIImage(named: "Triangle"), UIImage(named: "PlayButtonImage")]


    let kPadding:CGFloat = 20


    override func viewDidLoad() 
        super.viewDidLoad()



        let buttonSize = CGSizeMake(categoryScrollView.bounds.size.width/2, categoryScrollView.bounds.size.height/2)//hal

        let scrollingView = ImageButtonsView(buttonSize, buttonCount: 12)
        categoryScrollView.contentSize = scrollingView.frame.size
        categoryScrollView.addSubview(scrollingView)
        categoryScrollView.showsVerticalScrollIndicator = false
        categoryScrollView.delegate = self
        categoryScrollView.pagingEnabled = true
        categoryScrollView.indicatorStyle = .Default
        categoryScrollView.contentOffset = CGPointMake(0, 0)
        categoryScrollView.delaysContentTouches = false
        categoryScrollView.userInteractionEnabled = true;
        categoryScrollView.exclusiveTouch = true;
        categoryScrollView.canCancelContentTouches = true;
        //categoryScrollView.clipsToBounds = true

    


    func ImageButtonsView(buttonSize:CGSize, buttonCount:Int) -> UIView 




        let buttonView = UIView()
        buttonView.frame.origin = CGPointMake(50,142)
        let padding = CGSizeMake(kPadding, kPadding)
        buttonView.frame.size.width = (buttonSize.width + padding.width) * CGFloat(buttonCount)
        var buttonPosition = CGPointMake(0, padding.height)
        let buttonIncrement = buttonSize.width + padding.width
        for i in 0...(buttonCount - 1)  


            let button = UIButton(type: .Custom)

            buttonView.userInteractionEnabled = true

            button.frame.size = buttonSize
            button.frame.origin = buttonPosition
            buttonPosition.x = buttonPosition.x + buttonIncrement
            button.setTitle(categoryArr[i], forState: UIControlState.Normal)
            let buttonImagesOne = buttonImages[i]
            button.setImage(buttonImagesOne, forState: .Normal)
            button.layer.cornerRadius = 30
            button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)
            buttonView.addSubview(button)









        
        //buttonView.backgroundColor = UIColor.redColor()
        categoryScrollView.bringSubviewToFront(buttonView)


        return buttonView

    







extension ViewController:UIScrollViewDelegate
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) 





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



    func pressed(/*sender: UIButton!*/) 

        let vc = UIViewController(nibName: "GridViewController", bundle: nil)
        self.presentViewController(vc, animated: true, completion:nil)

        //viewController.hidden = true






    

【问题讨论】:

尝试将按钮的exclusiveTouch属性设置为true nick ..同样的问题仍然没有接触 【参考方案1】:

原来这里,我还以为行了

button.addTarget(self, action: "pressed:", forControlEvents: UIControlEvents.TouchUpInside)

应该说“按下”而不带“:”。但事实证明这是我的一个误解,对不起。它应该是“按下:”。

相反,这可能与Protocol extension and addTarget is crashing with NSInvalidArgumentException 有关,这也很令人困惑。

【讨论】:

这样做(以及 EmilioPalaez 所说的),按钮在按下时仍然没有任何作用,正如我之前所说,您可以滚动但不能单击按钮。 如果你在pressed()函数中放一个日志语句,它会记录吗?功能达到了吗? 你能举个例子说明你在说什么吗? print("pressed!")。另外,我认为您需要在此函数中添加一个参数。查看编辑后的答案。 使用您的建议后,我将按下功能中的发件人更改为“UIButton”,然后当这不起作用时,我将其更改为“AnyObject”,其中也不起作用。但我也添加了“print('pressed')”行,当我尝试点击按钮时,该按钮从未显示,暗示该功能可能尚未达到。【参考方案2】:

原来我的滚动视图的布局设置为“自动调整掩码”而不是“推断(约束)”。虽然我的布局看起来不错,但我的按钮实际上超出了滚动视图的范围,因此无法点击!

【讨论】:

【参考方案3】:

您是否尝试将UITapGestureRecognizer 添加到您的按钮?你可以试试这样的:

let myTap = UITapGestureRecognizer(target: self, action: Selector("handleButtonTap:"))
myTap.delegate = self
//**Make your class inherit from UIGestureRecognizerDelegate otherwise an error will be thrown**
myButton.addGestureRecognizer(myTap)

然后编写一个函数(函数的名称应该是您在上面的操作字段中输入的内容。所以在这种情况下,该函数将被称为handleButtonTap。这是代码:

func handleButtonTap() 
// Handle what happens when the button is tapped.

希望能帮到你:)。

【讨论】:

【参考方案4】:

这是我的方法(Swift 2 / xCode 7.2.1): 我有 2 个视图控制器:

带有滚动视图的“VC1” 在这里,我创建了不同的视图(“VC2”的实例)并将它们添加到滚动视图中:

    scrollView.addSubview(pageStep.view)
    addChildViewController(pageStep)

我称之为 page1, page2, page3 ... 然后你可以访问“page1.btn1”并做这样的事情

page1.nextBtn.addTarget(self, action: "nextPage:", forControlEvents: .TouchUpInside)

func nextPage(sender: UIButton!) 
        let xPosition = (pageFraction + 1) * scrollSize
        scrollView.setContentOffset(CGPoint(x: xPosition, y: 0), animated: true)

“VC2”与网点(每页)(UIImage、Buttons 等)

希望对你有帮助!

【讨论】:

以上是关于Swift:按钮在 UIScrollView 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 通过点击 ScrollView 内的 ViewController 内的按钮来滚动 UIScrollView

UITextField 触摸事件在 UIScrollView 中不起作用

如何使用 swift 填充我放置在 UITableCell 中的整个 UIScrollView

关闭按钮在 Swift Playground 中不起作用

Swift - UIScrollView 内的 UICollectionView - 自动布局

在 UIScrollView Swift3 中加载 UIViewController