禁用UIScrollView中对角线拖动(Disable diagonal dragging in

2019-09-28 18:43发布

我使用的UIScrollView加载我的两个视图让说AB。 它究竟看起来像下面的图片。 我设置PagingEnabledYESDirectionLockEnabledYES。

当我拖累从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中只在一个方向同时滚动 的http:// chandanshetty01.blogspot.ae/2012/07/restricting-diagonal-scrolling-in.html

所有帮助表示赞赏!

Answer 1:

设置directionalLockEnabled = YES; 之后您重置滚动视图的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;
}


文章来源: Disable diagonal dragging in UIScrollview