كيفية تتبع عقد طويل للإصبع في اي فون؟
سؤال
كيف يمكنني تتبع حدث مثل اللمس على شاشة iPhone لمدة ثانيتين. كما هو الحال في Safari لتوفير الصورة للإضافة إلى UIWebView؟
المحلول
إنشاء nstimer مع +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
في رأيك -touchesBegan:withEvent:
الطريقة، وإلغاءها (باستخدام -invalidate
) في -touchesEnded:withEvent:
. وبعد إذا تم استدعاء طريقة محددات محددها، فقد حمل المستخدم بإصبعهم على طريقة العرض لأي مدة قمت بتعيين الفاصل الزمني للموقت إلى. مثال:
واجهة (.h):
@interface MyView : UIView
...
NSTimer *holdTimer;
@end
التنفيذ (.m):
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(touchWasHeld) userInfo:nil repeats:NO];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)evt
{
[holdTimer invalidate];
holdTimer = nil;
}
- (void)touchWasHeld
{
holdTimer = nil;
// do your "held" behavior here
}
لا تنتمي إلى StackOverflow