图片只能全屏显示一次

时间:2018-06-24 01:17:41

标签: ios swift uiimageview

当用户点击FSPagerView中显示的图像时,图像全屏显示,这正是我想要的。但是,一旦再次触摸屏幕并关闭了全屏图像,再次触摸该图像将无法全屏显示。我将不得不离开视图并返回页面以使其能够全屏显示。是什么导致图像不能全屏显示超过一次?

 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()
}

1 个答案:

答案 0 :(得分:1)

tapGestureRecognizer添加到cellForItemAt中的图像中,并触发全屏图像:

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
}