scrolling not horizontal has content swift xcode uiscrollview zoom uiedgeinsets

swift - not - uiscrollview autolayout



UIScrollView Zooming & contentInset (1)

Tu enfoque parece correcto. Necesita actualizar su código de la siguiente manera.

func scrollViewDidZoom(scrollView: UIScrollView) { if scrollView.zoomScale > 1 { if let image = imageView.image { let ratioW = imageView.frame.width / image.size.width let ratioH = imageView.frame.height / image.size.height let ratio = ratioW < ratioH ? ratioW:ratioH let newWidth = image.size.width*ratio let newHeight = image.size.height*ratio let left = 0.5 * (newWidth * scrollView.zoomScale > imageView.frame.width ? (newWidth - imageView.frame.width) : (scrollView.frame.width - scrollView.contentSize.width)) let top = 0.5 * (newHeight * scrollView.zoomScale > imageView.frame.height ? (newHeight - imageView.frame.height) : (scrollView.frame.height - scrollView.contentSize.height)) scrollView.contentInset = UIEdgeInsetsMake(top, left, top, left) } } else { scrollView.contentInset = UIEdgeInsetsZero } }

Simliar a iOS Photos Aplicación donde el usuario está acercando y alejando una imagen pellizcando:

UIView> UIScrollView> UIImageView> UIImage

Inicialmente, tuve el problema de hacer zoom por debajo de la escala 1: la imagen estaba descentrada. Lo solucioné haciendo esto:

func scrollViewDidZoom(scrollView: UIScrollView) { let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0) let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0) scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0) }

Esto funciona bien cuando se aleja.

El modo de contenido UIImage es aspectFit

Problema

Cuando IZOOM IN , cuando zoomScale está por encima de 1, las inserciones de vista de desplazamiento deben abrazar los alrededores del UIImage que contiene la vista de desplazamiento. Esto quita el espacio muerto que rodeaba el UIImage. IE, la aplicación de fotos cuando se acerca el zoom pellizcando o tocando dos veces.

Intentó

func scrollViewDidZoom(scrollView: UIScrollView) { if scrollView.zoomScale > 1 { let imageScale = (self.imageView.bounds.width/self.imageView.image!.size.width) let imageWidth = self.imageView.image!.size.width * imageScale let imageHeight = self.imageView.image!.size.height * imageScale scrollView.contentInset = UIEdgeInsetsMake(((scrollView.frame.height - imageHeight) * 0.5), (scrollView.frame.width - imageWidth) * 0.5 , 0, 0) print (scrollView.contentInset.top) } else { let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, 0) let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, 0) scrollView.contentInset = UIEdgeInsetsMake(offsetY, offsetX, 0, 0) } }

Por encima de la suma, parece variar todavía la cantidad de inserción.

Actualización (imágenes agregadas)

La primera imagen muestra el diseño predeterminado. Descanso muestra cuando se acercó .....