簡體   English   中英

在UIScrollView中使用滑動手勢而不禁用水平和垂直滾動

[英]Using swipe gesture within UIScrollView without disabling horizontal and vertical scrolling

我瀏覽了其他解決方案,但找不到適合自己的解決方案。 我在UIScrollView有一個UIImageView ,其中顯示了大圖片。

我在UIScrollView以及左右滑動手勢識別器中啟用了捏合手勢。

目前,scrollview的平移手勢似乎禁用(或損壞)了滑動手勢。 我不想在UIScrollView上禁用水平或垂直滾動​​,並且照片的初始縮放比例太大,無法禁用水平滾動。

我想做的是當我進入UIScrollView的邊緣時觸發輕掃手勢。

這是一些代碼;

- (void)viewDidLoad
{
    // recognizer for pinch gestures
    UIPinchGestureRecognizer *pinchRecognizer =[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
    [self.myScrollView addGestureRecognizer:pinchRecognizer];
    [self.myScrollView setClipsToBounds:NO];

    // recognizer for swipe gestures
    UISwipeGestureRecognizer *recognizer;

    // left and right swipe recognizers for left and right animation
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self myScrollView] addGestureRecognizer:recognizer];

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [[self myScrollView] addGestureRecognizer:recognizer];
    ....

我的左滑動處理程序,當前的左和右滑動沒有任何其他功能

-(void)handleLeftSwipe:(UISwipeGestureRecognizer *)recognizer
{    
    if(!self.tableView.hidden) self.tableView.hidden = YES;
    [self showRequiredStuff];
    CATransition *transition = [CATransition animation];
    transition.duration = 0.75;
    transition.timingFunction = [CAMediaTimingFunction     functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype =kCATransitionFromLeft;
    transition.delegate = self;
    [self.view.layer addAnimation:transition forKey:nil];

我的初始屏幕尺寸;

#define IMAGE_VIEW_WIDTH 320.0
#define IMAGE_VIEW_HEIGHT 384.0

我對圖片使用縮放比例,以使其盡可能縮小,但其中大多數是寬圖像,這種情況下啟用了水平滾動,而禁用了垂直滾動。 盡管我的滑動處理程序也是水平的。

我想我已經清楚地解釋了正在發生的事情和我所需要的。 我已經發布了代碼,因為我是iPhone應用程序開發的新手,我倆都想幫助其他人看到盡可能多的代碼,也許有人會指出任何不好的編程,我們都將從中受益。

相關解決方案的其他發現; 設置后;

@interface myViewController () <UIScrollViewDelegate>

self.myScrollView.delegate = self;

水平檢測邊緣是否到達

- (BOOL) hasReachedAHorizontalEdge {
    CGPoint offset = self.myScrollView.contentOffset;
    CGSize contentSize = self.myScrollView.contentSize;
    CGFloat height = self.myScrollView.frame.size.height;
    CGFloat width  = self.myScrollView.frame.size.width;

if ( offset.x == 0 || 
    (offset.x + width) == contentSize.width ) {
    return YES;
}

    return NO;

}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    if ( [self hasReachedAHorizontalEdge] ) {
        NSLog(@"Reached horizontal edge.");
        // required here 
    }
}

在這一點上,我只需要在到達的末端禁用滾動,依此類推。如果我到達了滾動的右邊緣,則只需要禁用向右滾動,這樣就可以觸發滑動。

我在scrollView的邊緣添加了一個薄UIView,讓它們識別滑動。 我無法使scrollView與另一個滑動手勢識別器配合使用。 這不是理想的方法,但目前可以使用。 我將研究是否有一種方法可以覆蓋scrollView的滑動手勢識別器。

訣竅是檢查並查看滑動是否開始於邊緣附近,然后決定是否調用scrollView或我的滑動識別器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM