滚动时如何在 UIScrollView 中移动一个方向 [英] how to move one direction in UIScrollView when scrolling

查看:16
本文介绍了滚动时如何在 UIScrollView 中移动一个方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Objective-c 的新手.我创建 UIScrollView 对象并使用以下代码添加到我的视图中:

I'm new in objective-c. I create UIScrollView object and add in my view with this code:

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 &dy(dx:Axis.x 上的行驶距离和 dy:Axis.y 上的行驶距离)并检查这两个变量以及何时:

in my code I add 4 view in my ScrollView with different color now I want when scrolling on my ScrollView detect dx & dy (dx: driving distance on Axis.x & dy:driving distance on Axis.y) and check these two variable and when :

注意:当任何一次触摸 ScrollView 并移动触摸轴(x 或 y)或触摸两个轴(x 和 y)时,我想检查一下:

Notic: I want when any one touch on ScrollView and moving touch on Axis (x or y) or touch on Both Axis (x and y) check this :

if (dx > 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");
    }
}

请指导我.我不能禁用一侧并移动另一侧!!!谢谢

please guide me guys. I can't disable one side and move another side!!! thanks

推荐答案

你可以实现你想要的结果添加一些代码到你的委托.这是我的 ViewController.m 文件.-viewDidLoad#import语句等方法省略.

You can achieve the result you want adding some code to your delegate. This is my ViewController.m file. -viewDidLoad, #import statements and the other methods are omitted.

@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)偏移量来相应地更正当前滚动偏移量.

When you start dragging, the delegate will reset the flag direction to "unknown" state, and store the current content offset. After every dragging move, your -scrollViewDidScroll: will be called. There, you decide which is the predominant direction (if this hasn't been done yet) and correct the current scrolling offset accordingly, by removing the x (or y) offset.

我使用您提供的相同设置对此进行了测试,只是我在 UIScrollView 中使用了一个 UIImageView 并通过 InterfaceBuilder 设置了所有内容,但它应该可以正常工作.理论上,用这个方法你可以替换directionLock,但是记住-scrollViewDidScroll在一个动作中被调用了很多次,并且每次它都会重写内容偏移量(如果滚动是双向发生).因此,如果您让 directionLock 处于启用状态,您可以保存委托执行的许多对 setContentOffset: 的调用.

I tested this with the same settings you provided, only I used a UIImageView inside UIScrollView and I set up everything via InterfaceBuilder, but it should work fine. Theoretically, with this method you could replace directionLock, but remember that -scrollViewDidScroll is called many times during an action, and every time it rewrites the content offset (if the scrolling is happening in both directions). So if you leave directionLock enabled, you save many of the calls to setContentOffset: that the delegate performs.

这篇关于滚动时如何在 UIScrollView 中移动一个方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆