如何在 NSTimer 中通过 @selector 传递事件
//我需要通过@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法通过选择器传递数据。选择器只是方法的名称,而不是对其的调用。当与计时器一起使用时,您传递的选择器应该接受一个参数,并且该参数将是引发它的计时器。但是,您可以使用
userInfo
参数将数据传递给被调用的方法。您可以在该参数中传递事件,并使用计时器上的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 theuserInfo
method on the timer.如果您想使用计时器来触发采用参数的方法,请使用
-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.