iOS实现切换房间效果

本文介绍了如何使用iOS开发中,通过内容视图和三个UIViewController实现图片轮播,同时管理视图滚动、数据加载和视图切换。关键在于利用UIScrollView和滚动动画,配合currentIndex变量控制数据源的切换。
摘要由CSDN通过智能技术生成

请添加图片描述

    [self.contentView addSubview:self.lastVC.view];
    [self.contentView addSubview:self.currentVC.view];
    [self.contentView addSubview:self.willShowVC.view];
    self.lastVC.tableView.backgroundColor = [UIColor redColor];
    self.currentVC.tableView.backgroundColor = [UIColor cyanColor];
    self.willShowVC.tableView.backgroundColor = [UIColor greenColor];
    self.lastVC.view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight - SafeAreaTopHeight);
    self.currentVC.view.frame = CGRectMake(0, (ScreenHeight - SafeAreaTopHeight), ScreenWidth, ScreenHeight - SafeAreaTopHeight);
    self.willShowVC.view.frame = CGRectMake(0, (ScreenHeight - SafeAreaTopHeight) * 2, ScreenWidth, (ScreenHeight - SafeAreaTopHeight));
- (UIScrollView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, ScreenWidth, ScreenHeight - SafeAreaTopHeight)];
        _contentView.contentSize = CGSizeMake(ScreenWidth, (ScreenHeight - SafeAreaTopHeight) * 3);
        _contentView.scrollEnabled = NO;
    }
    return _contentView;
}

- (CUrrentViewController *)lastVC
{
    if (!_lastVC) {
        _lastVC = [[CUrrentViewController alloc] init];
    }
    return _lastVC;
}

- (CUrrentViewController *)currentVC
{
    if (!_currentVC) {
        _currentVC = [[CUrrentViewController alloc] init];
        WEAKSELF;
        _currentVC.loadMoreBlock = ^{
        ///中间视图上拉加载
            [weakSelf.currentVC openRefresh];
            weakSelf.currentIndex ++;
            weakSelf.currentIndex = MIN(self.dataArray.count - 1, weakSelf.currentIndex);
            [UIView animateWithDuration:0.5 animations:^{
                [weakSelf.contentView setContentOffset:CGPointMake(0, (ScreenHeight - SafeAreaTopHeight) * 2)];
            } completion:^(BOOL finished) {
                [weakSelf reloadAfterTranslation];
               }];
        };
        _currentVC.pull = ^{
        ///中间视图下啦刷新
            [weakSelf.currentVC openRefresh];
            weakSelf.currentIndex --;
            weakSelf.currentIndex = MAX(0, weakSelf.currentIndex);
            [UIView animateWithDuration:0.5 animations:^{
                [weakSelf.contentView setContentOffset:CGPointMake(0, 0)];
            } completion:^(BOOL finished) {
                [weakSelf reloadAfterTranslation];
            }];
        };
    }
    return _currentVC;
}

- (CUrrentViewController *)willShowVC
{
    if (!_willShowVC) {
        _willShowVC = [[CUrrentViewController alloc] init];
    }
    return _willShowVC;
}

/// 移动完成之后,要恢复原位,并切换展示的数据

- (void)reloadAfterTranslation
{
    NSArray *array = self.dataArray[self.currentIndex];
    [self.currentVC reloadWithData:array];
    
    if (self.currentIndex - 1 >= 0) {
        NSArray *lastArray = self.dataArray[self.currentIndex - 1];
        [self.lastVC reloadWithData:lastArray];
    }
    if (self.currentIndex + 1 < self.dataArray.count) {
        NSArray *nextArray = self.dataArray[self.currentIndex + 1];
        [self.willShowVC reloadWithData:nextArray];
    }

    if (self.currentIndex == 0) {
        [self.currentVC closeHeaderRefresh];
    }
    if (self.currentIndex == self.dataArray.count - 1) {
        [self.currentVC closeFooterLoad];
    }
    //将偏移量恢复到原位,展示中间的视图
    [self.contentView setContentOffset:CGPointMake(0, (ScreenHeight - SafeAreaTopHeight))];
}

该逻辑可以应用于图片轮播

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值