UICollectionView

效果圖.png

1扫步、MHConnectDeviceTipsView.h 文件:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MHConnectDeviceTipsView : UIView

@property (nonatomic, copy) NSArray * dataSource;//數(shù)據(jù)源

@end

NS_ASSUME_NONNULL_END

2俱尼、MHConnectDeviceTipsView.m 文件:

#import "MHConnectDeviceTipsView.h"
#import "MHConnectDeviceTipsCell.h"
#import "MHConnectDeviceTipsModel.h"

@interface MHConnectDeviceTipsView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIImageView * bgImageView;//背景圖
@property (nonatomic, strong) UICollectionView * collectionView;

@end

@implementation MHConnectDeviceTipsView

#pragma mark - Getter

- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_tips"]];
        _bgImageView.contentMode = UIViewContentModeScaleAspectFit;
        _bgImageView.frame = CGRectMake(self.bounds.size.width-103, self.bounds.size.height-130, 103, 130);
    }
    return _bgImageView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        _collectionView.pagingEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[MHConnectDeviceTipsCell class] forCellWithReuseIdentifier:@"MHConnectDeviceTipsCell"];
    }
    return _collectionView;
}


#pragma mark - Setter

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    [self.collectionView reloadData];
}


#pragma mark - Life Cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}


#pragma mark - UI

- (void)prepareUI {
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 8.0;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.bgImageView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    
    //配置【固定】數(shù)據(jù)源
    NSArray * describesArray = @[NeuLocalizedString(@"If your plans changs, you can adjust your AC's schedule for that day"), NeuLocalizedString(@"Your AC can automatically adjust the temperature to your preferred setting while you sleep. Saving your energy and keeping you comfortable."), NeuLocalizedString(@"You can share your connected device to your family members or friends to control at the same time.")];
    NSMutableArray * modelsArray = [NSMutableArray new];
    for (NSString * describeStr in describesArray) {
        MHConnectDeviceTipsModel * model = [[MHConnectDeviceTipsModel alloc] init];
        model.title = NeuLocalizedString(@"Did you know?");
        model.describe = describeStr;
        [modelsArray addObject:model];
    }
    self.dataSource = modelsArray;
}


#pragma mark - UICollectionViewDataSource, UICollectionViewDelegateFlowLayout

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MHConnectDeviceTipsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MHConnectDeviceTipsCell" forIndexPath:indexPath];
    cell.model = (MHConnectDeviceTipsModel *)self.dataSource[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.bounds.size.width, self.bounds.size.height);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

@end

3抖韩、MHConnectDeviceTipsCell.h 文件:

#import <UIKit/UIKit.h>
#import "MHConnectDeviceTipsModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface MHConnectDeviceTipsCell : UICollectionViewCell

@property (nonatomic, strong) MHConnectDeviceTipsModel * model;

@end

NS_ASSUME_NONNULL_END

4缤弦、MHConnectDeviceTipsCell.m 文件:

#import "MHConnectDeviceTipsCell.h"

@interface MHConnectDeviceTipsCell ()

@property (nonatomic, strong) UILabel * titleLabel;//標題
@property (nonatomic, strong) UILabel * describeLabel;//說明

@end

@implementation MHConnectDeviceTipsCell

#pragma mark - Getter

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = MEDIUMSYSTEMFONT(16);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UILabel *)describeLabel {
    if (!_describeLabel) {
        _describeLabel = [[UILabel alloc] init];
        _describeLabel.textColor = COLOR_TEXT_THIRDLEVEL;
        _describeLabel.font = SYSTEMFONT(14);
        _describeLabel.textAlignment = NSTextAlignmentCenter;
        _describeLabel.numberOfLines = 0;
    }
    return _describeLabel;
}


#pragma mark - Setter

- (void)setModel:(MHConnectDeviceTipsModel *)model {
    _model = model;
    
    self.titleLabel.text = _model.title;
    self.describeLabel.text = _model.describe;
}


#pragma mark - Life Cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI];
    }
    return self;
}


#pragma mark - UI

- (void)prepareUI {
    self.contentView.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.describeLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(24);
        make.left.equalTo(self.contentView).offset(32);
        make.right.equalTo(self.contentView).offset(-32);
        make.height.mas_equalTo(20);
    }];
    [self.describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(32);
        make.right.equalTo(self.contentView).offset(-32);
        make.bottom.lessThanOrEqualTo(self.contentView.mas_bottom).offset(-24);
    }];
}

@end

5谢鹊、MHConnectDeviceTipsModel.h 文件:

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MHConnectDeviceTipsModel : NSObject

@property (nonatomic, copy) NSString * title;//標題
@property (nonatomic, copy) NSString * describe;//描述

@end

NS_ASSUME_NONNULL_END
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末浴滴,一起剝皮案震驚了整個濱河市拓萌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌巡莹,老刑警劉巖司志,帶你破解...
    沈念sama閱讀 222,104評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異降宅,居然都是意外死亡骂远,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評論 3 399
  • 文/潘曉璐 我一進店門腰根,熙熙樓的掌柜王于貴愁眉苦臉地迎上來激才,“玉大人,你說我怎么就攤上這事额嘿∪衬眨” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評論 0 360
  • 文/不壞的土叔 我叫張陵册养,是天一觀的道長东帅。 經(jīng)常有香客問我,道長球拦,這世上最難降的妖魔是什么靠闭? 我笑而不...
    開封第一講書人閱讀 59,836評論 1 298
  • 正文 為了忘掉前任帐我,我火速辦了婚禮,結(jié)果婚禮上愧膀,老公的妹妹穿的比我還像新娘拦键。我一直安慰自己,他們只是感情好檩淋,可當我...
    茶點故事閱讀 68,851評論 6 397
  • 文/花漫 我一把揭開白布芬为。 她就那樣靜靜地躺著,像睡著了一般蟀悦。 火紅的嫁衣襯著肌膚如雪媚朦。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,441評論 1 310
  • 那天熬芜,我揣著相機與錄音莲镣,去河邊找鬼。 笑死涎拉,一個胖子當著我的面吹牛瑞侮,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播鼓拧,決...
    沈念sama閱讀 40,992評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼半火,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了季俩?” 一聲冷哼從身側(cè)響起钮糖,我...
    開封第一講書人閱讀 39,899評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎酌住,沒想到半個月后店归,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,457評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡酪我,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,529評論 3 341
  • 正文 我和宋清朗相戀三年消痛,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片都哭。...
    茶點故事閱讀 40,664評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡秩伞,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出欺矫,到底是詐尸還是另有隱情纱新,我是刑警寧澤,帶...
    沈念sama閱讀 36,346評論 5 350
  • 正文 年R本政府宣布穆趴,位于F島的核電站脸爱,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏未妹。R本人自食惡果不足惜阅羹,卻給世界環(huán)境...
    茶點故事閱讀 42,025評論 3 334
  • 文/蒙蒙 一勺疼、第九天 我趴在偏房一處隱蔽的房頂上張望教寂。 院中可真熱鬧捏鱼,春花似錦、人聲如沸酪耕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽迂烁。三九已至看尼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間盟步,已是汗流浹背藏斩。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留却盘,地道東北人狰域。 一個月前我還...
    沈念sama閱讀 49,081評論 3 377
  • 正文 我出身青樓,卻偏偏與公主長得像黄橘,于是被迫代替她去往敵國和親兆览。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,675評論 2 359

推薦閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 2,701評論 0 3
  • 在C語言中,五種基本數(shù)據(jù)類型存儲空間長度的排列順序是: A)char B)char=int<=float C)ch...
    夏天再來閱讀 3,352評論 0 2
  • 玫瑰花呀玫瑰花 你花燦了浪漫 迷了人間 勾走多少姑娘的魂香 甜蜜了芬芳 更愛戀了情郎
    冰糖檸檬閱讀 234評論 0 1
  • 《三體1,2塞关,3》抬探、 《小王子》、 《月亮與六便士》很喜歡帆赢, 阿加莎克里斯蒂作品 《東方快車謀殺案》這是上學期讀的...
    我是前方閱讀 97評論 0 1