2

我有一个主 UIView,其中包含一个滚动视图。我已经使用以下代码为 4 种类型的滑动为主视图设置了 UIGestureRecognizer:

UISwipeGestureRecognizer *swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(upCommand)];
[swipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[mainGameView addGestureRecognizer:swipeUpRecognizer];
... // Done 4 times for each direction

当我禁用滚动视图上的滚动时,此代码效果很好(我可以在屏幕上的任何位置滑动,相关操作按预期执行)。但是,我想添加功能,以便如果我在滚动视图上触摸两根手指,我可以像滚动视图通常那样来回平移。我尝试向滚动视图添加手势识别器以检测两个手指何时平移:

- (void)viewDidLoad
{
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizePan)];
    panGestureRecognizer.minimumNumberOfTouches = 2;
    panGestureRecognizer.maximumNumberOfTouches = 2;
    [scrollView addGestureRecognizer:panGestureRecognizer];
}

- (void)recognizePan
{
    [gameScrollView setScrollEnabled:YES];
}

我将其与以下方法结合使用,以在抬起手指后再次禁用滚动:

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [gameScrollView setScrollEnabled:NO];
}

这种工作,但不是我想要的方式。当我在滚动视图上拖动两个手指时,滚动将设置为启用,但我不能使用这两个手指滚动。我首先需要抬起两根手指,然后才能用一根手指滚动(两根手指不起作用)。当我抬起可以滚动滚动视图的单根手指时,滚动被禁用,如scrollViewDidEndDragging.

显然,这种类型的滚动对用户来说不是很友好,但我似乎找不到设置滚动视图的方法,所以它只有在两根手指在滚动视图上拖动时才会滚动。感谢您提前提供任何帮助。

~ 17 岁的业余 iOS 开发者和手势新手

编辑:根据这个问题的建议之一,我尝试实现 UISubView 的子类来覆盖默认的 touchesBegan 方法,但我无法让它工作。

自定义滚动视图.h:

@interface CustomScrollView : UIScrollView 
{
}

@end

自定义滚动视图.m:

#import "CustomScrollView.h"

@implementation CustomScrollView

- (id)initWithFrame:(CGRect)frame 
{
  return [super initWithFrame:frame];
}

- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event 
{   
  // What goes here so that The action it can be called from the ViewController.h
}

@end

视图控制器.h:

#import <UIKit/UIKit.h>

@class CustomScrollView;

@interface ViewController : UIViewController <UIScrollViewDelegate>
{
  CustomScrollView *scrollView;
}

@end

视图控制器.m:

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event 
{
  // What goes here?
}
4

4 回答 4

2

子类化 ScrollView 并实现以下方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  if(gestureRecognizer.numberOfTouches != 2) {
    return NO;
  } else {
    return YES;
  }
}
于 2016-11-30T07:39:22.643 回答
0

我认为解决这个问题的起点是遵循 UIView 方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

调用此方法时,我们可以检查触摸位置、触摸次数等,然后决定使用哪个识别器。如果您创建 UIScrollView 子类并覆盖gestureRecognizerShouldBegin,您还可以控制 UIScrollView 的默认手势识别器(平移/缩放)。

当然,不需要使用 scrollEnabled 属性。

于 2013-01-17T15:57:48.737 回答
0

尝试继承 UIScrollView 并覆盖方法 touchesShouldBegin:withEvent:inContentView:。该文档指出以下内容:

被子类覆盖以自定义手指在显示内容中触摸时的默认行为。

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

因此,您可能能够检查您是否有不止一次触摸,并且只有返回一个真实值。

于 2013-01-17T12:52:32.273 回答
-1

尝试这个

  - (void)viewDidLoad
{  

    [super viewDidLoad];


    UISwipeGestureRecognizer *swipeUpRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(upCommand)];
    swipeUpRecognizer.delegate = self;
    [swipeUpRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [self.view addGestureRecognizer:swipeUpRecognizer];
    scrollView.delegate = scrollView;

    panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizePan:)];
    panGestureRecognizer.minimumNumberOfTouches = 1;
    panGestureRecognizer.maximumNumberOfTouches = 1;
    [self.scrollView addGestureRecognizer:panGestureRecognizer];

}
-(void)upCommand
{
    NSLog(@"up");    
}
-(void)viewDidAppear:(BOOL)animated
{
    scrollView.contentSize = CGSizeMake(320, 600);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)recognizePan:(UIGestureRecognizer*)recognizer
{
    NSLog(@"pan");//Does nothing but catches the pan of scrollview so it doesnt scroll


}
//simultan recognition of pan and gesture
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

泛识别器在 Scrollview 中捕获单指滑动,因此视图不会滚动,然后添加

shouldRecognizeSimultaneouslyWithGestureRecognizer

使得平移和手势都将被捕获。

就这样

编辑你必须添加

<UIGestureRecognizerDelegate>

到你的 .h 文件

于 2013-01-17T16:09:07.113 回答