【发布时间】:2013-01-17 06:13:16
【问题描述】:
我有一个主 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 开发人员和手势新手
编辑:根据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?
}
【问题讨论】:
-
我已经回答了你的问题,试试吧,它对我有用 =)
-
如果对您没有帮助,请接受答案或添加评论
标签: ios objective-c uiscrollview uigesturerecognizer