performSelector:withObject:afterDelay: 不能从 scrollViewDi

performSelector:withObject:afterDelay: not working from scrollViewDidZoom(performSelector:withObject:afterDelay: 不能从 scrollViewDidZoom 工作)

本文介绍了performSelector:withObject:afterDelay: 不能从 scrollViewDidZoom 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我应该知道这一点,但我已经被难住了好几个小时,而且我已经没有想法了.

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.

所以我想我会在 scrollViewDidZoom 委托方法上启动 performSelector:withObject:withDelay.如果它过期,那么我记录设置.每次调用 scrollViewDidZoom 以及捏合手势完成时,我都会删除预定的调用.

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.

这就是我所拥有的:

- (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];
}

这是输出:

2010-05-19 22:43:01.931 重置计时器
2010-05-19 22:43:01.964 重置计时器
2010-05-19 22:43:02.231 重置计时器
2010-05-19 22:43:02.253 重置计时器
2010-05-19 22:43:02.269 重置计时器
2010-05-19 22:43:02.298 重置计时器
2010-05-19 22:43:05.399 删除定时器

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.

如果我用普通的旧 performSelector 替换 performSelector:withObject:withDelay: 我得到这个:

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

2010-05-19 23:08:30.333 重置计时器
2010-05-19 23:08:30.333 位置锁定
2010-05-19 23:08:30.366 重置计时器
2010-05-19 23:08:30.367 位置锁定
2010-05-19 23:08:30.688 删除定时器

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?

推荐答案

我认为在跟踪过程中会忽略计时器事件(当手指向下滚动或缩放时).您可能必须以不同的模式执行选择器(请参阅 [NSObject performSelector:withObject:afterDelay:inModes:]).具体来说,尝试使用 @[NSRunLoopCommonModes] 作为模式.

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.

这篇关于performSelector:withObject:afterDelay: 不能从 scrollViewDidZoom 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:performSelector:withObject:afterDelay: 不能从 scrollViewDi

基础教程推荐