如何使用Swift中的页面控件从ScrollView将图像传递给NewViewController

时间:2017-09-21 21:30:58

标签: swift uiscrollview uipagecontrol

我正在尝试使图像可以点击并将它们传递给具有更多细节的新视图控制器。我在HomeViewController中使用UIScrollView和UIPageController。 我试图研究,但找不到任何具体的例子。我想也许可以用segue传递它,但我无法弄清楚如何。欢迎任何想法。提前致谢。这是我的代码:

class HomeViewController: UIViewController, UIScrollViewDelegate {

@IBOutlet var pageControl: UIPageControl!
@IBOutlet var topScrollView: UIScrollView!

 override func viewDidLoad() {
    super.viewDidLoad()

    pageScrollView()

   }

func pageScrollView() {

    self.topScrollView.isUserInteractionEnabled = true
    self.topScrollView.addGestureRecognizer(UITapGestureRecognizer())

    self.topScrollView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 200)
    let scrollViewWidth:CGFloat = self.topScrollView.frame.width
    let scrollViewHeight:CGFloat = self.topScrollView.frame.height


    let imgOne = UIImageView(frame: CGRect(x:0, y:0,width:scrollViewWidth, height:scrollViewHeight))
    imgOne.image = UIImage(named: "img1")        
    let imgTwo = UIImageView(frame: CGRect(x:scrollViewWidth, y:0,width:scrollViewWidth, height:scrollViewHeight))
    imgTwo.image = UIImage(named: "img2")
    let imgThree = UIImageView(frame: CGRect(x:scrollViewWidth*2, y:0,width:scrollViewWidth, height:scrollViewHeight))
    imgThree.image = UIImage(named: "img3")
    let imgFour = UIImageView(frame: CGRect(x:scrollViewWidth*3, y:0,width:scrollViewWidth, height:scrollViewHeight))
    imgFour.image = UIImage(named: "img4")
    let imgFive = UIImageView(frame: CGRect(x:scrollViewWidth*4, y:0,width:scrollViewWidth, height:scrollViewHeight))
    imgFive.image = UIImage(named: "img5")


    self.topScrollView.addSubview(imgOne)
    self.topScrollView.addSubview(imgTwo)
    self.topScrollView.addSubview(imgThree)
    self.topScrollView.addSubview(imgFour)
    self.topScrollView.addSubview(imgFive)

    self.topScrollView.contentSize = CGSize(width:self.topScrollView.frame.width * 5, height:self.topScrollView.frame.height)
    self.pageControl.currentPage = 0

  }

    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    let pageWidth:CGFloat = scrollView.frame.width
    let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1

    // Change the indicator
    self.pageControl.currentPage = Int(currentPage)


   }
}

1 个答案:

答案 0 :(得分:0)

我明白了。希望它可以帮助别人。我补充说:

@objc var imgOne = UIImageView()
@objc var imgTwo = UIImageView()
@objc var imgThree = UIImageView()
@objc var imgFour = UIImageView()
@objc var imgFive = UIImageView()

   @objc func imageTapped(_sender:UITapGestureRecognizer) {

     let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailsMapsViewController") as! DetailsMapsViewController
     vc.restaurantMaps = self.restaurantArray[_sender.view!.tag]
     self.navigationController?.pushViewController(vc, animated: true)
}

    imgOne.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(HomeViewController.imageTapped)))
    imgTwo.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(HomeViewController.imageTapped)))
    imgThree.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(HomeViewController.imageTapped)))
    imgFour.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(HomeViewController.imageTapped)))
    imgFive.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(HomeViewController.imageTapped)))

    imgOne.tag = 0
    imgTwo.tag = 1
    imgThree.tag = 2
    imgFour.tag = 3
    imgFive.tag = 4