在UIScrollview中禁用对角线拖动

我正在使用UIScrollview来加载我的两个视图,比如说AB. 它看起来如下图所示。 我将PagingEnabled设置为YES ,将DirectionLockEnabled设置YES

当我将视图从A拖动到B时,它会对角线移动。 请找到下面的代码,

- (void)setupScrollView:(UIScrollView*)scrMain { for (int i = 0; i = YOffset) ) { scrollView.pagingEnabled = YES; scrollDirection = ScrollDirectionHorizontal; } else { scrollView.pagingEnabled = NO; scrollDirection = ScrollDirectionVertical; } } 

在此处输入图像描述

我查看以下这些链接,但我还没有得到解决方案, http: //bogdanconstantinescu.com/blog/proper-direction-lock-for-uiscrollview.html UIScrollView一次只向一个方向滚动 http:// chandanshetty01.blogspot.ae/2012/07/restricting-diagonal-scrolling-in.html

所有帮助表示赞赏!!

set directionalLockEnabled = YES; 重置scrollview的contentSize后

 [self.scrollView setContentSize:CGSizeMake((self.scrollView.frame.size.width-15)*2, self.ViewB.frame.size.height)]; self.scrollView.directionalLockEnabled = YES; // add this here. 

通过这样做,它将解决您的问题

根据您的代码回答

 - (void)viewDidLoad { [super viewDidLoad]; [self setupScrollView:self.scrollView]; self.scrollView.directionalLockEnabled = YES; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)setupScrollView:(UIScrollView*)scrMain { for (int i = 0; i < 2; i++) { if (i == 0) { // View A self.ViewA = [[UIView alloc] init]; self.ViewA.backgroundColor = [UIColor blackColor]; [self.ViewA setFrame:CGRectMake((i)*self.scrollView.frame.size.width + 20, 0, self.scrollView.frame.size.width - 40, 800)]; [self.scrollView addSubview:self.ViewA]; } else if (i == 1) { // View B self.ViewB = [[UIView alloc] init]; self.ViewB.backgroundColor = [UIColor blueColor]; [self.ViewB setFrame:CGRectMake((i)*self.ViewA.frame.size.width + 30, 0, self.scrollView.frame.size.width - 40, 800)]; [self.scrollView addSubview:self.ViewB]; } } [self.scrollView setContentSize:CGSizeMake((self.scrollView.frame.size.width-15)*2, self.ViewB.frame.size.height)]; self.scrollView.directionalLockEnabled = YES; }