[英]How to detect when no fingers are on the screen?
我需要有關touchesEnded
函數的幫助。 我想使用touchesEnded
函數在屏幕上沒有手指時啟動NSTimer
。 這可能嗎?。 目前,我有一個touchesBegan
函數正在工作:-)。
這是我的代碼:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger numTaps = [[touches anyObject] tapCount];
if (numTaps >= 2)
{
self.label.text = [NSString stringWithFormat:@"%d", numTaps];
self.label2.text = @"+1";
[self.button setHidden:YES];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)showButton
{
[self.button setHidden:NO];
}
這很棘手。 您不會得到一個告訴您所有手指都向上的事件。 每個touchesEnded
僅告訴您該手指向上。 因此,您必須自己跟蹤觸摸。 通常的技術是在觸摸到達時將其存儲在可變集合中。 但是您不能自己存儲觸摸,因此必須存儲一個標識符。
首先在UITouch上放置一個類別,以派生此唯一標識符:
@interface UITouch (additions)
- (NSString*) uid;
@end
@implementation UITouch (additions)
- (NSString*) uid {
return [NSString stringWithFormat: @"%p", self];
}
@end
現在,我們必須在有觸摸的整個過程中保持可變的設置,在第一次觸摸時創建它,並在最后一次觸摸消失時銷毀它。 我假設您有一個NSMutableSet實例變量/屬性。 這是偽代碼:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// create mutable set if it doesn't exist
// then add each touch's uid to the set with addObject:
// and then go on and do whatever else you want to do
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// remove uid of *every* touch that has ended from our mutable set
// if *all* touches are gone, nilify the set
// now you know that all fingers are up
}
要創建一個空閑計時器,最簡單的方法是創建UIApplication的自定義子類,並覆蓋sendEvent:
-在此,調用super並重置您的計時器。 這將為您涵蓋所有方面。 您的應用收到的每一次觸摸都會通過這種方法進行。
要使用定制應用程序子類,您需要在main.m中修改對UIApplicationMain()的調用,並插入定制類名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.