我想在UIScrollView和pageViewController中顯示一些圖像,因爲每個圖像都有不同的信息,所以現在我能夠做到這一點,但我想顯示當我更改圖片時,我的意思是當我將照片更改爲另一張照片時,我想要顯示其他照片和關於該照片的信息,我想要在代碼中顯示的信息是名爲「labelList」的數組和我顯示的圖像命名爲imageList。如何使用滾動視圖和pageController顯示圖像和信息
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)
}
}
類似於你一個UIImageView添加到滾動視圖的方式,你能不能添加一個UILabel呢? –
嗯,我試過,但我可以做到。 – delbertoms