UITableView嵌套UICollectionView泳炉,UICollectionView彈性效果失靈問(wèn)題

在UITableView內(nèi)嵌套UICollectionView,實(shí)現(xiàn)聯(lián)動(dòng)效果

重寫(xiě)gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:方法

@implementation MyTableView

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];;
}

@end

監(jiān)聽(tīng)UICollectionView的滾動(dòng)

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageSelectViewScrolled:) name:@"image_picker_notice" object:nil];

在imageSelectViewScrolled內(nèi)控制UICollectionView的滾動(dòng)效果

- (void)imageSelectViewScrolled: (NSNotification *)notification{
    
    self.childVCScrollView = notification.object;
    NSLog(@"%lf", kTopHeight);
    if ((long long)self.tableView.contentOffset.y < (long long)kTopHeight) {
        self.childVCScrollView.contentOffset = CGPointZero;
        self.childVCScrollView.showsVerticalScrollIndicator = NO;
    } else {
        self.childVCScrollView.showsVerticalScrollIndicator = YES;
    }
    
}

在UICollectionView的代理函數(shù)scrollViewDidScroll內(nèi)發(fā)送通知

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSLog(@"%@", [scrollView class]);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"image_picker_notice" object:scrollView];
    
}

在tableView的代理函數(shù)scrollViewDidScroll控制tableView的滾動(dòng)效果

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
//    NSLog(@"%@", [scrollView class]);
    if (self.tableView == scrollView) {
        if ((self.childVCScrollView && self.childVCScrollView.contentOffset.y > 0) || (scrollView.contentOffset.y > kTopHeight)) {
             //限制self.tableView的滾動(dòng)
             self.tableView.contentOffset = CGPointMake(0, kTopHeight);
        }
     }
    
    
}

最后任洞,滾動(dòng)效果出現(xiàn)瑕疵蓄喇,當(dāng)UICollectionView滾動(dòng)到底部時(shí),第一次會(huì)出現(xiàn)彈簧效果交掏,之后就不會(huì)發(fā)生滾動(dòng)效果妆偏,只有UITableView的scrollViewDidScroll有效應(yīng)。

解決方案如下:

1盅弛、創(chuàng)建一個(gè)UIScrollView钱骂,將UIScrollView放在UITableView的cell上
2叔锐、將UICollectionView放在UIScrollView上

#import "ImagePickerViewController.h"
#import "ImageSelectViewController.h"

#define kTopHeight (CGRectGetHeight(self.preView.bounds) * 7 / 16)

@interface MyTableView : UITableView

@end

@implementation MyTableView

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];;
}

@end

@interface ImagePickerViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) MyTableView *tableView;
@property (nonatomic, strong) UIImageView *preView;
@property (nonatomic, strong) UIScrollView * _Nullable childVCScrollView;
@property (nonatomic, strong) UIView * _Nullable selectImageView;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation ImagePickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViews];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageSelectViewScrolled:) name:@"image_picker_notice" object:nil];
}

#pragma mark - Setup UI

- (void)setupViews{
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self.view addSubview:self.tableView];
    self.tableView.frame = self.view.bounds;
        
    self.preView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetWidth(self.view.bounds));
    self.tableView.tableHeaderView = self.preView;
    
}

#pragma mark - Methods

- (void)imageSelectViewScrolled: (NSNotification *)notification{
    
    self.childVCScrollView = notification.object;
    NSLog(@"%lf", kTopHeight);
    if ((long long)self.tableView.contentOffset.y < (long long)kTopHeight) {
        self.childVCScrollView.contentOffset = CGPointZero;
        self.childVCScrollView.showsVerticalScrollIndicator = NO;
    } else {
        self.childVCScrollView.showsVerticalScrollIndicator = YES;
    }
    
}

#pragma mark - Getter

- (MyTableView *)tableView{
    if (!_tableView) {
        _tableView = [[MyTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}


- (UIImageView *)preView{
    if (!_preView) {
        _preView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
        _preView.backgroundColor = UIColor.cyanColor;
    }
    return _preView;
}

- (UIView *)selectImageView{
    if (!_selectImageView) {
        ImageSelectViewController *selectVC = [[ImageSelectViewController alloc] init];
        [self addChildViewController:selectVC];
        _selectImageView = selectVC.view;
    }
    return _selectImageView;
}

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate = self;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
//    NSLog(@"%@", [scrollView class]);
    if (self.tableView == scrollView) {
        if ((self.childVCScrollView && self.childVCScrollView.contentOffset.y > 0) || (scrollView.contentOffset.y > kTopHeight)) {
             //限制self.tableView的滾動(dòng)
             self.tableView.contentOffset = CGPointMake(0, kTopHeight);
        }
     }
    
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return CGRectGetHeight(tableView.frame) - kTopHeight - 44;
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
    }
    [cell.contentView addSubview:self.scrollView];
    self.scrollView.frame = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), CGRectGetHeight(tableView.bounds) - kTopHeight - 44);
    [self.scrollView addSubview:self.selectImageView];
    self.selectImageView.frame = self.scrollView.bounds;


    return cell;
}
#import "ImageSelectViewController.h"
#import <TZImagePickerController/TZImagePickerController.h>

@interface MyCollectionView : UICollectionView

@end

@implementation MyCollectionView

@end


@interface ImageSelectViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) MyCollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation ImageSelectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupViews];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageTitleViewToTop) name:@"headerViewToTop" object:nil];
    
}

- (void)viewWillLayoutSubviews{
    self.collectionView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
}

#pragma mark - Setup UI

- (void)setupViews{
    
    self.view.backgroundColor = UIColor.whiteColor;
    
    [self.view addSubview:self.collectionView];
}

#pragma mark - Methods

- (void)pageTitleViewToTop{
    
}

/// 獲取相冊(cè)中的視頻和圖片
- (void)startFetchTheImage {
    
    if (![[TZImageManager manager] authorizationStatusAuthorized]) {
        NSLog(@"未授訪問(wèn)相冊(cè)的權(quán)利");
    }
    
    [self didGetAuth];

}

/// 獲取到權(quán)限
- (void)didGetAuth {
    
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        [TZImagePickerConfig sharedInstance].allowPickingImage = YES;
        [TZImagePickerConfig sharedInstance].allowPickingVideo = YES;
        [TZImageManager manager].sortAscendingByModificationDate = NO;
        
        [[TZImageManager manager] getCameraRollAlbum:YES allowPickingImage:YES needFetchAssets:YES completion:^(TZAlbumModel *model) {
            dispatch_async(dispatch_get_main_queue(), ^{
                                
                [self.dataArray removeAllObjects];
                
                // 拍攝按鈕 占位的model
                [self.dataArray addObject:[[TZAssetModel alloc] init]];
                
                                
                for (TZAssetModel * assetModel in model.models) {
                    if ((assetModel.type == TZAssetModelMediaTypeVideo && assetModel.asset.duration <= 61) || assetModel.type == TZAssetModelMediaTypePhoto) {
                        [self.dataArray addObject:assetModel];
                    }
                }
                
                // 第一次加載
                [self.collectionView reloadData];
                
            });

        }];
    });

}

#pragma mark - Getter

- (MyCollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        _collectionView = [[MyCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
    }
    return _collectionView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = CGRectGetWidth(self.view.frame);
    CGFloat itemWidth = (width - 3 * 5) * 1.0 / 4;
    return CGSizeMake(itemWidth, itemWidth);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

#pragma mark - UICollectionViewDelegate、UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 30;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColor.lightGrayColor;
    return cell;;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSLog(@"%@", [scrollView class]);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"image_picker_notice" object:scrollView];
    
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末见秽,一起剝皮案震驚了整個(gè)濱河市愉烙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌解取,老刑警劉巖步责,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異禀苦,居然都是意外死亡蔓肯,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)振乏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)省核,“玉大人,你說(shuō)我怎么就攤上這事昆码∑遥” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵赋咽,是天一觀的道長(zhǎng)旧噪。 經(jīng)常有香客問(wèn)我,道長(zhǎng)脓匿,這世上最難降的妖魔是什么淘钟? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮陪毡,結(jié)果婚禮上米母,老公的妹妹穿的比我還像新娘。我一直安慰自己毡琉,他們只是感情好铁瞒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著桅滋,像睡著了一般慧耍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上丐谋,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天芍碧,我揣著相機(jī)與錄音,去河邊找鬼号俐。 笑死泌豆,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的吏饿。 我是一名探鬼主播踪危,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼蔬浙,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了陨倡?” 一聲冷哼從身側(cè)響起敛滋,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤许布,失蹤者是張志新(化名)和其女友劉穎兴革,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體蜜唾,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杂曲,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了袁余。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片擎勘。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖颖榜,靈堂內(nèi)的尸體忽然破棺而出棚饵,到底是詐尸還是另有隱情,我是刑警寧澤掩完,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布噪漾,位于F島的核電站,受9級(jí)特大地震影響且蓬,放射性物質(zhì)發(fā)生泄漏欣硼。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一恶阴、第九天 我趴在偏房一處隱蔽的房頂上張望诈胜。 院中可真熱鬧,春花似錦冯事、人聲如沸焦匈。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)括授。三九已至,卻和暖如春岩饼,著一層夾襖步出監(jiān)牢的瞬間荚虚,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工籍茧, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留版述,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓寞冯,卻偏偏與公主長(zhǎng)得像渴析,于是被迫代替她去往敵國(guó)和親晚伙。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354