Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView animation options using Swift

How do I set the UIViewAnimationOptions to .Repeat in an UIView animation block:

UIView.animateWithDuration(0.2, delay:0.2 , options: UIViewAnimationOptions, animations: (() -> Void), completion: (Bool) -> Void)?) 
like image 611
Morten Gustafsson Avatar asked Jun 06 '14 11:06

Morten Gustafsson


People also ask

Does UIView animate need weak self?

You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.

Does UIView animate run on the main thread?

The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.


2 Answers

Swift 3

Pretty much the same as before:

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

except that you can leave out the full type:

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

and you can still combine 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) 
like image 132
nschum Avatar answered Oct 06 '22 15:10

nschum


Most of the Cocoa Touch 'option' sets that were enums prior to Swift 2.0 have now been changed to structs, UIViewAnimationOptions being one of them.

Whereas UIViewAnimationOptions.Repeat would previously have been defined as:

(semi-pseudo code)

enum UIViewAnimationOptions {   case Repeat } 

It is now defined as:

struct UIViewAnimationOption {   static var Repeat: UIViewAnimationOption } 

Point being, in order to achieve what was achieved prior using bitmasks (.Reverse | .CurveEaseInOut) you'll now need to place the options in an array, either directly after the options parameter, or defined in a variable prior to utilising it:

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

or

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

Please refer to the following answer from user @0x7fffffff for more information: Swift 2.0 - Binary Operator “|” cannot be applied to two UIUserNotificationType operands

like image 43
Kyle G Avatar answered Oct 06 '22 17:10

Kyle G