我試圖識別UIScrollView
中的左/右輕掃手勢。我試圖創建UISwipeGestureRecognizers
並將它們與滾動視圖關聯。它有效,但很少。大多數時候我都不會打電話。爲什麼?如何在UIScrollView中識別輕掃手勢
我該如何可靠地刷左/右工作?我可以使用手勢識別器或我必須以某種方式處理它自己在touchesBegan/Ended
感謝
我試圖識別UIScrollView
中的左/右輕掃手勢。我試圖創建UISwipeGestureRecognizers
並將它們與滾動視圖關聯。它有效,但很少。大多數時候我都不會打電話。爲什麼?如何在UIScrollView中識別輕掃手勢
我該如何可靠地刷左/右工作?我可以使用手勢識別器或我必須以某種方式處理它自己在touchesBegan/Ended
感謝
想通了。在我的情況下,我的UIScrollView包含一個UIImage,允許縮放。顯然這意味着滾動被啓用,並且UIScrollView無法區分意圖滾動與滑動(下一個,前一個圖像)的手勢。
在我的情況下,關鍵是禁用圖像放大時滾動視圖中的滾動,並在放大時對其進行renable。這提供了預期的行爲。
的關鍵部分是放在滾動視圖的委託執行以下操作:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if (scrollView.zoomScale!=1.0) {
// Zooming, enable scrolling
scrollView.scrollEnabled = TRUE;
} else {
// Not zoomed, disable scrolling so gestures get used instead
scrollView.scrollEnabled = FALSE;
}
}
我也有來初始化禁用滾動滾動視圖。 要啓用縮放,只需一個委託調用提供圖像,
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the scroll view
return myImage;
}
,並設置viewDidLoad中的幾個PARMS的縮放和設置手勢識別以及
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.contentSize = CGSizeMake(myImage.frame.size.width, myImage.frame.size.height);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1.0;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
[myScrollView addSubview:myImage];
[self setWantsFullScreenLayout:TRUE];
myScrollView.scrollEnabled = FALSE;
UISwipeGestureRecognizer *recognizer =
[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
recognizer.delaysTouchesBegan = TRUE;
[myScrollView addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[myScrollView addGestureRecognizer:recognizer];
[recognizer release];
[myScrollView delaysContentTouches];
}
好貼。
我正在做一個類似的事情(沒有圖像視圖),我基本上不得不禁用滾動如果contentSize小於高度(我的滾動視圖只滾動垂直)。
if (scrollView.contentSize.height>scrollView.frame.size.height) {
scrollView.scrollEnabled = YES;
}
else {
scrollView.scrollEnabled = NO;
}
這奏效了,我
UIScrollView *scrollView = ...
UISwipeGestureRecognizer *mySwipe = ...
正確的解決辦法來解決這個問題是添加一行代碼:
[scrollView.panGestureRecognizer requireGestureRecognizerToFail:mySwipe]
斯威夫特版本:
scrollView.panGestureRecognizer.requireGestureRecognizerToFail(mySwipe)
絕對認同這是正確的解決方案!我還必須在'pinchGestureRecognizer'中添加相同的行,以便在我嘗試檢測多指滑動時使其可靠。 – 2015-04-24 10:51:21
這絕對看起來是正確的路!有時候我發現滑動手勢過早燃燒,但現在這是一個滑動處理程序問題。至少我們不必做其他人建議的各種奇怪的黑客事情,只是「感覺不對」。謝謝!! – horseshoe7 2015-12-14 16:24:18
所有時間的偉大答案.. :) – 2016-02-11 12:48:50
真的很棒找到大衛。我的滑動識別器也無法工作。我放棄了並使用了touchesBegan:使用NSNotification,但它也給了MasterViewControllers(在iPad中)的通知。我對模糊的眼光尋找這個概率。你節省了我的時間和精力!衷心感謝大衛。繼續發佈技巧..祝你有個美好的一天。 – gopikrishnan 2010-12-08 21:56:26
感謝您的技巧 – iOSAppDev 2012-06-27 12:48:59
或者也許在一行 - scrollView.scrollEnabled =(scale!= 1.0f) – Yariv 2013-02-22 09:11:44