animations animate ios iphone swift uiview uiviewanimation

ios - animate - uiview animation swift 3



Swift: mueve UIView en gesto de diapositiva (7)

Actualización para Swift 3.x

Al asignar el selector, la sintaxis ha cambiado y ahora requiere #selector

let gesture = UIPanGestureRecognizer(target: self, action: #selector(navViewDragged(gesture:))) self.navLayoutView.addGestureRecognizer(gesture) self.navLayoutView.isUserInteractionEnabled = true gesture.delegate = self

Implementación de la función:

func navViewDragged(gesture: UIPanGestureRecognizer){ //Code here }

Estoy tratando de mover un UIView en el gesto de deslizamiento hacia arriba desde su posición inicial a una posición final fija. La imagen debe moverse con el gesto de la mano, y no animar de forma independiente.

No he intentado nada, ya que no tengo ni idea de por dónde empezar, qué clase de gesto usar.


Así es como realmente lo hace como la vista de noticias en la aplicación Stocks

Primero agregue 2 restricciones en Storyboard a la vista deslizante, una para su estado cuando está completamente abierta y otra para cuando está cerrada. No olvide dejar una de las restricciones deshabilitada / no instalada para que su vista se vea abierta o cerrada cuando se alcance la escena. Haz referencia a ellos en tu código

@IBOutlet weak var optionsOpenedConstraint: NSLayoutConstraint! @IBOutlet weak var optionsVisiableConstraint: NSLayoutConstraint!

ahora agregue el UIPanGestureRecognizer a su vista en la función viewDidLoad .

let gesture = UIPanGestureRecognizer(target: self, action: #selector(type(of: self).wasDragged(gestureRecognizer:))) optionsView.addGestureRecognizer(gesture)

Finalmente agregue este callback y 2 funciones:

@objc func wasDragged(gestureRecognizer: UIPanGestureRecognizer) { let distanceFromBottom = screenHeight - gestureRecognizer.view!.center.y if gestureRecognizer.state == UIGestureRecognizer.State.began || gestureRecognizer.state == UIGestureRecognizer.State.changed { optionsOpenedConstraint.isActive = false optionsVisiableConstraint.isActive = false let translation = gestureRecognizer.translation(in: self.view) if((distanceFromBottom - translation.y) < 100) { gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x, y: gestureRecognizer.view!.center.y + translation.y) gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view) } } if gestureRecognizer.state == UIGestureRecognizer.State.ended{ if distanceFromBottom > 6{ openOptionsPanel() }else{ closeOptionsPanel() } } } func openOptionsPanel(){ optionsOpenedConstraint.isActive = true optionsVisiableConstraint.isActive = false UIView.animate(withDuration: 0.5) { self.view.layoutIfNeeded() } } func closeOptionsPanel(){ optionsOpenedConstraint.isActive = false optionsVisiableConstraint.isActive = true UIView.animate(withDuration: 0.5) { self.view.layoutIfNeeded() } }

y voalá


Finalmente lo hice como abajo.

let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:")) slideUpView.addGestureRecognizer(gesture) slideUpView.userInteractionEnabled = true gesture.delegate = self

Se llama a la siguiente función cuando se detecta el gesto, (aquí estoy restringiendo la vista para que tenga un centre.y máximo de 555, y estoy restableciendo de nuevo a 554 cuando la vista se mueve más allá de este punto)

func wasDragged(gestureRecognizer: UIPanGestureRecognizer) { if gestureRecognizer.state == UIGestureRecognizerState.Began || gestureRecognizer.state == UIGestureRecognizerState.Changed { let translation = gestureRecognizer.translationInView(self.view) print(gestureRecognizer.view!.center.y) if(gestureRecognizer.view!.center.y < 555) { gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x, gestureRecognizer.view!.center.y + translation.y) }else { gestureRecognizer.view!.center = CGPointMake(gestureRecognizer.view!.center.x, 554) } gestureRecognizer.setTranslation(CGPointMake(0,0), inView: self.view) } }


Mueve la vista a cualquier lugar en Swift 3

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(dragged(gestureRecognizer:))) demoView.isUserInteractionEnabled = true demoView.addGestureRecognizer(panGesture)

Función

@objc func dragged(gestureRecognizer: UIPanGestureRecognizer) { if gestureRecognizer.state == UIGestureRecognizerState.began || gestureRecognizer.state == UIGestureRecognizerState.changed { let translation = gestureRecognizer.translation(in: self.view) gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x + translation.x, y: gestureRecognizer.view!.center.y + translation.y) gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view) } }


Probablemente quieras usar un UIPanGestureRecognizer .

let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged:")) customView.addGestureRecognizer(gesture) gesture.delegate = self

Y para arrastrar el objeto solo a lo largo del eje y:

func wasDragged(gesture: UIPanGestureRecognizer) { let translation = gesture.translationInView(self.view) // Use translation.y to change the position of your customView, e.g. customView.center.y = translation.y // Customize this. }


Swift 4:

@objc func wasDragged(gestureRecognizer: UIPanGestureRecognizer) { if gestureRecognizer.state == UIGestureRecognizerState.began || gestureRecognizer.state == UIGestureRecognizerState.changed { let translation = gestureRecognizer.translation(in: self.view) print(gestureRecognizer.view!.center.y) if(gestureRecognizer.view!.center.y < 555) { gestureRecognizer.view!.center = CGPoint(x: gestureRecognizer.view!.center.x, y: gestureRecognizer.view!.center.y + translation.y) }else { gestureRecognizer.view!.center = CGPoint(x:gestureRecognizer.view!.center.x, y:554) } gestureRecognizer.setTranslation(CGPoint(x: 0, y: 0), in: self.view) } }

Llamada

let gesture = UIPanGestureRecognizer(target: self, action: self.wasDragged(gestureRecognizer:)) customView.addGestureRecognizer(gesture) gesture.delegate = self


@IBAction func handlePanGesture(_ recognizer: UIPanGestureRecognizer) { if MainView.bounds.contains(mainImage.frame) { let recognizerCenter = recognizer.location(in: MainView) mainImage.center = recognizerCenter } if MainView.bounds.intersection(mainImage.frame).width > 50 && MainView.bounds.intersection(mainImage.frame).height > 0 { let recognizerCenter = recognizer.location(in: MainView) print(recognizerCenter) mainImage.center = recognizerCenter } } @IBAction func handlePinchGesture(_ recognizer: UIPinchGestureRecognizer) { mainImage.transform = mainImage.transform.scaledBy(x: recognizer.scale, y: recognizer.scale) recognizer.scale = 1.0 } @IBAction func handleRotateGesture(_ recognizer: UIRotationGestureRecognizer) { mainImage.transform = mainImage.transform.rotated(by: recognizer.rotation) recognizer.rotation = 0.0 }