Swift:在UIScrollView

时间:2016-02-06 16:42:25

标签: ios swift uiscrollview

我有一个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






    }

}

4 个答案:

答案 0 :(得分:1)

原来在这里,我想到了这条线

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

应该说“按下”没有“:”。但这对我来说是一种误解,对不起。应该“按下:”。

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

答案 1 :(得分:1)

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

Inferred ("Constraints") option in Storyboard

答案 2 :(得分:0)

您是否尝试在按钮中添加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)

然后编写一个函数(函数的名称应该是你在上面的action字段中放置的。所以在这种情况下函数将被称为handleButtonTap。这是代码:

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

我希望我能够提供帮助:)。

答案 3 :(得分:0)

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

带有scrollview的

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

    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等)

我希望它有所帮助!