當使用UIPanGestureRecognizer
並檢測UIGestureRecognizerStateEnded
時,手勢的速度不是真實的速度。相反,這是以前調用我的操作方法的舊速度。我如何在手勢結束時訪問真實速度?如何確定平底手勢的真實最終速度?
創建我UIPanGestureRecognizer
這樣的:
UIPanGestureRecognizer* panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
[panGestureRecognizer setMaximumNumberOfTouches:2];
[panGestureRecognizer setMinimumNumberOfTouches:1];
[panGestureRecognizer setDelegate:self];
[panGestureRecognizer setDelaysTouchesBegan:NO];
[panGestureRecognizer setDelaysTouchesEnded:NO];
[panGestureRecognizer setCancelsTouchesInView:NO];
[self addGestureRecognizer:panGestureRecognizer];
我的行動方法的開始是在這裏:日誌輸出的
- (IBAction) panGestureRecognized:(UIPanGestureRecognizer *)recognizer {
UIGestureRecognizerState state = recognizer.state;
CGPoint gestureTranslation = [recognizer translationInView:self];
CGPoint gestureVelocity = [recognizer velocityInView:self];
[CBAppDelegate log:@"panGestureRecognized: state: %s\n translation: (%f, %f)\n velocity: (%f, %f)", [self toString:state], gestureTranslation.x, gestureTranslation.y, gestureVelocity.x, gestureVelocity.y];
例子:
2013-09-30_10:46:32.830 panGestureRecognized: state: UIGestureRecognizerStateChanged
translation: (-283.000000, 2.000000)
velocity: (-43.046783, 45.551472)
2013-09-30_10:47:02.942 panGestureRecognized: state: UIGestureRecognizerStateEnded
translation: (-283.000000, 2.000000)
velocity: (-43.046783, 45.551472)
,你可以看,兩個日誌條目的速度是一樣的(同樣的故事與翻譯,但我只關心速度),儘管我沒有移動手指約30秒,然後擡起手指。您可以根據條目的時間戳來確定時間。在不移動我的手指30秒後,肯定不會有速度報告。
我已經用iOS 6.1的iOS模擬器對iPhone進行了測試。
就可以計算出自己與時間戳。長時間擱置,如果開始時間戳足夠長,則可以重置開始時間戳,然後根據需要計算任何內容。祝你好運! –