Cómo activar el zoom en UIScrollView


¿Cómo se puede activar el efecto de zoom en UIScrollView?

Author: Ideveloper, 2010-09-07

6 answers

Echa un vistazo a este video

Descripción del vídeo:

  • Añadir UIScrollViewDelegate delegar
  • Tome un UIScrollView
  • Tome un UIImageView que se va a agregar en Scrollview
  • Coloque la vista de desplazamiento en la Ventana Principal
  • Conecta a los delegados
  • Configurar ImageView
  • Establecer max / min escala de zoom[cosa central aquí]
  • Implementar el método delegado para devolver la vista
 10
Author: Jigar Joshi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-05-26 19:26:15

La respuesta es aquí :

La vista de desplazamiento también maneja el zoom y panorámica del contenido. Como usuario realiza un gesto de pellizco o pellizco, la vista de desplazamiento ajusta el desplazamiento y la escala del contenido. Cuando el gesto termina, el objeto de gestión de la la vista de contenido debería actualizarse subviews del contenido según sea necesario. (Tenga en cuenta que el gesto puede terminar y un el dedo aún podría estar abajo.) Mientras que el el gesto está en progreso, el pergamino ver does no enviar llamadas de seguimiento al sub-visor.

La clase UIScrollView puede tener delegate that must adopt the Protocolo UIScrollViewDelegate. Para zoom y panorámica para trabajar, el delegate debe implementar ambos Vista para zooming en ScrollView: y scrollviewdidzooming: Con vista: AtScale:; además, el máximo (maximumZoomScale) y mínimo ( minimumZoomScale) escala de zoom debe ser diferente.

Así que:

  1. Necesitas un delegado que implementa UIScrollViewDelegate y se establece en {[1] } en su instancia UIScrollView
  2. En tu delegado tienes que implementar un método: viewForZoomingInScrollView: (que debe devolver la vista de contenido que le interesa ampliar ). También puede implementar scrollViewDidEndZooming:withView:atScale: opcionalmente.
  3. En su instancia UIScrollView, tiene que establecer el minimumZoomScale y el maximumZoomScale para ser diferentes (son 1.0 por defecto).

Nota: lo interesante de esto es lo que si desea break zoom. Es ¿suficiente para devolver nil en el método viewForZooming...? Se rompe el zoom, pero algunos de los gestos se estropearán (para dos dedos). Por lo tanto, para romper el zoom debe establecer la escala de zoom mínimo y máximo a 1.0.

 178
Author: Dan Rosenstark,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-28 22:03:22

Lea este tutorial de Ray Wenderlich:

Http://www.raywenderlich.com/76436/use-uiscrollview-scroll-zoom-content-swift

Si sigue la sección 'Desplazamiento y Zoom de una imagen más grande' obtendrá una imagen y le permitirá pellizcar y hacer zoom.

En caso de que el enlace se altera, aquí está la información principal: Ponga este código en su controlador de vista (esto establece la funcionalidad principal):

override func viewDidLoad() {
  super.viewDidLoad()

  // 1
  let image = UIImage(named: "photo1.png")!
  imageView = UIImageView(image: image)
  imageView.frame = CGRect(origin: CGPoint(x: 0, y: 0), size:image.size)
  scrollView.addSubview(imageView)

  // 2
  scrollView.contentSize = image.size

  // 3
  var doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "scrollViewDoubleTapped:")
  doubleTapRecognizer.numberOfTapsRequired = 2
  doubleTapRecognizer.numberOfTouchesRequired = 1
  scrollView.addGestureRecognizer(doubleTapRecognizer)

  // 4
  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;

  // 5
  scrollView.maximumZoomScale = 1.0
  scrollView.zoomScale = minScale;

  // 6
  centerScrollViewContents()
}

Añadir esto a la clase:

func centerScrollViewContents() {
  let boundsSize = scrollView.bounds.size
  var contentsFrame = imageView.frame

  if contentsFrame.size.width < boundsSize.width {
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0
  } else {
    contentsFrame.origin.x = 0.0
  }

  if contentsFrame.size.height < boundsSize.height {
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0
  } else {
    contentsFrame.origin.y = 0.0
  }

  imageView.frame = contentsFrame
}

Y entonces esto si desea que el gesto de doble toque sea reconocido:

func scrollViewDoubleTapped(recognizer: UITapGestureRecognizer) {
  // 1        
  let pointInView = recognizer.locationInView(imageView)

  // 2
  var newZoomScale = scrollView.zoomScale * 1.5
  newZoomScale = min(newZoomScale, scrollView.maximumZoomScale)

  // 3
  let scrollViewSize = scrollView.bounds.size
  let w = scrollViewSize.width / newZoomScale
  let h = scrollViewSize.height / newZoomScale
  let x = pointInView.x - (w / 2.0)
  let y = pointInView.y - (h / 2.0)

  let rectToZoomTo = CGRectMake(x, y, w, h);

  // 4
  scrollView.zoomToRect(rectToZoomTo, animated: true)
}

Si desea más detalles, lea el tutorial, pero eso lo cubre.

 8
Author: lg365,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-12 08:15:14

Asegúrese de establecer su ViewController como el delegado de ScrollViews e implementar:

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
    return imageView
}
 0
Author: Menno,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-03 13:55:10

Check tucan9389/ZoomableScrollView bifurcado desde huynguyencong/ImageScrollView.
Simplemente crea ZoomableScrollView y llama display(view:).

DEMO

DEMO

Uso

import ZoomableScrollView

class ViewController: UIViewController

    @IBOutlet weak var zoomableScrollView: ZoomableScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let myView = MyCustomView()
        zoomableScrollView.display(view: myView)
    }
}
 0
Author: tucan9389,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-24 03:58:59

No creo que esto funcione para iOS 5.0 y Xcode 4.3+ Estoy buscando lo mismo aquí, encontré esto es para imágenes, pero puede ayudarte.

Http://www.youtube.com/watch?v=Ptm4St6ySEI

 -1
Author: user1426865,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-02 13:17:35