2017-09-26 58 views
0

我想在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) 
    } 
} 
+0

類似於你一個UIImageView添加到滾動視圖的方式,你能不能添加一個UILabel呢? –

+0

嗯,我試過,但我可以做到。 – delbertoms

回答

0

您可以使用UICollectionView顯示內部UICllectionViewCell影像和信息水平滾動。

+0

是的,我認爲,但在應用程序設計是不是這樣,所以我不得不適應代碼。 :( – delbertoms

1

如果想顯示在短短的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) 
+0

謝謝你現在在工作,我申請你的例子,它的工作正常。再次感謝。 – delbertoms