0

When a user taps on the image that is being displayed in FSPagerView, the image goes full screen which is exactly what I want. However once the screen is touched again and the full screen image is dismissed, upon touching the image again it will not go full screen. I will have to leave the view and go back to the page in order to enable it to go full screen. What is causing the image not to go full screen more than one time only?

 public func numberOfItems(in pagerView: FSPagerView) -> Int {
    return imagesArray.count
}
public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
    let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
    cell.imageView?.loadImageUsingCacheWithUrlString(urlString: imagesArray[index])
    cell.imageView?.contentMode = .scaleAspectFill
    return cell
}

func pagerView(_ pagerView: FSPagerView, shouldSelectItemAt index: Int) -> Bool {
    return true 
}

func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
    let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
    cell.imageView?.loadImageUsingCacheWithUrlString(urlString: imagesArray[index])
    cell.imageView?.contentMode = .scaleAspectFill

    let imageView = cell.imageView as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = UIScreen.main.bounds
    newImageView.backgroundColor = .black
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
}

@objc func dismissFullscreenImage(sender: UITapGestureRecognizer) {
     sender.view?.removeFromSuperview()
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
 //   sender.view?.removeFromSuperview()
}
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
ppApp
  • 11
  • 4
  • Why are you dequeueing the cell in `didSelectItemAt`? I don't know what a `FSPageView` is, but could you perhaps do something like `let cell = pagerView.cellForRow(at: index)` instead of the first 3 lines of `didSelectItemAt`? – vacawama Jun 24 '18 at 01:42
  • That code [looks familiar](https://stackoverflow.com/q/34694377/1630618) – vacawama Jun 24 '18 at 01:46
  • im not sure how else to get the image from that index, pagerView has no member cellForRow – ppApp Jun 24 '18 at 01:49

1 Answers1

1

Add a tapGestureRecognizer to your image in cellForItemAt and have that trigger the full screen image:

public func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
    let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
    cell.imageView?.loadImageUsingCacheWithUrlString(urlString: imagesArray[index])
    cell.imageView?.contentMode = .scaleAspectFill

    // Add a gesture recognizer the first time
    if cell.imageView?.gestureRecognizers?.count != 1 {
        let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
        cell.imageView?.isUserInteractionEnabled = true
        cell.imageView?.addGestureRecognizer(tap)
    }
    return cell
}

@objc func imageTapped(_ sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = UIScreen.main.bounds
    newImageView.backgroundColor = .black
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • the imageTapped function is not being called. I put a print statement right after cell.imageView?.addGestureRecognizer(tap), the print statement shows in console. However for some reason it won't call imageTapped. I have made sure userInteractionEnabled is on. – ppApp Jun 24 '18 at 02:39
  • Oh, you probably also need `cell.imageView?.isUserInteractionEnabled = true` – vacawama Jun 24 '18 at 02:41