【问题标题】:Using swipe gesture within UIScrollView without disabling horizontal and vertical scrolling在 UIScrollView 中使用滑动手势而不禁用水平和垂直滚动
【发布时间】:2012-05-26 00:07:17
【问题描述】:

我查看了其他解决方案,但找不到适合自己的解决方案。 我在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 
    }
}

此时,我只需要在到达结束时禁用滚动等。如果我到达滚动的右边缘,我只需要禁用右滚动,这样就会触发滑动。

【问题讨论】:

  • 接受的答案here 可能会帮助您找到解决方案。
  • @rokjarc 在发布此问题之前,我看到了该解决方案。它不适用于此解决方案,因为我的图像最初大于屏幕尺寸(在缩放之前),这意味着我应该一直启用滚动。
  • 在大卫的案例中,滚动仅在放大/缩小时被禁用。这就是用户通常使用 UIScrollView 的方式。
  • 根据大卫的解决方案“在我的情况下,关键是在图像未放大时禁用滚动视图中的滚动,并在放大时重新启用它。这提供了预期的行为。 "这意味着,如果缩放比例为 1,则在未缩放时,没有滚动。我想启用滚动,因为我的图像在放大之前比屏幕尺寸大。不适合我想要实现的目标。
  • 当然,你是对的。我误解了那里的代码。

标签: iphone uiscrollview scroll gesture swipe


【解决方案1】:

我在我的 scrollView 边缘添加了一个薄 UIView 并让它们识别滑动。我无法让 scrollView 与另一个滑动手势识别器合作。这并不理想,但它现在有效。我将不得不调查是否有办法覆盖滚动视图的滑动手势识别器。

诀窍是检查滑动是否在边缘附近开始,然后决定是调用 scrollView 还是我的滑动识别器。

【讨论】:

  • 你找到覆盖scrollView滑动手势的方法了吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
  • 2010-12-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
相关资源
最近更新 更多