2017-04-26 2 views
0

Ich habe eine Tastaturbenachrichtigung und möchte, dass die Platzhalteransicht am linken Rand der Suchleiste animiert wird.UIView Animation funktioniert nicht mit NSLayoutConstraint?

Unten ist, was ich verwende, um die Animation aufzurufen, die asynchron mit der Tastaturöffnung ist. Momentan bewegt sich die Ansicht an die Stelle, an der ich sie haben möchte, aber es ist eher so, als würde sie dort teleportieren anstatt zu animieren.

 DispatchQueue.main.async { 
      if isKeyboardShowing { 
       UIView.animate(withDuration: 2, delay: 1, options: UIViewAnimationOptions.curveEaseOut, animations: { 
        NSLayoutConstraint(item: self.placeholder, attribute: .left, relatedBy: .equal, toItem: self.searchBar, attribute: .left, multiplier: 1, constant: 5).isActive = true 
       }, completion: { (completed) in 

       }) 

      } 
     } 

Irgendwelche Vorschläge?

Antwort

1

Die Animation mit Einschränkungen könnte ein wenig schwierig sein. Zuerst müssen Sie die gewünschten Einschränkungen anzupassen, außerhalb der Animation Schließung:

NSLayoutConstraint(item: self.placeholder, attribute: .left, relatedBy: .equal, toItem: self.searchBar, attribute: .left, multiplier: 1, constant: 5).isActive = true 

Da Einschränkung Änderungen dazu führen könnten, andere Ansichten ihre Position ändern, müssen Sie Superview fragen (in den meisten Fällen die VC-Ansicht) seine Subviews Layout innerhalb der Animation Schließung:

UIView.animate(withDuration: 2, delay: 1, options: UIViewAnimationOptions.curveEaseOut, animations: { 
     self.view.layoutIfNeeded() 
}, completion: nil) 

Deshalb ist die Superview stellt Subviews Positionen für Sie innerhalb der Animation Block.

0

Um ein NSLayoutConstraint Sie animieren müssen setNeedsLayout() in der Animation-Block setzen. Sie sollten dann die

NSLayoutConstraint(item: self.placeholder, attribute: .left, relatedBy: .equal, toItem: self.searchBar, attribute: .left, multiplier: 1, constant: 5).isActive = true 

wie so über dem Animationsblock bewegen:

NSLayoutConstraint(item: self.placeholder, attribute: .left, relatedBy: .equal, toItem: self.searchBar, attribute: .left, multiplier: 1, constant: 5).isActive = true 
UIView.animate(withDuration: 2, delay: 1, options: UIViewAnimationOptions.curveEaseOut, animations: { 
    self.layoutIfNeeded() 
}, completion: { (completed) in 

}) 

Hoffnung, das hilft!

Verwandte Themen