iOS卡片樣式控件的實(shí)現(xiàn)

前言

項(xiàng)目中碰到一種卡片樣式的視圖的需求,在這里順便也記錄一下,可能性能方面不是很好。如果用在大量的列表里面,可能還需要優(yōu)化。

card.PNG

設(shè)計(jì)思路

主要的設(shè)計(jì)思路是:

  1. contentView上放置imageView和titleLabel;
  2. dimmingView負(fù)責(zé)渲染陰影;
  3. contentView和dimmingView放在相同的父視圖上,并且具有相同的frame。

代碼實(shí)現(xiàn)

主要的實(shí)現(xiàn)代碼如下:

@interface ViewController ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *dimmingView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation ViewController

#pragma mark - life cycle
// 列表展示
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = UIColorFromHex(0xeeeeee);
    [self setupConstraintsForSubviews];
    
    self.imageView.image = [UIImage imageNamed:@"son"];
    self.titleLabel.text = @"可愛的小朋友";
}

#pragma mark - layout subviews
- (void)setupConstraintsForSubviews {
    [self.view addSubview:self.dimmingView];
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.titleLabel];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_offset(0);
    }];
    
    [self.dimmingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_offset(0);
        make.size.mas_equalTo(200);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(self.imageView.mas_bottom).mas_offset(20);
        make.bottom.mas_offset(-20);
    }];
}

#pragma mark - lazy load
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc]init];
    }
    return _imageView;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc]init];
        _contentView.layer.cornerRadius = 10;
        _contentView.clipsToBounds = YES;
    }
    return _contentView;
}

- (UIView *)dimmingView {
    if (!_dimmingView) {
        _dimmingView = [[UIView alloc]init];
        // 設(shè)置陰影
        _dimmingView.layer.cornerRadius = 10;
        _dimmingView.layer.backgroundColor = [UIColor whiteColor].CGColor;
        _dimmingView.layer.shadowOpacity = 1;
        _dimmingView.layer.shadowColor = [UIColor brownColor].CGColor;
        _dimmingView.layer.shadowRadius = 10;
        _dimmingView.layer.shadowOffset = CGSizeMake(10, 10);
        _dimmingView.clipsToBounds = NO;
    }
    return _dimmingView;
}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容