Reputation: 111
I'm searching for the best solution for the following scenario: I have a label which can be moved around by the user (using UIPanGestureRecognizer). When the gesture is finished, I want the label to keep moving automatically, until it reaches specified coordinates.
What would be the simplest way to implement that? I was thinking about UIFieldBehavior.radialGravityField, but in this case label doesn't stop in the point towards which it gravitates. There is also electric field I didn't tested yet (it's pretty difficult to find any tutorials/ info apart from laconic official documentation). I would prefer kind of solution where I specify the point towards which this label "gravitates" rather than specifying curve of the movement.
Would appreciate any suggestions on the elegant and simple solution.
Upvotes: 0
Views: 49
Reputation: 6049
You can use UIViewPropertyAnimator
, which has been available since iOS 10.
// set desired frame of label here
let animator = UIViewPropertyAnimator(duration: duration, dampingRatio: 0.2, animations: {
self.view.layoutIfNeeded()
})
animator.startAnimation()
The benefit to this approach is that you can pause, reverse, repeat, and stop animations at any time, if desired.
Upvotes: 1
Reputation: 1713
If I understand correctly you just want the TextField to move to "specific coords." If you know them and the duration it should take to get there at that moment you want to move the View (TextField), just try something like that:
UITextField.animate(withDuration: <#T##TimeInterval#>) {
textField.center = CGPoint(x: <#T##CGFloat#>, y: <#T##CGFloat#>)
}
Upvotes: 1