idoodler
idoodler

Reputation: 3545

How to call a void method with arguments with a delay?

I have a void method, this method animates a UIView to a point, after a specific time the UIView should animate to another point, I am trying to do this with a [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(METHOD) userInfo:nil repeats:NO]; but it looks like I can't perform a void with Arguments. I need to perform this void:

-(void)showNotificationViewWithText:(NSString *)title andTextColor:(UIColor *)titleTintColor andNotificationBackGroundColor:(UIColor *)backGroundColor andDuration:(float)duration direction:(BOOL) up:

Or does anyone have a better idea to move a UIView to different points after a delay?

Upvotes: 1

Views: 727

Answers (4)

Mario
Mario

Reputation: 4520

Or does anyone have a better idea to move a UIView to different points after a delay?

If you animate the view you can specify a delay right in the animation method

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

Upvotes: 0

Martin R
Martin R

Reputation: 539945

A very flexible method to execute any code after some delay is the GCD function dispatch_after(). It takes a block as an argument where you can call any methods with arbitrary arguments and return types:

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    // do whatever you want, e.g.
    [self showNotificationViewWithText: ....];
});

(Tip: Just type "dispatch_after" in the Xcode editor, and let the autocompletion do the rest!)

Upvotes: 3

Hussain Shabbir
Hussain Shabbir

Reputation: 15025

Its simple when scheduling timer just pass nstimer arguement. Try like this:-

     [NSTimer    
   scheduledTimerWithTimeInterval:duration 
    target:self selector:@selector(METHOD:) 
   userInfo:nil repeats:NO];


  -(void)METHOD:(NSTimer*) timer
  {
 // your code
   }    

Upvotes: 0

Parag Bafna
Parag Bafna

Reputation: 22930

You can use performSelector:withObject:afterDelay: method of NSObject class.

 NSDictionary *obj = [[NSDictionary alloc] initWithObjectsAndKeys:title,@"title",titleTintColor,@"titleTintColor",backGroundColor,@"backGroundColor",[NSNumber numberWithFloat:duration],@"duration",[NSNumber numberWithBool:up],@"up" nil];
 [self performSelector:@selector(showNotificationViewWithText:) withObject:obj afterDelay:2.0];  

-(void)showNotificationViewWithText:(NSDictionary *)arg
{
    NSDictionary *title = [arg objectForkey:@"title"];
    UIColor      *titleTintColor = [arg objectForkey:@"titleTintColor"];
    UIColor      *backGroundColor = [arg objectForkey:@"backGroundColor"];
    float        duration = [arg objectForkey:@"duration"];
    BOOL         up = [arg objectForkey:@"up"];

    //code
}

Upvotes: 2

Related Questions