performSelector:withObject:afterDelay: not working from scrollViewDidZoom

不羁的心 提交于 2020-04-29 08:20:34

问题


I feel like I should know this but I've been stumped for hours now and I've run out of ideas.

The theory is simple, the user manipulates the zoom and positioning in a scrollview using a pinch. If they hold that pinch for a short period of time then the scrollview records the zoom level and content offsets.

So I thought I'd start a performSelector:withObject:withDelay on the scrollViewDidZoom delegate method. If it expires then I record the settings. I delete the scheduled call every time scrollViewDidZoom is called and when the pinch gesture finishes.

This is what I have:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    NSLog(@"resetting timer");
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(positionLock) object:nil];

    [self performSelector:@selector(positionLock) withObject:nil afterDelay:0.4];               
}

-(void)positionLock{
    NSLog(@"position locked ");
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{

    NSLog(@"deleting timer");
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(positionLock) object:nil];
}

This is the output:

2010-05-19 22:43:01.931 resetting timer
2010-05-19 22:43:01.964 resetting timer
2010-05-19 22:43:02.231 resetting timer
2010-05-19 22:43:02.253 resetting timer
2010-05-19 22:43:02.269 resetting timer
2010-05-19 22:43:02.298 resetting timer
2010-05-19 22:43:05.399 deleting timer

As you can see the delay between the last and second last events should have been more than enough for the delayed selector call to execute.

If I replace performSelector:withObject:withDelay with plain old performSelector: I get this:

2010-05-19 23:08:30.333 resetting timer
2010-05-19 23:08:30.333 position locked
2010-05-19 23:08:30.366 resetting timer
2010-05-19 23:08:30.367 position locked
2010-05-19 23:08:30.688 deleting timer

Which isn't what I want but serves to show that it's only the delay that's causing it to not function, not something to do with the selector not being declared in the header, being misspelt or any other reason.

Any ideas as to why it's not working?


回答1:


I think that timer events are ignored during tracking (when a finger is down in order to scroll or zoom). You might have to perform the selector in a different mode (see [NSObject performSelector:withObject:afterDelay:inModes:]). Specifically, try using @[NSRunLoopCommonModes] for the mode.




回答2:


Just to add onto what Brian said - here's my implementation of his answer:

[self performSelector:@selector(callMethod) withObject:0 afterDelay:1.0 inModes:@[NSRunLoopCommonModes]];

Note - inModes: takes an array of modes.



来源:https://stackoverflow.com/questions/2865760/performselectorwithobjectafterdelay-not-working-from-scrollviewdidzoom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!