web-dev-qa-db-fra.com

Options d'animation UIView en utilisant Swift

Comment définir le UIViewAnimationOptions sur .Repeat dans un bloc d'animation UIView:

UIView.animateWithDuration(0.2, delay:0.2 , options: UIViewAnimationOptions, animations: (() -> Void), completion: (Bool) -> Void)?)
39

Swift 3

À peu près la même qu'avant:

UIView.animate(withDuration: 0.2, delay: 0.2, options: UIViewAnimationOptions.repeat, animations: {}, completion: nil)

sauf que vous pouvez laisser de côté le type complet:

UIView.animate(withDuration: 0.2, delay: 0.2, options: .repeat, animations: {}, completion: nil)

et vous pouvez toujours combiner les options:

UIView.animate(withDuration: 0.2, delay: 0.2, options: [.repeat, .curveEaseInOut], animations: {}, completion: nil)

Swift 2

UIView.animateWithDuration(0.2, delay: 0.2, options: UIViewAnimationOptions.Repeat, animations: {}, completion: nil)

UIView.animateWithDuration(0.2, delay: 0.2, options: .Repeat, animations: {}, completion: nil)

UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: {}, completion: nil)
102
nschum

La plupart des ensembles d'options Cocoa Touch qui étaient des énumérations avant Swift 2.0 ont maintenant été modifiés en structures, UIViewAnimationOptions étant l'un d'entre eux.

Tandis que UIViewAnimationOptions.Repeat aurait précédemment été défini comme:

(semi-pseudo code)

enum UIViewAnimationOptions {
  case Repeat
}

Il est désormais défini comme:

struct UIViewAnimationOption {
  static var Repeat: UIViewAnimationOption
}

Le point étant, afin de réaliser ce qui a été réalisé avant d'utiliser des masques de bit (.Reverse | .CurveEaseInOut), vous devez maintenant placer les options dans un tableau, soit directement après le paramètre options, soit définies dans une variable avant de l'utiliser:

UIView.animateWithDuration(0.2, delay: 0.2, options: [.Repeat, .CurveEaseInOut], animations: {}, completion: nil)

ou

let options: UIViewAnimationOptions = [.Repeat, .CurveEaseInOut]
UIView.animateWithDuration(0.2, delay: 0.2, options: options, animations: {}, completion: nil)

Veuillez vous référer à la réponse suivante de l'utilisateur @ 0x7fffffff pour plus d'informations: Swift 2.0 - L'opérateur binaire "|" ne peut pas être appliqué à deux opérandes UIUserNotificationType

13
Kyle Gillen