gpt4 book ai didi

ios - 如何使用 ScrollView 和 pageController 显示图像和信息

转载 作者:行者123 更新时间:2023-11-28 12:16:20 26 4
gpt4 key购买 nike

我想在 UIScrollView 和 pageViewController 中显示一些图像,因为每个图像都会有不同的信息,所以现在我可以做到这一点,但我想在更改图像时同时显示信息,我的意思是,当我将照片更改为另一张照片时,我想显示另一张照片和关于该照片的信息,我想在代码中显示的信息是一个名为“labelList”的数组,我显示的图像名为 imageList。

class PremiosViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet var myImageView: UIImageView!
let imagelist = ["img_1", "img_2", "img_3"]
let labelList = ["label_1", "label_2", "label_3"]

var scrollView = UIScrollView()

var pageControl : UIPageControl = UIPageControl(frame:CGRect(x: 50, y: 250, width: 200, height: 50))

var yPosition:CGFloat = 0
var scrollViewContentSize:CGFloat=0;

override func viewDidLoad() {
super.viewDidLoad()

scrollView = UIScrollView(frame: CGRect(x: 0, y: 270, width: self.view.frame.width, height: 300))
configurePageControl()

scrollView.delegate = self
self.view.addSubview(scrollView)
for i in stride(from: 0, to: imagelist.count, by: 1) {
var frame = CGRect.zero
frame.origin.x = self.scrollView.frame.size.width * CGFloat(i)
frame.origin.y = -100
frame.size = self.scrollView.frame.size
self.scrollView.isPagingEnabled = true

let myImage:UIImage = UIImage(named: imagelist[i])!
let myImageView:UIImageView = UIImageView()
myImageView.image = myImage
myImageView.contentMode = UIViewContentMode.scaleAspectFit
myImageView.frame = frame

scrollView.addSubview(myImageView)
}

self.scrollView.contentSize = CGSize(width: self.scrollView.frame.size.width * CGFloat(imagelist.count), height: self.scrollView.frame.size.height)
pageControl.addTarget(self, action: Selector(("changePage:")), for: UIControlEvents.valueChanged)
pageControl.addTarget(self, action: Selector(("changePage:")), for: UIControlEvents.valueChanged)
// Do any additional setup after loading the view.
}
func configurePageControl() {
// The total number of pages that are available is based on how many available colors we have.
self.pageControl.numberOfPages = imagelist.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.clear
self.pageControl.pageIndicatorTintColor = UIColor.clear
self.pageControl.currentPageIndicatorTintColor = UIColor.clear
self.view.addSubview(pageControl)

}

// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL
func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x: x,y :0), animated: true)
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func clickBackButton(_ sender: Any) {

self.dismiss(animated: true, completion: nil)
}
}

最佳答案

如果只想在 UILabel 中显示信息,那么您可以使用与添加多个 UIImageView 类似的方法来完成,您可以添加还有一个 UILabel

例如在您编写的代码中:

let myImage:UIImage = UIImage(named: imagelist[i])!
let myImageView:UIImageView = UIImageView()
myImageView.image = myImage
myImageView.contentMode = UIViewContentMode.scaleAspectFit
myImageView.frame = frame

// TODO: I added this here

scrollView.addSubview(myImageView)

我在那里添加了一个 TODO 注释只是为了告诉你你可以在哪里添加你的 UILabel 也可以这样做:

let myLabel = UILabel();
myLabel.text = labelList[i]
myLabel.textAlignment = .center
var labelFrame = CGRect(x: self.scrollView.frame.size.width * CGFloat(i), y: frame.size.height - 100, width: frame.size.width, height: 100)
myLabel.frame = labelFrame

在您的代码行 scrollView.addSubview(myImageView) 下面,您还可以添加您的 UILabel:

scrollView.addSubview(myLabel)

关于ios - 如何使用 ScrollView 和 pageController 显示图像和信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46436949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com