問題描述
我覺得我應該知道這一點,但我已經被難住了好幾個小時,而且我已經沒有想法了.
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 工作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!