在UIScrollview中禁用对角拖动 [英] Disable diagonal dragging in UIScrollview
问题描述
我正在使用UIScrollview加载我的两个视图,比如说 A 和 B .它看起来完全像下面的图片.我将 PagingEnabled 设置为 YES ,将 DirectionLockEnabled 设置为 YES .
I am using UIScrollview to load my two views let say A and B. It exactly looks like the below picture. I set PagingEnabled as YES and DirectionLockEnabled as YES.
当我将视图从A拖动到B时,它会成对角线.请在下面找到代码,
When I drag the view from A to B it goes diagonally. Please find the code below,
- (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一次只能在一个方向上滚动 http://chandanshetty01.blogspot.ae/2012/07/stricting-diagonal-scrolling-in.html
I check with these following links but I did't get a solution yet, 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
感谢所有帮助!!
推荐答案
设置 directionLockEnabled = YES;
在重置滚动视图的contentSize后
set directionalLockEnabled = YES;
after you reset the contentSize of the scrollview
[self.scrollView setContentSize:CGSizeMake((self.scrollView.frame.size.width-15)*2, self.ViewB.frame.size.height)];
self.scrollView.directionalLockEnabled = YES; // add this here.
这样做可以解决您的问题
By doing like this, it will fix your issue
基于您的代码的答案
- (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;
}
这篇关于在UIScrollview中禁用对角拖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!