在 UIScrollview 中禁用对角线拖动
Posted
技术标签:
【中文标题】在 UIScrollview 中禁用对角线拖动【英文标题】:Disable diagonal dragging in UIScrollview 【发布时间】:2017-05-02 06:57:54 【问题描述】:我正在使用 UIScrollview 加载我的两个视图,比如说 A 和 B。如下图所示。我将 PagingEnabled 设置为 YES 并将 DirectionLockEnabled 设置为 YES。
当我将视图从 A 拖到 B 时,它是对角线的。请在下面找到代码,
- (void)setupScrollView:(UIScrollView*)scrMain
for (int i = 0; i < 2; i++)
if (i == 0)
// View A
[ViewA setFrame:CGRectMake((i)*scrollview.frame.size.width + 20, 0, scrollview.frame.size.width - 40, ViewA.frame.size.height)];
[scrollview addSubview:ViewA];
else if (i == 1)
// View B
[ViewB setFrame:CGRectMake((i)*ViewB.frame.size.width + 30, 0, scrollview.frame.size.width - 40, ViewB.frame.size.height)];
[scrollview addSubview:ViewB];
[scrollview setContentSize:CGSizeMake((scrollview.frame.size.width-15)*2, ViewB.frame.size.height)];
- (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView
self.oldContentOffset = scroller.contentOffset;
- (void) scrollViewDidScroll: (UIScrollView *) scrollView
float XOffset = fabs(self.oldContentOffset.x - scrollView.contentOffset.x );
float YOffset = fabs(self.oldContentOffset.y - scrollView.contentOffset.y );
if (scrollView.contentOffset.x != self.oldContentOffset.x && (XOffset >= YOffset) )
scrollView.pagingEnabled = YES;
scrollDirection = ScrollDirectionHorizontal;
else
scrollView.pagingEnabled = NO;
scrollDirection = ScrollDirectionVertical;
我查看了以下链接,但我还没有找到解决方案, http://bogdanconstantinescu.com/blog/proper-direction-lock-for-uiscrollview.html UIScrollView scrolling in only one direction at a time http://chandanshetty01.blogspot.ae/2012/07/restricting-diagonal-scrolling-in.html
感谢所有帮助!
【问题讨论】:
我这样做的方式是不允许包含页面的滚动视图滚动两种方式(所以在这种情况下只是水平滚动)。并允许该滚动视图中的视图(在本例中为 A 和 B)具有自己的滚动方式(在本例中为垂直)。 【参考方案1】:重置滚动视图的 contentSize 后设置directionalLockEnabled = YES;
[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;
【讨论】:
问题仍然存在。 奇怪.. 它为我工作。我现在试了一下,它对我有用。 能否请您发布您的完整代码。会很有用的 评论的行数更多,所以我在答案中编辑并添加了代码。 directionalLockEnabled 不会禁用对角滚动,请参阅:developer.apple.com/documentation/uikit/uiscrollview/…以上是关于在 UIScrollview 中禁用对角线拖动的主要内容,如果未能解决你的问题,请参考以下文章