Fix the loss of Corner effect caused by tableViewCell reuse.

This commit is contained in:
wushangkun 2021-11-16 20:47:18 +08:00 committed by osborn
parent c53fe3c208
commit 7bc22c6c1f
2 changed files with 64 additions and 23 deletions

View File

@ -20,6 +20,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h> #import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
typedef UIEdgeInsets DoricMargin; typedef UIEdgeInsets DoricMargin;
typedef UIEdgeInsets DoricPadding; typedef UIEdgeInsets DoricPadding;
@ -121,3 +122,9 @@ typedef NS_ENUM(NSInteger, DoricGravity) {
@interface UIView (DoricLayout) @interface UIView (DoricLayout)
@property(nonatomic, strong) DoricLayout *doricLayout; @property(nonatomic, strong) DoricLayout *doricLayout;
@end @end
@interface DoricShapeLayer : CAShapeLayer
@property CGRect viewBounds;
@property UIEdgeInsets corners;
@end

View File

@ -61,6 +61,10 @@ CGPathRef DoricCreateRoundedRectPath(CGRect bounds,
static const void *kLayoutConfig = &kLayoutConfig; static const void *kLayoutConfig = &kLayoutConfig;
@implementation DoricShapeLayer
@end
@implementation UIView (DoricLayout) @implementation UIView (DoricLayout)
@dynamic doricLayout; @dynamic doricLayout;
@ -253,34 +257,64 @@ - (void)setFrame {
} }
if (![self rect:originFrame equalTo:self.view.frame]) { if (![self rect:originFrame equalTo:self.view.frame]) {
self.view.frame = originFrame; self.view.frame = originFrame;
if (!UIEdgeInsetsEqualToEdgeInsets(self.corners, UIEdgeInsetsZero)) { }
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; if (!UIEdgeInsetsEqualToEdgeInsets(self.corners, UIEdgeInsetsZero)) {
CGPathRef path = DoricCreateRoundedRectPath(self.view.bounds, if (self.view.layer.mask) {
self.corners.top, self.corners.left, self.corners.bottom, self.corners.right); if ([self.view.layer.mask isKindOfClass:[DoricShapeLayer class]]) {
shapeLayer.path = path; DoricShapeLayer *shapeLayer = (DoricShapeLayer *)self.view.layer.mask;
if (![self compareCornersValue:shapeLayer.corners withCorners:self.corners]
if ((self.corners.left != self.corners.right || !CGRectEqualToRect(self.view.bounds, shapeLayer.viewBounds)) {
|| self.corners.left != self.corners.top shapeLayer.corners = self.corners;
|| self.corners.left != self.corners.bottom) shapeLayer.viewBounds = self.view.bounds;
&& self.view.layer.borderWidth > CGFLOAT_MIN) { [self configMaskWithLayer:shapeLayer];
CAShapeLayer *lineLayer = [CAShapeLayer layer]; }
lineLayer.lineWidth = self.view.layer.borderWidth * 2; } else if (![self rect:originFrame equalTo:self.view.frame]) {
lineLayer.strokeColor = self.view.layer.borderColor; CAShapeLayer *shapeLayer = [CAShapeLayer layer];
lineLayer.path = path; [self configMaskWithLayer:shapeLayer];
lineLayer.fillColor = nil;
[[self.view.layer sublayers] forEach:^(__kindof CALayer *obj) {
if ([obj isKindOfClass:CAShapeLayer.class] && ((CAShapeLayer *) obj).lineWidth > CGFLOAT_MIN) {
[obj removeFromSuperlayer];
}
}];
[self.view.layer addSublayer:lineLayer];
} }
CGPathRelease(path); } else {
self.view.layer.mask = shapeLayer; DoricShapeLayer *shapeLayer = [DoricShapeLayer layer];
shapeLayer.corners = self.corners;
shapeLayer.viewBounds = self.view.bounds;
[self configMaskWithLayer:shapeLayer];
} }
} }
} }
- (void)configMaskWithLayer:(CAShapeLayer *)shapeLayer {
CGPathRef path = DoricCreateRoundedRectPath(self.view.bounds,
self.corners.top, self.corners.left, self.corners.bottom, self.corners.right);
shapeLayer.path = path;
if ((self.corners.left != self.corners.right
|| self.corners.left != self.corners.top
|| self.corners.left != self.corners.bottom)
&& self.view.layer.borderWidth > CGFLOAT_MIN) {
CAShapeLayer *lineLayer = [CAShapeLayer layer];
lineLayer.lineWidth = self.view.layer.borderWidth * 2;
lineLayer.strokeColor = self.view.layer.borderColor;
lineLayer.path = path;
lineLayer.fillColor = nil;
[[self.view.layer sublayers] forEach:^(__kindof CALayer *obj) {
if ([obj isKindOfClass:CAShapeLayer.class] && ((CAShapeLayer *) obj).lineWidth > CGFLOAT_MIN) {
[obj removeFromSuperlayer];
}
}];
[self.view.layer addSublayer:lineLayer];
}
CGPathRelease(path);
self.view.layer.mask = shapeLayer;
}
- (BOOL)compareCornersValue:(UIEdgeInsets)corners1 withCorners:(UIEdgeInsets)corners2 {
if (corners1.top == corners2.top
&& corners1.left == corners2.left
&& corners1.right == corners2.right
&& corners1.bottom == corners2.bottom) {
return YES;
}
return NO;
}
- (void)measureUndefinedContent:(CGSize)targetSize { - (void)measureUndefinedContent:(CGSize)targetSize {
CGSize measuredSize = [self.view sizeThatFits:targetSize]; CGSize measuredSize = [self.view sizeThatFits:targetSize];
if (self.widthSpec == DoricLayoutFit) { if (self.widthSpec == DoricLayoutFit) {