問題描述
我正在嘗試識別 UIScrollView
中的左/右滑動手勢.我試圖創建 UISwipeGestureRecognizers
并將它們與滾動視圖相關聯.它有效,但很少.大多數時候我沒有接到電話.為什么?
I'm trying to recognize left/right swipe gesture in a UIScrollView
. I've tried to create UISwipeGestureRecognizers
and associate them with the scroll view. It works but very rarely. Most of the time I do not get called. Why?
我怎樣才能可靠地向左/向右滑動來工作?我可以使用手勢識別器還是我必須自己在 touchesBegan/Ended
How can I reliably get swiping left/right to work? Can I use the gesture recognizers or do I have to somehow handle it myself in touchesBegan/Ended
謝謝
推薦答案
想通了.就我而言,我的 UIScrollView 包含一個允許縮放的 UIImage.顯然,這意味著啟用了滾動,并且 UIScrollView 無法區分旨在滾動和滑動的手勢(下一個、上一個圖像).
Figured it out. In my case, my UIScrollView contained a UIImage that I allowed zooming. Apparently that meant that scrolling is enabled and the UIScrollView had trouble distinguishing between gestures intended to scroll vs. swipe (next, previous image).
在我的例子中,關鍵是在圖像未放大時禁用滾動視圖中的滾動,并在放大時重新啟用它.這提供了預期的行為.
The key in my case, is to disable scrolling in the scroll view when the image is not zoomed in, and renabled it when it is zoomed in. This provides the expected behavior.
關鍵是在滾動視圖的委托中添加以下內容:
The critical piece is to put the following in the scroll view's delegate:
- (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;
}
}
我還必須在禁用滾動的情況下初始化滾動視圖.要啟用縮放,只需在委托調用中提供圖像,
I also have to initialize the scroll view with scrolling disabled. To enable zooming, simply provide an image on a delegate call,
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
// Return the scroll view
return myImage;
}
并在 viewDidLoad 中為縮放和設置手勢識別器設置一些參數
And set a few parms in viewDidLoad for the zooming and setup gesture recognizers as well
- (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];
}
這篇關于如何識別 UIScrollView 中的滑動手勢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!