0

in this I had implemented the swipe gestures on image view and it is embedded in scroll view but gestures are not working here is my code any solution for this ?

 collectionView.delegate = self
        collectionView.dataSource = self
        imageView.isUserInteractionEnabled = true
        let swipeLeft = UISwipeGestureRecognizer(target: self, action:#selector(swiped(gesture:)))
        swipeLeft.direction = .left
        self.imageView.addGestureRecognizer(swipeLeft)
        swipeLeft.cancelsTouchesInView = false
        let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swiped(gesture:)))
        swipeRight.direction = .right
        self.imageView.addGestureRecognizer(swipeRight)
        swipeRight.cancelsTouchesInView = false
        imageView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))
        self.imageView.addGestureRecognizer(tap)

2 Answers2

0

If you want swipe and scrolling both at work concurrently you have to implement gesture recognizer delegate.

// here are those protocol methods with Swift syntax

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
    return true
}

May be you could try this.Don't forget to delegate as self. :)

elk_cloner
  • 1,843
  • 1
  • 9
  • 11
0

For your case, you want to have UI like Gallery View. Just Left and right swipe to change photo and tap/DoubleTap to zoom.

Don't need Swipe Gestures.

Just use Collection view with paging enabled. Each cell will display a single image. when Paging enabled, only a single cell will be shown at a time.

So just enable Paging enabled in collection view and try running.

If you want to zoom, when tapping a cell then, add TapGesture to CollectionView's parent UIScrollView and do write actions correspondingly. For this case, I've used custom collection view cell.

I've just added my custom cell code for your reference as given below.

class ImageViewerCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func awakeFromNib() {
    super.awakeFromNib()

    self.scrollView.minimumZoomScale = 1.0
    self.scrollView.maximumZoomScale = 6.0
    self.scrollView.delegate = self
    self.scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
    self.scrollView.zoom(to: CGRect(origin: CGPoint.zero, size: scrollView.frame.size), animated: true)

    let doubleTap = UITapGestureRecognizer(target: self, action: #selector(doubleTapAction(_:)))
    doubleTap.numberOfTapsRequired = 2
    self.scrollView.addGestureRecognizer(doubleTap)
}

func setURL(imageURL : URL, needLoader : Bool) {

    DispatchQueue.main.async {
        self.scrollView.setZoomScale(self.scrollView.minimumZoomScale, animated: true)
        self.scrollView.zoom(to: CGRect(origin: CGPoint.zero, size: self.scrollView.frame.size), animated: true)

        if needLoader {
            self.activityIndicator.isHidden = false
            self.activityIndicator.startAnimating()
            self.imageView.pin_setImage(from: imageURL, completion: { (completed) in
                self.activityIndicator.stopAnimating()
                self.activityIndicator.isHidden = true
            })
        }
        else {
            self.activityIndicator.isHidden = true
            self.imageView.pin_setImage(from: imageURL, placeholderImage: placeHolderImage)
        }
        self.imageView.pin_updateWithProgress = true
    }
}

@IBAction func doubleTapAction(_ sender: Any) {

    if scrollView.zoomScale == scrollView.minimumZoomScale {

        let touchPoint = (sender as! UITapGestureRecognizer).location(in: scrollView)
        let scale = min(scrollView.zoomScale * 3, scrollView.maximumZoomScale)

        let scrollSize = scrollView.frame.size
        let size = CGSize(width: scrollSize.width / scale,
                          height: scrollSize.height / scale)
        let origin = CGPoint(x: touchPoint.x - size.width / 2,
                             y: touchPoint.y - size.height / 2)
        scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true)
    }
    else {
        scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
        scrollView.zoom(to: CGRect(origin: CGPoint.zero, size: scrollView.frame.size), animated: true)
    }
}
}

extension ImageViewerCollectionViewCell : UIScrollViewDelegate {

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return self.imageView
}
}

As given above, I've used a imageview inside UIScrollView. So when you double tapped that scroll view, scrollview will zoom in & out.

Hope you understand and hope it helps for sure.

Balaji Ramakrishnan
  • 1,713
  • 9
  • 19
  • not this actually when u see a product details page in example Flipkart like that I was trying to display so for recently viewed products I was using collection view –  May 24 '17 at 14:00