Ios 如何调用带有延迟参数的void方法?
我有一个void方法,该方法将Ios 如何调用带有延迟参数的void方法?,ios,objective-c,arguments,void,Ios,Objective C,Arguments,Void,我有一个void方法,该方法将UIView设置为一个点的动画,在UIView应该设置为另一个点的特定时间后,我尝试使用[NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(method)userInfo:nil repeats:NO]来实现这一点但是看起来我不能用参数执行void。我需要执行以下操作: -(void)showNotificationViewWithText:(NSString
UIView
设置为一个点的动画,在UIView
应该设置为另一个点的特定时间后,我尝试使用[NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(method)userInfo:nil repeats:NO]来实现这一点代码>但是看起来我不能用参数执行void。我需要执行以下操作:
-(void)showNotificationViewWithText:(NSString *)title andTextColor:(UIColor *)titleTintColor andNotificationBackGroundColor:(UIColor *)backGroundColor andDuration:(float)duration direction:(BOOL) up:
或者有人有更好的主意在延迟后将UIView移动到不同的点吗?您可以使用NSObject类的方法
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
}
您可以使用NSObject类的方法
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
}
GCD函数是一种非常灵活的方法,可以在延迟后执行任何代码
dispatch\u 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\u after”,然后让自动完成完成完成其余工作!)一种非常灵活的方法是GCD函数,可以在延迟后执行任何代码
dispatch\u 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\u after”,让自动完成完成完成其余工作!)当调度计时器通过nstimer参数时,这很简单。试着这样做:-
[NSTimer
scheduledTimerWithTimeInterval:duration
target:self selector:@selector(METHOD:)
userInfo:nil repeats:NO];
-(void)METHOD:(NSTimer*) timer
{
// your code
}
当调度计时器只是通过nstimer参数时,它很简单。试着这样做:-
[NSTimer
scheduledTimerWithTimeInterval:duration
target:self selector:@selector(METHOD:)
userInfo:nil repeats:NO];
-(void)METHOD:(NSTimer*) timer
{
// your code
}
或者有人有更好的主意将UIView移动到不同的点吗
耽搁之后
如果对视图设置动画,则可以在动画方法中指定右侧的延迟
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
或者有人有更好的主意将UIView移动到不同的点吗
耽搁之后
如果对视图设置动画,则可以在动画方法中指定右侧的延迟
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
好的,谢谢,但是我有多个对象,并且没有对象的方法。你可以传递NSDictionary参数。你最终会让它工作的,但它要高得多。好的,谢谢,但我有多个对象,没有对象的方法。你可以通过NSDictionary的参数。你最终会让这个工作,但它是非常优秀的。这是farThanks最简洁的解决方案,最好的解决方案!这是法特汉克斯最整洁的解决方案,最好的解决方案!