天天看点

iOS 设置视图阴影

第一种:

//设置四周阴影
- (void)setShadowLayer{
    self.layer.masksToBounds = false;
    self.layer.shadowOffset = CGSizeMake(0, -2);//设置阴影偏移
    self.layer.shadowOpacity = 阴影透明度;
    self.layer.shadowRadius = 阴影弧度;
    self.layer.shadowColor = 阴影颜色.CGColor;
}           

第二种:

/**
 设置顶边阴影

 @param shadowColor 阴影颜色
 @param radious 阴影圆角
 @param offset 阴影偏移量
 */
- (void)setShadowLayer:(UIColor *)shadowColor shadowRadius:(CGFloat)radious shadowOffset:(CGFloat)offset{
    CGSize size = self.bounds.size;
    self.layer.frame = self.bounds;
    self.layer.shadowOpacity = 1;
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowOffset = CGSizeMake(0, offset);
    
    CGRect rect = CGRectMake(-offset, offset - radious/2, size.width+2*offset, radious);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radious];
    self.layer.shadowPath = path.CGPath;
}           

继续阅读