您的位置:首页 > 产品设计 > UI/UE

设置图片圆角加阴影效果

2017-05-26 16:43 267 查看
需要在我的界面将头像设置成悬浮阴影效果,但头像是圆角的,设置完成后发现要么有阴影无圆角,要么有圆角无阴影,查阅资料发现可以在layer上动手脚,代码如下:

CALayer *subLayer = [CALayer
layer];

    CGRect imgFrame =
CGRectMake(SCREENWIDTH/2-40,
20, 80,
80);

    subLayer.frame = imgFrame;

    subLayer.cornerRadius =
40;

    subLayer.backgroundColor = [UIColor
blackColor].CGColor;

    subLayer.masksToBounds =
YES;

    subLayer.shadowOffset =
CGSizeMake(15,
15);

    subLayer.shadowOpacity =
0.8f;

    subLayer.shadowRadius =
8;

    subLayer.masksToBounds =
NO;

    

    [self.contentView.layer
addSublayer:subLayer];

    

    self.avatarImageView = [[UIImageView
alloc] initWithFrame:CGRectZero];

    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(imageTapAction)];

    self.avatarImageView.backgroundColor = [UIColor
redColor];

    self.avatarImageView.layer.cornerRadius
= 40;

    self.avatarImageView.layer.masksToBounds
= YES;

    [self.avatarImageView
addGestureRecognizer:tapGes];

    self.avatarImageView.userInteractionEnabled
= YES;

    self.avatarImageView.layer.shadowColor
= [UIColor
blackColor].CGColor;

    self.avatarImageView.layer.shadowOffset
= CGSizeMake(10,
10);

    self.avatarImageView.layer.shadowOpacity
= 0.8f;

    [self.contentView
addSubview:self.avatarImageView];

上述代码中的 self 是自定义的一个 UITableViewCell,效果还可以。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  UIView