I am creating an iOS application (swift) where I have multiple labels in a view controller. I am not using any scrollView. I need to zoom the view controller on a pinch gesture. I know I have found solutions with scroll view, but I don't want to implement scrollView in my view controller. Any help would be appreciated! Thank You.
UPDATED:
I have implemented a scrollView in view controller. I have added a view in scrollview and the view contains some labels, but the issues that I am facing is now are:
Zoom does not stay static. That is as soon as I release my fingers, the scrollview is automatically zoomed to original as it was.
When the view controller is in original state, the app crashes.
here is my code:
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet var viewThis: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
//let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
//self.view.addGestureRecognizer(gesture)
// Do any additional setup after loading the view.
let scrollViewFrame = scrollView.frame
let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
let minScale = min(scaleWidth, scaleHeight);
scrollView.minimumZoomScale = minScale
scrollView.maximumZoomScale = 1.0
scrollView.zoomScale = minScale;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return viewThis
}
Thanks to @YunCHEN for helping me with this. He gave me an idea what's going on in the code. I was facing an issue such that while zooming out, my app used to crash because it was getting out of limits when compared to the view width and height. I have updated an answer if somebody finds the same issue in future.
Step 1: Add a scrollView in view controller.
Step 2: Add a view on scrollView.
Step 3: Add all your items/objects from objects library on this view.
Step 4: Bind the scrollView and view to the view controller in code. Also add a UIScrollViewDelegate.
Step 5: Do as done below.
Here is the code:
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet var viewThis: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 3.0
scrollView.zoomScale = 1.0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return viewThis
}
Again thanks to @YunCHEN for helping me out!
UIView's transform property can make a view and its sub views smaller or bigger, like zooming effect. Example working with UIPinchGestureRecognizer:
class ExampleViewController: UIViewController {
var previousScale:CGFloat = 1.0
override func viewDidLoad() {
super.viewDidLoad()
let gesture = UIPinchGestureRecognizer(target: self, action: #selector(pinchAction(sender:)))
self.view.addGestureRecognizer(gesture)
}
func pinchAction(sender:UIPinchGestureRecognizer) {
let scale:CGFloat = previousScale * sender.scale
self.view.transform = CGAffineTransform(scaleX: scale, y: scale);
previousScale = sender.scale
}
}
And maybe you need to consider adjusting position of view after zooming, use CGAffineTransform(translationX: , y: ) to do that.
Still agree with that the UIScrollView is the easiest way to do zooming.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With