在多个页面控件上显示数组的单个元素
Display single element of array on multiple page controls
以下代码是使用页面控件显示图片。我想使用相同的循环在每个页面上显示数组的单个元素。现在代码在所有页面上显示 a、b、c。我希望它只显示一个字母,所以第 1 页 a 页 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
}
试试这个
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)
...
}
使用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)]
}
以下代码是使用页面控件显示图片。我想使用相同的循环在每个页面上显示数组的单个元素。现在代码在所有页面上显示 a、b、c。我希望它只显示一个字母,所以第 1 页 a 页 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
}
试试这个
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)
...
}
使用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)]
}