22

Probably it is just a question of proper syntax.

I use the animateWithDuration:delay:options:animations:completion: UIView method.

The options: is the problematic part here: when I assign only one option (for example UIViewAnimationOptionCurveEaseInOut) everything works fine.

What if I want to assign multiple options to the same animation? How can I do that?

I have tried the following code, but the options: part turned out to be completely ignored:

>   [UIView animateWithDuration:DURATION
>                         delay:DELAY
>                       options:(UIViewAnimationOptionAllowUserInteraction,
>                                UIViewAnimationOptionCurveEaseInOut)
>                    animations: ^{/*animations here*/}
>                    completion: ^(BOOL finished){/*actions on complete*/}];

It was just a try and it didn't work. Which syntax should I use here?

Thanks for any help in advance.

Sergey Lost
  • 2,511
  • 3
  • 19
  • 22

2 Answers2

58

Objective-C

options:(UIViewAnimationOptionAllowUserInteraction |
                            UIViewAnimationOptionCurveEaseInOut)

Swift

In Swift UIViewAnimationOptions is an Option set type and multiple options can be passed following way:

options:[.AllowUserInteraction, .CurveEaseInOut]
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • I was sure it will be easy. Thank you, Vladimir. Спасибо. – Sergey Lost Aug 17 '10 at 08:08
  • What's the answer for Swift? It doesn't like the '|' operator apparently. – Mark A. Donohoe Sep 01 '16 at 02:01
  • Why do they use, what i see as the "OR" operator? What is the history behind this? – nick carraway Apr 21 '23 at 13:27
  • @nickcarraway, not sure I understand the question. Every UIViewAnimationOptions value is a bit flag, i.e. it has exactly one bit set to 1 in its binary representation. Result of `|` (bitwise OR) operator will be a number that has bits set for every option passed to it. – Vladimir Apr 27 '23 at 22:42
2

Just to add the reason it seems the compiler ignored your supplied options yet didn't throw an error is because the syntax that you tried makes use of the comma operator which is often overlooked in C. Essentially

(UIViewAnimationOptionAllowUserInteraction, UIViewAnimationOptionCurveEaseInOut)

tels the compiler to discard the first option and only assign the value after the comma. In the more general case, the first argument to the comma operator is evaluated, but it's result is discarded.

mikecsh
  • 852
  • 7
  • 12