1. 程式人生 > >設定圖片圓角加陰影效果

設定圖片圓角加陰影效果

需要在我的介面將頭像設定成懸浮陰影效果,但頭像是圓角的,設定完成後發現要麼有陰影無圓角,要麼有圓角無陰影,查閱資料發現可以在layer上動手腳,程式碼如下:

CALayer *subLayer = [CALayerlayer];

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

    subLayer.frame = imgFrame;

    subLayer.cornerRadius = 40;

    subLayer.backgroundColor = [UIColorblackColor].CGColor;

    subLayer.

masksToBounds = YES;

    subLayer.shadowOffset = CGSizeMake(15, 15);

    subLayer.shadowOpacity = 0.8f;

    subLayer.shadowRadius = 8;

    subLayer.masksToBounds = NO;

    [self.contentView.layeraddSublayer:subLayer];

self.avatarImageView = [[UIImageViewalloc] initWithFrame:CGRectZero];

UITapGestureRecognizer

*tapGes = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(imageTapAction)];

self.avatarImageView.backgroundColor = [UIColorredColor];

self.avatarImageView.layer.cornerRadius = 40;

self.avatarImageView.layer.masksToBounds = YES;

    [self.avatarImageViewaddGestureRecognizer:tapGes];

self.avatarImageView.userInteractionEnabled = YES;

self.avatarImageView.layer.shadowColor = [UIColorblackColor].CGColor;

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

self.avatarImageView.layer.shadowOpacity = 0.8f;

    [self.contentViewaddSubview:self.avatarImageView];


上述程式碼中的 self 是自定義的一個 UITableViewCell,效果還可以。