UIView的阴影和圆角,一般情况因为不能共存,每次写这个一大坨,封装了下,原理就是view后面再加一个专门显示阴影和圆角的View。
嗯,滚动视图用的时候小心点。
看起来代码简单多了:
UIButton * button = [UIButton buttonWithType:0];
[button setTitle:@"尿你一脸" forState:0];
button.frame = CGRectMake(150, 500, 160, 60);
button.backgroundColor = [UIColor orangeColor];
[button addTarget:self action:@selector(btnClick) forControlEvents:1<<6];
[self.view addSubview:button];
//或者
[button addCornersAndShadow];
//或者
[button addCorners:UIRectCornerTopRight|UIRectCornerBottomRight rRadii:30.0f shadowLayer:^(CALayer * _Nonnull shadowLayer) {
shadowLayer.shadowOpacity = 0.5;
shadowLayer.shadowOffset = CGSizeMake(5, 5);
shadowLayer.shadowRadius = 10;
}];
效果:
效果图
封装成了个UIView的Category
//
// UIView+CornerShadow.h
// testDmoe2
//
// Created by 马致远尿你 on 2019/12/20.
// Copyright © 2019 马致远尿你. All rights reserved.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
//滚动视图里面用小心,用法看.m文件代码
@interface UIView (CornerShadow)
- (void)addCornersAndShadow;
- (void)addCorners:(UIRectCorner)corners
shadowLayer:(void (^)(CALayer * shadowLayer))shadowLayer;
- (void)addCorners:(UIRectCorner)corners
rRadii:(CGFloat)rRadii;
/*
@param corners 圆角设置
@param rRadii 圆角设置
@return shadowLayer 阴影设置
*/
- (void)addCorners:(UIRectCorner)corners
rRadii:(CGFloat)rRadii
shadowLayer:(nullable void (^)(CALayer * shadowLayer))shadowLayer;
@end
NS_ASSUME_NONNULL_END
//
// UIView+CornerShadow.m
// testDmoe2
//
// Created by 马致远尿你 on 2019/12/20.
// Copyright © 2019 马致远尿你. All rights reserved.
//
#import "UIView+CornerShadow.h"
static CGFloat rRadii = 8.0f;//默认圆角大小
@implementation UIView (CornerShadow)
- (void)addCornersAndShadow {
[self addCorners:UIRectCornerAllCorners rRadii:rRadii shadowLayer:NULL];
}
- (void)addCorners:(UIRectCorner)corners
shadowLayer:(void (^)(CALayer * shadowLayer))shadowLayer{
[self addCorners:corners rRadii:rRadii shadowLayer:shadowLayer];
}
- (void)addCorners:(UIRectCorner)corners
rRadii:(CGFloat)rRadii{
[self addCorners:corners rRadii:rRadii shadowLayer:^(CALayer * shadowLayer) {
shadowLayer.shadowOpacity = 0.25;
shadowLayer.shadowOffset = CGSizeZero;
shadowLayer.shadowRadius = 10;
}];
}
- (void)addCorners:(UIRectCorner)corners
rRadii:(CGFloat)rRadii
shadowLayer:(nullable void (^)(CALayer * shadowLayer))shadowLayer{
UIView * aview = self;
CGSize cornerRadii = CGSizeMake(rRadii, rRadii);
//前面的裁剪
CAShapeLayer *mask = [CAShapeLayer layer];
mask.path = [UIBezierPath bezierPathWithRoundedRect:aview.bounds
byRoundingCorners:corners cornerRadii:cornerRadii].CGPath;
aview.layer.mask = mask;
//后面的那个
if(!aview.superview) return;
UIView * draftView = [[UIView alloc] initWithFrame:aview.frame];
draftView.backgroundColor = aview.backgroundColor;
[aview.superview insertSubview:draftView belowSubview:aview];
if(shadowLayer){
shadowLayer(draftView.layer);
}else{
draftView.layer.shadowOpacity = 0.25;
draftView.layer.shadowOffset = CGSizeZero;
draftView.layer.shadowRadius = 10;
}
draftView.backgroundColor = nil;
draftView.layer.masksToBounds = NO;
CALayer *cornerLayer = [CALayer layer];
cornerLayer.frame = draftView.bounds;
cornerLayer.backgroundColor = aview.backgroundColor.CGColor;
CAShapeLayer *lay = [CAShapeLayer layer];
lay.path = [UIBezierPath bezierPathWithRoundedRect:aview.bounds
byRoundingCorners:corners cornerRadii:cornerRadii].CGPath;
cornerLayer.mask = lay;
[draftView.layer addSublayer:cornerLayer];
}
@end
记录下,希望能帮到有需要的童鞋~
网友评论