使用双层 View 实现;
self.signInView.backgroundColor = [UIColor clearColor]; // 底层 view 设置阴影,要设置透明
[self.headerBgImgView addSubview:self.signInView];
[self.signInView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self);
make.top.mas_equalTo(self.userHeadBtn.mas_top);
make.width.mas_equalTo(AUTO_RATIO(74.f));
make.height.mas_equalTo(AUTO_RATIO(28.f));
}];
[self layoutIfNeeded];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.signInView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(AUTO_RATIO(14), AUTO_RATIO(14))];
self.signInView.layer.shadowColor = [UIColor blackColor].CGColor;
self.signInView.layer.shadowOffset = CGSizeMake(0, 2); // 阴影偏移量,默认(0,0)
self.signInView.layer.shadowOpacity = 1;
self.signInView.layer.shadowRadius = 8.0;
self.signInView.layer.shadowPath = maskPath.CGPath;
self.signInView.layer.masksToBounds = NO;
[self.signInBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.signInView);
}];
[self layoutIfNeeded];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
//设置大小
maskLayer.frame = self.signInBtn.bounds;
//设置图形样子
maskLayer.path = maskPath.CGPath;
self.signInBtn.layer.mask = maskLayer;