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,12 +257,34 @@ - (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)) { if (!UIEdgeInsetsEqualToEdgeInsets(self.corners, UIEdgeInsetsZero)) {
if (self.view.layer.mask) {
if ([self.view.layer.mask isKindOfClass:[DoricShapeLayer class]]) {
DoricShapeLayer *shapeLayer = (DoricShapeLayer *)self.view.layer.mask;
if (![self compareCornersValue:shapeLayer.corners withCorners:self.corners]
|| !CGRectEqualToRect(self.view.bounds, shapeLayer.viewBounds)) {
shapeLayer.corners = self.corners;
shapeLayer.viewBounds = self.view.bounds;
[self configMaskWithLayer:shapeLayer];
}
} else if (![self rect:originFrame equalTo:self.view.frame]) {
CAShapeLayer *shapeLayer = [CAShapeLayer layer]; CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[self configMaskWithLayer:shapeLayer];
}
} else {
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, CGPathRef path = DoricCreateRoundedRectPath(self.view.bounds,
self.corners.top, self.corners.left, self.corners.bottom, self.corners.right); self.corners.top, self.corners.left, self.corners.bottom, self.corners.right);
shapeLayer.path = path; shapeLayer.path = path;
if ((self.corners.left != self.corners.right if ((self.corners.left != self.corners.right
|| self.corners.left != self.corners.top || self.corners.left != self.corners.top
|| self.corners.left != self.corners.bottom) || self.corners.left != self.corners.bottom)
@ -277,8 +303,16 @@ - (void)setFrame {
} }
CGPathRelease(path); CGPathRelease(path);
self.view.layer.mask = shapeLayer; 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 {