Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass arguments to NSAction?

Well, everyone knows that in ObjC we have

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

Notice that completion block has a BOOL argument. Now let's look at Monotouch:

public static void Animate (double duration, double delay, UIViewAnimationOptions options, NSAction animation, NSAction completion)

NSAction is:

public delegate void NSAction ();

Just the delegate without any arguments. Moreover, in Monotouch "internals" we can see:

public static void Animate (double duration, double delay, UIViewAnimationOptions options, 
NSAction animation, NSAction completion)
{
    UIView.AnimateNotify (duration, delay, options, animation, delegate (bool x)
    {
        if (completion != null)
        {
            completion ();
        }
    });
}

Notice delegate (bool x), It calls the function just like I need. Now, how can I pass Action<bool> as completion to UIView.Animate ?

like image 465
folex Avatar asked Sep 04 '12 14:09

folex


2 Answers

That was an old binding bug (wrong type) and, for compatibility reason, Animate still use a NSAction completion handler.

To fix this a new method AnimateNotify was added to MonoTouch. This version accept a UICompletionHandler which is defined like this:

public delegate void UICompletionHandler (bool finished);

So the solution to your problem is to use the newer AnimateNotify API.

like image 104
poupou Avatar answered Oct 06 '22 00:10

poupou


So that should look like:

UIView.AnimateNotify(duration, 0, UIViewAnimationOptions.CurveEaseInOut, delegate () {

}, delegate (bool finished) {

});

Or with lambda syntax:

UIView.AnimateNotify(duration, 0, UIViewAnimationOptions.CurveEaseInOut, () => {

}, (finished) => {

});
like image 21
superlogical Avatar answered Oct 06 '22 00:10

superlogical