This could possibly be an Xcode bug? I have an UIImage inside a UIScrollView done programatically in code. The user can zoom into the image using the zoom gesture or double tapping the image. This part all works fine.
Next, I am trying to detect when the scroll view has finished zooming after a zoom gesture or double tap using the below function scrollViewDidEndZooming. However, scrollViewDidEndZooming never gets called, and doesn't print the line I just finished zooming!
Code not working:
func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}
Question:
What might be going on here, why isn't scrollViewDidEndZooming(...)
getting called?
Note: I've referred to the below document with no luck.
https://developer.apple.com/reference/uikit/uiscrollviewdelegate/1619407-scrollviewdidendzooming
Complete code:
class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
override func viewWillLayoutSubviews() {
setZoomScale()
}
func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}
func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func scrollViewDidEndZooming(scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}
You make a small mistake. Try this code:
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
print("I just finished zooming!")
}
Related
Simply I have UIScrollView with one view for zooming inside that view: UIImageView.
This is how it looks when I just display the image:
When I pinch:
And when I swipe down after pinch:
And now I have black space I do not want to see. Is there a way to set some offsets and prevent from scrolling far after the image?
In code I simply use:
//MARK: - UIScrollViewDelegate
public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return photoImageView
}
Simply implement UIScrollViewDelegate:
//MARK: - UIScrollViewDelegate
public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return photoImageView
}
public func scrollViewDidZoom(_ scrollView: UIScrollView) {
let imageViewSize = photoImageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
if verticalPadding >= 0 {
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
} else {
scrollView.contentSize = imageViewSize
}
}
and prepare view on viewDidLoad:
private func setupInitialView() {
photoImageView.sizeToFit()
let imageViewSize = photoImageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: false)
scrollViewDidZoom(scrollView)
}
The below Swift code manages zooming in and out of an UIImage inside a UIScrollView.
When double tapping, the image zooms into the centre and zooms out to the centre.
Question:
What code changes need to be made to set the zoom in point to be the centre of the image area the user touches on screen?
(For example, if the user double taps the top left of the image, the image would correspondingly zoom into the top left of the image.)
class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
override func viewWillLayoutSubviews() {
setZoomScale()
}
func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}
func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}
According to this post. It works fine with me.
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
let touchPoint = recognizer.locationInView(view)
let scrollViewSize = scrollView.bounds.size
let width = scrollViewSize.width / scrollView.maximumZoomScale
let height = scrollViewSize.height / scrollView.maximumZoomScale
let x = touchPoint.x - (width/2.0)
let y = touchPoint.y - (height/2.0)
let rect = CGRectMake(x, y, width, height)
scrollView.zoomToRect(rect, animated: true)
}
}
Here's a Swift 2.3 function for returning the location of a touch inside a zoomable UIScrollView:
UIScrollView with UIImageView inside
Zoomed or not - scrollView.zoomScale
imageView.contentMode = .ScaleAspectFit
The image can be bigger, high res than imageView and scrollView
Any shape of the image, portrait, landscape or square
imageView.contentMode = .ScaleAspectFit
//import AVFoundation // needed for AVMakeRectWithAspectRatioInsideRect()
func getLocationOfTouchInImageInScrollView(paintLocation:CGPoint)->CGPoint {
let imageSize = imageViewInScrollView.image!.size
let imageFrame = scrollView.frame
let imageRect = AVMakeRectWithAspectRatioInsideRect(imageSize, imageFrame)
let imageHeightToViewHeight = max(imageSize.height, imageSize.width) / imageFrame.size.height
let px = (max(0, min(imageSize.width, ((paintLocation.x - imageRect.origin.x) * imageHeightToViewHeight))))
let py = (max(0, min(imageSize.height, ((paintLocation.y - imageRect.origin.y ) * imageHeightToViewHeight))))
var imageTouchPoint = CGPointMake(CGFloat(px), CGFloat(py))
return imageTouchPoint
}
Took me DAYS to write this one since I couldn't figure out the imageHeightToViewHeight variable, thought there was something else funny going on.
I have a UIViewController that is attached to a class, Swift code below. The code basically adds to the View Controller a UIScrollView with an image.
I am wanting to add a UIButton to the ViewController which I could do through code, however in this case, I want to add the UIButton to the ViewController using the Storyboard.
When I add a UIButton and then run my project, the UIButton is not visible, only the UIScrollView is visible.
Question:
What is going on, why is the UIButton not visible? How can I add a UIButton (to the Storyboard) and ensure that it is visible and in front of the UIScrollView (that is created programatically) when I run the project?
class ScrollViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView!
var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView = UIImageView(image: UIImage(named: "image.png"))
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = imageView.bounds.size
scrollView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(imageView)
view.addSubview(scrollView)
scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
override func viewWillLayoutSubviews() {
setZoomScale()
}
func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
scrollView.zoomScale = 1.0
}
func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
}
Your scrollview is blocking the UIButton on z axis, please use
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;
to insert scrollview below UIButton
Swift:
func insertSubview(view: UIView, atIndex index: Int) {
}
Answer:
Instead of this line view.addSubview(scrollView), you need to do self.view.insertSubview(scrollView, atIndex: 0) and make sure UIButton is above the scrollView.
You have to write:
self.view.insertSubview(scrollview, belowSubview: button)
Having a image array(Download from server) which i want to show in scrollview with paging and zooming enable but i didn't find any solution. I am searching a lot but i didn't get any solution.
Any help is really appreciating.
Thanks!
In MainController
var mainScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
var x:CGFloat = 0
mainScrollView = UIScrollView(frame: view.bounds)
mainScrollView.pagingEnabled = true
let images=["image.png","photo.jpg","3.png"]
for var i = 0; i < images.count; i++
{
let imageView = UIImageView(image: UIImage(named: images[i]))
let scrollView = SlideShow(image: imageView, scrollFrame: CGRectMake(x, view.bounds.origin.y, view.bounds.width, view.bounds.height))
x = x + UIScreen.mainScreen().bounds.size.width
mainScrollView.addSubview(scrollView)
}
mainScrollView.contentSize=CGSizeMake(x, self.mainScrollView.frame.size.height);
mainScrollView.contentOffset=CGPointMake(0, 0);
view.addSubview(mainScrollView)
// Do any additional setup after loading the view.
}
In SubClass
public class SlideShow: UIScrollView, UIScrollViewDelegate {
public var imageView = UIImageView()
// MARK: - Life cycle
init(image: UIImageView, scrollFrame: CGRect) {
super.init(frame: scrollFrame)
//image.setToImageView(imageView)
imageView = image
imageView.clipsToBounds = true
imageView.userInteractionEnabled = true
// UIScrollView(frame: bounds)
delegate = self
showsVerticalScrollIndicator = false
showsHorizontalScrollIndicator = false
backgroundColor = UIColor.blackColor()
// contentSize = imageView.bounds.size
// autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
contentOffset = CGPoint(x: 1000, y: 450)
addSubview(imageView)
// setZoomScale()
setupGestureRecognizer()
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (zoomScale > minimumZoomScale) {
setZoomScale(minimumZoomScale, animated: true)
} else {
setZoomScale(maximumZoomScale, animated: true)
}
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func layoutSubviews() {
setZoomScale()
//setZoomScale(minimumZoomScale, animated: false)
}
public func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageView
}
func setZoomScale() {
let imageViewSize = imageView.bounds.size
let scrollViewSize = bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
minimumZoomScale = min(widthScale, heightScale)
zoomScale = 1.0
}
public func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = imageView.frame.size
let scrollViewSize = bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
}
Solution 1 :- Agrume Library with load photo url.
There are multiple ways you can use the image viewer (and the included Example project shows them all).
This one is best library for that.
Solution 2 :-
This library provide multiple images and zoom also.
I've been building a UIScrollView with GestureRecognizer to place UIImageView inside UIScrollView scaled to the center of the UIScrollView. but i am facing a little problem with scaling the Image to fit inside UIScrollView. i have to zoom with finger then the image comes from the left side(out of the screen) of the screen phone to the center. but i just wanted the image to be scaled to be fit when my UIViewController loads.
So here is my code viewDidLoad:
scrollView = UIScrollView(frame: view.bounds)
scrollView.backgroundColor = UIColor.blackColor()
scrollView.contentSize = myimageView.bounds.size
scrollView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
scrollView.contentOffset = CGPoint(x: 1000, y: 450)
scrollView.addSubview(myimageView)
view.addSubview(scrollView)
scrollView.delegate = self
setZoomScale()
setupGestureRecognizer()
And the other methods to Scale and zoom :
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return myimageView
}
override func viewWillLayoutSubviews() {
setZoomScale()
}
func scrollViewDidZoom(scrollView: UIScrollView) {
let imageViewSize = myimageView.frame.size
let scrollViewSize = scrollView.bounds.size
let verticalPadding = imageViewSize.height < scrollViewSize.height ? (scrollViewSize.height - imageViewSize.height) / 2 : 0
let horizontalPadding = imageViewSize.width < scrollViewSize.width ? (scrollViewSize.width - imageViewSize.width) / 2 : 0
scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func setZoomScale() {
let imageViewSize = myimageView.bounds.size
let scrollViewSize = scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
scrollView.minimumZoomScale = min(widthScale, heightScale)
// self.scrollView.sizeToFit()
scrollView.zoomScale = 0
}
func setupGestureRecognizer() {
let doubleTap = UITapGestureRecognizer(target: self, action: "handleDoubleTap:")
doubleTap.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(doubleTap)
}
func handleDoubleTap(recognizer: UITapGestureRecognizer) {
if (scrollView.zoomScale > scrollView.minimumZoomScale) {
scrollView.setZoomScale(scrollView.minimumZoomScale, animated: true)
} else {
scrollView.setZoomScale(scrollView.maximumZoomScale, animated: true)
}
}
For some images which the height for it is large this issue happened but not for squared images. and i think it can be fixed from here : scrollView.minimumZoomScale but i can't make it right !