wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
//  HDLCoverFlow.m
//  CoverFlow
//
//  Created by JLCHEN on 2019/8/23.
//  Copyright © 2019 JLCHEN. All rights reserved.
//
 
#import "HDLCoverFlow.h"
#import "HDLCoverFlowCell.h"
#import "HDLCoverFlowLayout.h"
 
@interface HDLCoverFlow()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *mUICollectionView;
 
 
@end
 
 
@implementation HDLCoverFlow{
    
        NSArray<UIView *> *viewDataArray;
}
 
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.mUICollectionView];
    }
    return self;
}
 
#pragma mark- 子控件坐标
//当控件本身的尺寸发送改变时,系统会自动调用这个方法
- (void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat personW = self.frame.size.width;
    CGFloat personH = self.frame.size.height;
 
    self.mUICollectionView.frame = CGRectMake(0, 0, personW, personH);
    
}
 
#pragma mark- 懒加载
- (UICollectionView *)mUICollectionView{
    if (_mUICollectionView == nil) {
        HDLCoverFlowLayout *flowLayout = [[HDLCoverFlowLayout alloc]init];
        
        _mUICollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
        _mUICollectionView.backgroundColor = [UIColor lightGrayColor];
        // 设置数据源对象
        _mUICollectionView.dataSource = self;
        _mUICollectionView.delegate = self;
        // 注册cell
        [_mUICollectionView registerClass:[HDLCoverFlowCell class] forCellWithReuseIdentifier:@"CoverFlowID"];
   
 
    }
    return _mUICollectionView;
}
 
 
#pragma mark - 数据源方法
// 返回item的个数!
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return viewDataArray.count;
}
 
// 返回cell!
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 1.创建cell
    HDLCoverFlowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CoverFlowID" forIndexPath:indexPath];
    
    [cell setCustomViewCell: viewDataArray[indexPath.row]];
    // 3.返回cell
    return cell;
}
 
 
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    // 1.创建cell
//    ZHCoverFlowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CoverFlowID" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor redColor];
    
    NSLog(@"点击了:%ld",(indexPath.row));
    
    [_mSelectItemDelegate onSelectItem:indexPath.row];
}
 
 
#pragma mark - 加载数据
 
-(void)addCoverFlowViewDatas:(NSArray<UIView *> *) viewsData{
    viewDataArray = viewsData;
    [_mUICollectionView reloadData];
}
 
/**
 设置选中
 
 @param selectIndex
 */
-(void)setCoverFlowSelectIndex:(NSInteger) selectIndex{
    
    [self.mUICollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:selectIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
//    _mUICollectionView
}
 
 
/**
 重置布局
 
 @param mCGRect 布局
 
 */
-(void)initWithFrameArc:(CGRect) mCGRect{
    self.frame = mCGRect;
    _mUICollectionView.frame =  self.frame;
    _mUICollectionView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
}
 
@end