Reputation: 121
I have a scroll view with some images in it, which i scroll horizontally. I want to zoom using pinch gestures. This is the code I'm using, but it isn't working. I can't seem to figure out what I'm doing wrong. Any suggestions please?
let scrollView = UIScrollView(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.size.width , height: (UIScreen.main.bounds.size.height - 140))) var frame: CGRect = CGRect(x: 0, y: 0, width: 0, height: 0)
var subView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
getOfferImages()
swipeGesture()
setupView()
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 4.0
}
func configureScrollView(){
scrollView.backgroundColor = UIColor.black
scrollView.showsHorizontalScrollIndicator = false
scrollView.isUserInteractionEnabled = true
scrollView.clipsToBounds = true
scrollView.bouncesZoom = true
pageNumberlabel.text = "1 of \(offersImagesArray.count)"
self.view.addSubview(scrollView)
for index in 0..<images.count {
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.size = self.scrollView.frame.size
subView = UIImageView(frame: frame)
subView.tag = 1
subView.contentMode = .scaleToFill
subView.isUserInteractionEnabled = true
subView.sd_setImage(with: images[index], placeholderImage: UIImage(named:""), options: [.continueInBackground])
self.scrollView.addSubview(subView)
}
self.scrollView.isPagingEnabled = true
self.scrollView.contentSize = CGSize(width: (self.scrollView.frame.size.width * CGFloat(images.count)), height: self.scrollView.frame.size.height)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
// let image = self.view.findSubview(withTag: 1)
return self.subView
}
Upvotes: 1
Views: 4512
Reputation: 23
1) set zoom scales
scrollView.maximumZoomScale = 10
scrollView.minimumZoomScale = 1
2) set up scroll view delegate, return your image in viewForZooming
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return myImageView
}
Upvotes: 1
Reputation: 7168
You need to added PinchGestureRecognizer
to your storyboard then link it to a function like below.
@IBAction func scaleImage(_ sender: UIPinchGestureRecognizer) {
self.subView.transform = self.subView.transform.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
}
Note: subView
should be replaced by the view you are attempting to scale.
Upvotes: 1