如何在 NSTimer 中通过 @selector 传递事件

发布于 2024-11-19 18:27:23 字数 1325 浏览 3 评论 0原文

//我需要通过@selector([move:event])发送事件 提前致谢。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(move:) userInfo:nil repeats:YES];
}

//我的移动函数

- (void)move:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    if (location.x > myImageView.center.x){
        [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x+5, myImageView.center.y); 
        }];
    }
    else if (location.x < myImageView.center.x){
        [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x-5, myImageView.center.y); 
        }];
    }
    if (location.y < myImageView.center.y){
         [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y-5); 
        }];
    }
    else if (location.y > myImageView.center.y){
        [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y+5); 
        }];
    }
}

//I need to send event trough @selector([move:event])
thanks in advance.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(move:) userInfo:nil repeats:YES];
}

//my move function

- (void)move:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint location = [touch locationInView:touch.view];
    if (location.x > myImageView.center.x){
        [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x+5, myImageView.center.y); 
        }];
    }
    else if (location.x < myImageView.center.x){
        [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x-5, myImageView.center.y); 
        }];
    }
    if (location.y < myImageView.center.y){
         [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y-5); 
        }];
    }
    else if (location.y > myImageView.center.y){
        [UIView animateWithDuration:0.001 animations:^{
            myImageView.center = CGPointMake(myImageView.center.x, myImageView.center.y+5); 
        }];
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浊酒尽余欢 2024-11-26 18:27:23

您无法通过选择器传递数据。选择器只是方法的名称,而不是对其的调用。当与计时器一起使用时,您传递的选择器应该接受一个参数,并且该参数将是引发它的计时器。但是,您可以使用 userInfo 参数将数据传递给被调用的方法。您可以在该参数中传递事件,并使用计时器上的 userInfo 方法检索它。

moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self
                                           selector:@selector(move:)
                                           userInfo:event repeats:YES];

- (void)move:(NSTimer *)theTimer {
    UIEvent *event = [theTimer userInfo];
    ...

You cannot pass data through a selector. A selector is simply the name of a method, not a call to it. When used with a timer, the selector you pass should accept one argument, and that argument will be the timer which caused it. However, you can pass data to the called method using the userInfo parameter. You pass the event in that parameter, and retrieve it using the userInfo method on the timer.

moveTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self
                                           selector:@selector(move:)
                                           userInfo:event repeats:YES];

- (void)move:(NSTimer *)theTimer {
    UIEvent *event = [theTimer userInfo];
    ...
人生百味 2024-11-26 18:27:23

如果您想使用计时器来触发采用参数的方法,请使用 -scheduledTimerWithTimeInterval:inspiration:repeats: 和适当的 NSInitation 实例来代替采用选择器的方法之一。

也就是说,您将不得不重新考虑您的方法。各个 UIEvent 和 UITouch 对象的生命周期至少与整个触摸序列一样长。根据每个类的文档,您不应保留它们或在接收它们的方法之外使用它们。如果您需要保存这些对象中的信息以供以后使用,则应将所需的信息复制到您自己的存储中。

If you want to use a timer to trigger a method that takes parameters, use -scheduledTimerWithTimeInterval:invocation:repeats: with an appropriate instance of NSInvocation in place of one of the methods that take selectors.

That said, you're going to have to reconsider your approach. Individual UIEvent and UITouch objects have a lifetime at least as long as an entire touch sequence. Per the documentation for each of those classes, you shouldn't retain them or otherwise use them outside of the method where you receive them. If you need to save the information from either of those objects for later use, you should copy the information you need into your own storage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文