当前位置: 代码迷 >> 综合 >> 设置图片圆角加阴影效果
  详细解决方案

设置图片圆角加阴影效果

热度:6   发布时间:2023-09-21 02:37:39.0

需要在我的界面将头像设置成悬浮阴影效果,但头像是圆角的,设置完成后发现要么有阴影无圆角,要么有圆角无阴影,查阅资料发现可以在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,效果还可以。

  相关解决方案