ScrollView中的2个手指平底锅带其他手势 - iOS SDK

时间:2013-01-17 06:13:17

标签: ios objective-c uiscrollview uigesturerecognizer

我有一个主UIView,其中包含一个scrollview。我使用此代码为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开发者&新手势

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

CustomScrollView.h:

@interface CustomScrollView : UIScrollView 
{
}

@end

CustomScrollView.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

ViewController.h:

#import <UIKit/UIKit.h>

@class CustomScrollView;

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

@end

ViewController.m:

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

4 个答案:

答案 0 :(得分:2)

子类ScrollView并实现以下方法:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
  if(gestureRecognizer.numberOfTouches != 2) {
    return NO;
  } else {
    return YES;
  }
}

答案 1 :(得分:0)

尝试子类化UIScrollView并覆盖方法touchesShouldBegin:withEvent:inContentView:。文档说明如下:

  

当手指触及显示的内容时,子类重写以自定义默认行为。

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

因此,您可以检查是否有多次触摸,只返回真实值。

答案 2 :(得分:0)

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

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

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

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

答案 3 :(得分:-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;
}

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

shouldRecognizeSimultaneouslyWithGestureRecognizer

使得pan和手势都会被捕获。

那就是

编辑 你必须添加

<UIGestureRecognizerDelegate>

到你的.h文件