1

我有一个 void 方法,该方法将 a 动画UIView到一个点,在特定时间后UIView应该animate到另一个点,我试图用 a 来做这个,[NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(METHOD) userInfo:nil repeats:NO];但看起来我不能用Arguments. 我需要执行这个 void:

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

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

4

4 回答 4

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”,剩下的就交给自动补全了!)

于 2013-10-27T09:27:31.273 回答
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
}
于 2013-10-27T09:18:08.097 回答
0

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

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


  -(void)METHOD:(NSTimer*) timer
  {
 // your code
   }    
于 2013-10-27T09:31:57.640 回答
0

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

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

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
于 2013-10-27T10:58:32.687 回答