从ScrollViewController内容视图导航到堆栈上的另一个视图

时间:2012-10-05 22:57:05

标签: iphone ios uiviewcontroller navigation

我正在尝试使用UIButton从UIScrollView的内容视图导航到另一个视图。我不想以模态方式显示其他视图,并且推送堆栈上的视图对我不起作用。

实施例: 我的Viewcontroller包含一个包含2个内容视图的Scrollview。在这些内容视图中,我想将另一个视图推送到堆栈中。

我正在尝试做什么?

1 个答案:

答案 0 :(得分:1)

好的,让我们看看我是否理解你的问题。

@interface MyViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *firstPlayerView;
@property (nonatomic, strong) UIView *secondPlayerView;
@end

@implementation MyViewController

-(UIView *)firstPlayerView
{
    if (!_firstPlayerView) {
        _firstPlayerView = [[UIView alloc] initWithFrame:self.view.bounds];
        // set up your view as you like and place the button to go to the second player
        // view when you need to.
        // let's suppose that you called that button "goToSecondPlayerViewButton"
        [goToSecondPlayerViewButton addTarget:self
                                       action:@selector(switchPlayer) 
                             forControlEvents:UIControlEventTouchUpInside];
    }
    return _firstPlayerView;
}

-(UIView *)secondPlayerView
{
    if (!_secondPlayerView) {
        _secondPlayerView = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0)];
        // set up your view as you like and place the button to go to the first player
        // view when you need to.
        // let's suppose that you called that button "goToFirstPlayerViewButton"
        [goToFirstPlayerViewButton addTarget:self
                                      action:@selector(switchPlayer) 
                            forControlEvents:UIControlEventTouchUpInside];
    }
    return _secondPlayerView;
}

-(UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        _scrollView.autoresizingMask = UIViewAutoresizingNone;
        [_scrollView addSubview:self.firstPlayerView];
        [_scrollView addSubview:self.secondPlayerView];
        _scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.scrollEnabled = NO;
        _scrollView.bounces = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

-(void)switchPlayer
{
    if(self.scrollView.contentOffset.x == 0) {
        [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:YES];
        self.title = @"Second Player";
    } else {
        [self.scrollView scrollRectToVisible:self.scrollView.bounds animated:YES];
        self.title = @"First Player";
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.scrollView];
    self.title = @"First Player";
}

@end

我希望能解决你的问题!我没有测试过代码,所以如果你遇到问题只是评论,我会尽力帮助。

已编辑:要添加视图,只需放大滚动视图contentSize并将该视图添加为其子视图

-(UIView *)thirdView
{
    if (!_thirdView) {
        _thirdView = [[UIView alloc] initWithFrame:CGRectOffset(self.view.bounds * 2, self.view.bounds.size.width, 0)];
        // set up your view as you like 
    }
    return _thirdView;
}

-(void)addThirdView
{
    self.scrollView.contentSize = CGSizeMake(self.scrollView.bounds.size.width * 3, self.scrollView.bounds.size.height);
    [self.scrollView addSubview:self.thirdView];
    [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width * 2, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) animated:YES];
}

您可以对此进行概括并先设置内容大小,然后使用一个方法将索引作为参数。

-(void)scrollToViewAtIndex:(NSInteger)index
{
    [self.scrollView scrollRectToVisible:CGRectMake(self.scrollView.bounds.size.width * index, 0, self.scrollView.bounds.size.width, self.scrollView.bounds.size.height) 
                                animated:YES]; 
}
相关问题