好的,这是简短的答案:
你必须使用UIGestureRecognizer
's 方法-requireGestureRecognizerToFail:
。
这是长答案:
只有当平移手势识别器TimerViewController
失败时,您才必须使滚动视图的平移手势识别器成功。但是,该手势(TimerViewController
的手势)只有在初始移动是垂直的情况下才能成功。如果它是水平的,它应该会失败。为了实现这一点,我们必须对其进行子类化UIPanGestureRecognizer
和修改以适应这些需求。这是你必须做的:
- 忽略您对我之前的回答所做的所有更改
- 添加
VerticalPanGestureRecognizer
到您的项目中。
- 如图修改
TimerViewController
。
- 如图修改
ScrollViewController
。
VerticalPanGestureRecognizer.h
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface VerticalPanGestureRecognizer : UIPanGestureRecognizer
@end
VerticalPanGestureRecognizer.m
#import "VerticalPanGestureRecognizer.h"
@interface VerticalPanGestureRecognizer ()
@property (nonatomic, assign) CGPoint origLoc;
@end
@implementation VerticalPanGestureRecognizer
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
self.origLoc = [[touches anyObject] locationInView:self.view.superview];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.state == UIGestureRecognizerStatePossible) {
CGPoint loc = [[touches anyObject] locationInView:self.view.superview];
CGFloat deltaX = fabs(loc.x - self.origLoc.x);
CGFloat deltaY = fabs(loc.y - self.origLoc.y);
if (deltaY < deltaX)
self.state = UIGestureRecognizerStateFailed;
}
[super touchesMoved:touches withEvent:event];
}
@end
TimerViewController.h
// Your imports here
@interface TimerViewController : UIViewController
{
// Your ivars here
}
// Add the following property
@property (nonatomic, strong) UIPanGestureRecognizer *pan;
// Your methods here
@end
TimerViewController.m
#import "TimerViewController.h"
#import "VerticalPanGestureRecognizer.h"
@implementation TimerViewController
@synthesize pan = _pan;
// prefersStatusBarHidden method here
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil // Initialise view controller
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Instantiate the pan gesture as "VerticalPanGestureRecognizer"
self.pan = [[VerticalPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; // Create recogniser for a pan guesture
self.pan.maximumNumberOfTouches = self.pan.minimumNumberOfTouches = 1;
[self.view addGestureRecognizer:self.pan];
}
return self;
}
// The rest of your code here
@end
滚动视图控制器.m
- (void)viewDidLoad
{
// Your code here
TimerViewController *tvc = [[TimerViewController alloc]init];
CGRect frame = tvc.view.frame;
frame.origin.x = 320;
tvc.view.frame = frame;
// Add the following line
[self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:tvc.pan];
[self addChildViewController:tvc];
[self.scrollView addSubview:tvc.view];
[tvc didMoveToParentViewController:self];
// More code here
}
这种新方法非常有效。我测试了它。如果您有更多问题,请告诉我。
干杯!
更新
要回答您在评论中发布的问题,您需要执行以下操作:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
BarsViewController *bvc = [[BarsViewController alloc]init];
[self addChildViewController:bvc];
[self.scrollView addSubview:bvc.view];
[bvc didMoveToParentViewController:self];
TimerViewController *tvc = [[TimerViewController alloc]init];
CGRect frame = tvc.view.frame;
frame.origin.x = 320;
tvc.view.frame = frame;
[self.scrollView.panGestureRecognizer requireGestureRecognizerToFail:tvc.pan];
[self addChildViewController:tvc];
[self.scrollView addSubview:tvc.view];
[tvc didMoveToParentViewController:self];
StopwatchViewController *svc = [[StopwatchViewController alloc] init];
frame = svc.view.frame;
frame.origin.x = 320*2;
svc.view.frame = frame;
[self addChildViewController:svc];
[self.scrollView addSubview:svc.view];
[svc didMoveToParentViewController:self];
self.scrollView.contentSize = CGSizeMake(320*3, self.view.frame.size.height);
self.scrollView.pagingEnabled = YES;
[self.scrollView setShowsHorizontalScrollIndicator:NO];
}
再次,我测试了它并且它正在工作。您只需为条形添加手势识别器