在多个页面控件上显示数组的单个元素

时间:2018-03-19 02:57:49

标签: ios arrays swift element uipagecontrol

以下代码使用页面控件显示图像。我想使用相同的循环在每个页面上显示数组的单个元素。现在,代码在所有页面上显示a,b,c。我希望它只显示一个字母,所以第1页,第2页,b等。

@IBOutlet var lz: UILabel!
var judo = ["a","b","c"]
var output = ""

override func viewDidLoad() {
    super.viewDidLoad()
    scrol.delegate = self

    for image in 0...2 {
        output += " \(judo[image])"

        let imageTo = UIImage(named: "\(image).png")
        let imageView = UIImageView(image: imageTo)
        let xCord = view.frame.midX + view.frame.width * CGFloat(image)
        contenetWidth += view.frame.width
        scrol.addSubview(imageView)
    }
    lz.text = output
}

2 个答案:

答案 0 :(得分:1)

使用UIScrollViewDelegate方法scrollViewWillEndDragging

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
        let index = targetContentOffset.pointee.x / view.frame.width
        self.lz.text = self.judo[Int(index)]
}

答案 1 :(得分:0)

试试这个

 for image in 0...2 {

        output += " \(judo[image])"

        ...

        let xCord = view.frame.midX + view.frame.width * CGFloat(image)
        let label = UILabel.init(frame: CGRect(x:xCord,y:0,width:50,height:30))
        label.text = output
        scrol.addSubview(label)

        ...
 }