如何使用带有延迟的参数调用void方法?

时间:2013-10-27 09:13:14

标签: ios objective-c arguments void

我有一个void方法,这个方法会将UIView设置为一个点,在UIView应该animate到另一个点的特定时间之后,我正在尝试使用[NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(METHOD) userInfo:nil repeats:NO];但看起来我无法使用Arguments执行无效操作。我需要执行这个空白:

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

或者有人有更好的想法在延迟后将UIView移动到不同的点吗?

4 个答案:

答案 0 :(得分:3)

GCD功能是一种非常灵活的方法,可以在延迟一段时间后执行任何代码 dispatch_after()。它需要一个作为你可以调用的参数 具有任意参数和返回类型的任何方法:

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: ....];
});

(提示:只需在Xcode编辑器中键入“dispatch_after”,然后让自动完成功能完成!)

答案 1 :(得分:2)

您可以使用NSObject类的performSelector:withObject:afterDelay:方法。

 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
}

答案 2 :(得分:0)

当调度计时器只是通过nstimer争论时它很简单。试试这样: -

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


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

答案 3 :(得分:0)

  

或者有人有更好的想法将UIView移动到不同的点   过了一会儿?

如果为视图设置动画,则可以在动画方法中指定延迟

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