ScrollView向右滑动不能正常工作XCode

时间:2013-12-25 03:24:58

标签: iphone objective-c

我有一个scrollView,其中添加了子视图,这些子视图可以向上,向右和向左滑动。但由于某种原因,滑动右侧似乎根本不起作用。我对Objective-C完全不熟悉,因为我一直努力使其成功。我有一个主视图控制器实现与以下代码: 我的MainViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    _headerViewController = [self.storyboard instantiateViewControllerWithIdentifier:kHeaderId];
    _headerViewController.delegate = self;
    [scrollView addSubview:_headerViewController.view];

    _actionsListViewController = [self.storyboard instantiateViewControllerWithIdentifier:kActionsListId];
    _actionsListViewController.delegate = self;
    [scrollView insertSubview:_actionsListViewController.view
                 belowSubview:_headerViewController.view];

    _agendasListViewController = [self.storyboard instantiateViewControllerWithIdentifier:KAgendasListId];
    _agendasListViewController.delegate = self;
    [scrollView insertSubview:_agendasListViewController.view
                belowSubview:_headerViewController.view];    
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self positionSearchField:YES];

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 2,
                                        scrollView.frame.size.height * 2);


    _headerViewController.view.frame = CGRectMake(0, 0,
                                                  scrollView.frame.size.width,
                                                  scrollView.frame.size.height);
    //action view will be displayed when swiped left
    _actionsListViewController.view.frame = CGRectMake(scrollView.frame.size.width, 0,
                                                       scrollView.frame.size.width,
                                                       scrollView.frame.size.height);
    //agenda view should have been displayed when swiped right
    _agendasListViewController.view.frame = CGRectMake(-scrollView.frame.size.width, 0,
                                                       scrollView.frame.size.width,
                                                       scrollView.frame.size.height); 

    _scrollViewBounds = scrollView.bounds;
}

viewShouldScroll函数

- (void)viewShouldScroll:(UISwipeGestureRecognizerDirection)direction {
    CGRect frame = CGRectMake(scrollView.contentOffset.x,
                              scrollView.contentOffset.y,
                              scrollView.frame.size.width,
                              scrollView.frame.size.height);    


    UIViewController *currentController = [self visibleViewController];

    switch (direction) {
        case UISwipeGestureRecognizerDirectionUp:
            frame.origin.y += scrollView.frame.size.height;
            break;
        case UISwipeGestureRecognizerDirectionLeft:
            //works fine and displays action view
            frame.origin.x += scrollView.frame.size.width;
            break;
        case UISwipeGestureRecognizerDirectionDown:
            frame.origin.y -= scrollView.frame.size.height;
            break;
        case UISwipeGestureRecognizerDirectionRight:
            if( currentController == _headerViewController ) {
                //does not work for agenda when swiped right
                NSLog(@"x position is %f@", frame.origin.x);
                frame.origin.x = -320; //hardcoded to check if this works, but doesnot
                NSLog(@"width is %f@", scrollView.frame.size.width); //gives -320
            }
            else {       
                //works fine when swiped from left to right to show main view       
                frame.origin.x -= scrollView.frame.size.width; //0
            }            
            break;

        default:
            break;
    }     
    [scrollView scrollRectToVisible:frame animated:YES];
}

从故事板中添加了手势识别器,用于LEFT,RIGHT和TOP。根据代码,我在CGRectMake中添加了位置为x为-320的议程视图,并且在向右滑动时,我试图通过更改{{1}的x点来使其可见但它似乎不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我设法这样做::

...
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3,
                                        scrollView.frame.size.height * 2);
...

我使用scrollView的宽度作为scrollView.frame.size.width * 2,这使得滚动视图的总宽度加倍scrollView.frame.size.width,但由于我在主scrollView中有3个子视图},我需要创建scrollView,其宽度是scrollView帧宽度的3倍scrollView.frame.size.width * 3