UIView同时设置圆角和投影

2021-07-15  本文已影响0人  大浪捉鱼

这是代码


@interface ShadowCornerView ()
@property (nonatomic, strong) UIView *contentView;// 加投影效果
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UIImageView *arrow;
@end

@implementation ShadowCornerView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView = [UIView new];
        [self addSubview:self.contentView];
        [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        self.icon = [UIImageView new];
        self.icon.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:self.icon];
        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(35);
            make.size.mas_equalTo(CGSizeMake(50, 50));
            make.centerY.mas_equalTo(0);
        }];
        
        self.titleLabel = [UILabel new];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.icon.mas_right).offset(25);
            make.top.mas_equalTo(25);
            make.right.mas_equalTo(-30);
            make.height.mas_equalTo(26);
        }];
        
        self.subTitleLabel = [UILabel new];
        self.subTitleLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.subTitleLabel];
        [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.icon.mas_right).offset(25);
            make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(3);
            make.right.mas_equalTo(-30);
            make.height.mas_equalTo(19);
        }];
        
        self.arrow = [UIImageView new];
        self.arrow.contentMode = UIViewContentModeScaleAspectFill;
        self.arrow.image = [UIImage imageNamed:@"icon_common_arrow_24"];
        [self.contentView addSubview:self.arrow];
        [self.arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-6);
            make.size.mas_equalTo(CGSizeMake(24, 24));
            make.centerY.mas_equalTo(0);
        }];
        
       // 解决不能同时加圆角和投影的问题,里面一层控件加圆角,外面一层控件加投影
        self.contentView.layer.cornerRadius = 15;
        self.contentView.layer.masksToBounds = YES;
        self.layer.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.04].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 2);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 10.0;
        self.layer.cornerRadius = 15.0;
        self.clipsToBounds = NO;
   
    }
    return self;
}

上一篇下一篇

猜你喜欢

热点阅读