//I need to send event trough @selector([move:event]) thanks in advance.
- (void)touchesBegan:(NSSet *)touches withE开发者_如何学Govent:(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);
}];
}
}
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];
...
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.
精彩评论