Swift:如何在Scroll View中使用按钮

时间:2016-02-04 05:43:37

标签: ios swift scrollview

我用“按钮”创建了一个滚动视图。但是“按钮”实际上目前没有做任何事情。所以,我想知道,我如何使“按钮”可用。因此,当我按下它们时,它们会引导我到另一个视图控制器。所有按钮都会引导我到同一个视图控制器,只是在视图控制器内有不同的数据(取决于我想要那个按钮代表什么)。到目前为止,这是我的代码:

     @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()]
    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 = colorButtonsView(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)
    }


    func colorButtonsView(buttonSize:CGSize, buttonCount:Int) -> UIView {
        let buttonView = UIView()
        buttonView.frame.origin = CGPointMake(50,300)
        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)
            button.frame.size = buttonSize
            button.frame.origin = buttonPosition
            buttonPosition.x = buttonPosition.x + buttonIncrement
            button.setTitle(categoryArr[i], forState: UIControlState.Normal)
            button.backgroundColor = buttonColors[i]
            buttonView.addSubview(button)
        }
        buttonView.backgroundColor = UIColor.redColor()
        return buttonView
    }
}
extension ViewController:UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {

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

}

2 个答案:

答案 0 :(得分:2)

创建按钮时,必须指定一个目标才能对其执行操作(在for循环中添加此项):

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

然后创建相应的方法

 func myaction(sender: UIButton!) {
// navigation code here.
}

请注意,您不必创建多种方法,以便为每个按钮执行不同的操作,您只需要执行以下操作:

 button.tag = i

然后在你的行动方法中:

switch sender.tag {

 case 0 : 
 //do some stuff

 case 1 :
 //do some stuff

 } 

答案 1 :(得分:0)

您必须向按钮添加目标以触发某些操作方法。就像在创建UIButton时在循环中一样,添加

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Test dynamic ngModel</title>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.29/angular.js"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.29/angular-sanitize.js"></script>
  <script type="text/javascript" src="script.js"></script>
</head>

<body ng-app="myApp">
  <div ng-controller="MyCtrl">
    <div grid="" grid-config="myGridConfig"></div>
    <div>Row 2 selected: {{myGridConfig.rowCollection[1].selected}}</div>
  </div>
</body>

</html>

并实现一个方法,如:

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