0

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

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

我正在尝试做的事情可能吗?

4

1 回答 1

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]; 
}
于 2012-10-06T11:08:56.010 回答