如何在滚动时在UIScrollView中移动一个方向

时间:2014-07-06 09:56:34

标签: ios objective-c uiscrollview

我是objective-c的新手。我创建了UIScrollView个对象,并在我的视图中添加了以下代码:

height = self.view.frame.size.height;
width = self.view.frame.size.width;

scrollbar = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
scrollbar.delegate = self;
scrollbar.backgroundColor = [UIColor whiteColor];
scrollbar.maximumZoomScale = 1.0;
scrollbar.minimumZoomScale = 1.0;
scrollbar.clipsToBounds = YES;
scrollbar.showsHorizontalScrollIndicator = YES;
scrollbar.pagingEnabled = YES;
[scrollbar setContentSize:CGSizeMake(width*4, height*4)];
[self.view addSubview:scrollbar];

for (int i = 1; i <= 4; i++) {
    first = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    first.view.frame = CGRectMake((i-1)*width, 0, width, height*4);
    [scrollbar addSubview:first.view];

    switch (i) {
          ase 1:
              first.view.backgroundColor = [UIColor blueColor];
              break;
          case 2:
              first.view.backgroundColor = [UIColor redColor];
              break;
          case 3:
              first.view.backgroundColor = [UIColor greenColor];
              break;
          case 4:
              first.view.backgroundColor = [UIColor grayColor];
              break;
          default:
              break;
        }
    }

在我的代码中,我在我的ScrollView中添加了4个不同颜色的视图,现在我想要滚动我的ScrollView检测dx&amp; dy(dx:Axis.x上的行驶距离&amp; dy:Axis.y上的行驶距离)并检查这两个变量以及何时:

Notic:我希望当任何人触摸ScrollView并在Axis上移动触摸(x或y)或触摸Both Axis(x和y)时检查:

if(dx&gt; dy)禁用水平滚动并在垂直方向移动!!! 否则在水平方向移动并禁用垂直滚动!!!

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGRect visibleRect = CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, scrollView.contentOffset.x + scrollView.bounds.size.width, scrollView.contentOffset.y + scrollView.bounds.size.height);

    NSLog(@"%f,%f",visibleRect.origin.x,visibleRect.origin.y);

    /*NSLog(@"x : %f",scrollView.contentOffset.x);
    NSLog(@"y : %f",scrollView.contentOffset.y);*/
    if (fabsf(scrollView.contentOffset.x) > fabsf(scrollView.contentOffset.y)) {
        NSLog(@"Vertical Side");
    }
    else {
        NSLog(@"Horizontal Side");
    }
}

请指导我们。我无法禁用一侧并移动另一侧!感谢

2 个答案:

答案 0 :(得分:6)

如果我理解你正确,你想要禁用用户拖动的方向滚动。如果是这样,UIScrollView已经提供了这样做的选项:

scrollView.directionalLockEnabled = YES;

当此选项设置为true时,UIScrollView将自行处理正确的方向锁定。

有关详细信息,请查看文档:{​​{3}}

答案 1 :(得分:1)

您可以获得想要向代理添加一些代码的结果。这是我的ViewController.m文件。 -viewDidLoad#import语句和其他方法被省略。

@interface ViewController () {
    CGPoint initialOffset;
    NSInteger direction; // 0 undefined, 1 horizontal, 2 vertical
}
@end

@implementation ViewController

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // retrieve current offset
    CGPoint currentOffset = scrollView.contentOffset;

    // do we know which is the predominant direction?
    if (direction == 0) {

        // no.

        CGFloat dx = currentOffset.x - initialOffset.x;
        CGFloat dy = currentOffset.y - initialOffset.y;

        // we need to decide in which direction we are moving

        if (fabs(dx) >= fabs(dy)) {
            direction = 1; // horizontal
        } else if (fabs(dy) > fabs(dx)) {
            direction = 2;
        }

    }

    // ok now we have the direction. update the offset if necessary
    if (direction == 1 && currentOffset.y != initialOffset.y) {

        // remove y offset
        currentOffset.y  = initialOffset.y;
        // update
        [scrollView setContentOffset:currentOffset];

    } else if (direction == 2 && currentOffset.x != initialOffset.x) {

        currentOffset.x = initialOffset.x;
        [scrollView setContentOffset:currentOffset];

    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // store the current offset
    initialOffset = scrollView.contentOffset;

    // reset flag
    direction = 0; // AKA undefined
}

@end

当您开始拖动时,委托会将标志direction重置为“未知”状态,并存储当前内容偏移量。每次拖动后,都会调用-scrollViewDidScroll:。在那里,您决定哪个是主要方向(如果尚未完成)并通过删除x(或y)偏移来相应地校正当前滚动偏移。

我使用您提供的相同设置对此进行了测试,只是我在UIImageView内使用了UIScrollView并通过InterfaceBuilder设置了所有内容,但它应该可以正常工作。从理论上讲,使用此方法可以替换directionLock,但请记住在操作期间多次调用-scrollViewDidScroll,并且每次重写内容偏移时(如果滚动在两个方向上进行)。因此,如果您启用directionLock,则可以保存代理执行的setContentOffset:的许多调用。

相关问题