iOS: fix the loss of Corner effect caused by tableViewCell reuse.

This commit is contained in:
吴尚昆 2021-11-17 10:23:21 +08:00 committed by osborn
parent 7bc22c6c1f
commit 8c52fe5265
2 changed files with 11 additions and 20 deletions

View File

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

View File

@ -21,6 +21,7 @@
#import <objc/runtime.h>
#import "UIView+Doric.h"
#import "DoricExtensions.h"
#import <QuartzCore/QuartzCore.h>
void DoricAddEllipticArcPath(CGMutablePathRef path,
CGPoint origin,
@ -61,8 +62,12 @@ CGPathRef DoricCreateRoundedRectPath(CGRect bounds,
static const void *kLayoutConfig = &kLayoutConfig;
@implementation DoricShapeLayer
@interface DoricShapeLayer : CAShapeLayer
@property CGRect viewBounds;
@property UIEdgeInsets corners;
@end
@implementation DoricShapeLayer
@end
@implementation UIView (DoricLayout)
@ -255,20 +260,21 @@ - (void)setFrame {
originFrame = CGRectApplyAffineTransform(originFrame, self.view.transform);
originFrame = CGRectOffset(originFrame, anchor.x * self.measuredWidth + self.measuredX, anchor.y * self.measuredHeight + self.measuredY);
}
if (![self rect:originFrame equalTo:self.view.frame]) {
BOOL isFrameChange = ![self rect:originFrame equalTo:self.view.frame];
if (isFrameChange) {
self.view.frame = originFrame;
}
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]
if (!UIEdgeInsetsEqualToEdgeInsets(self.corners, shapeLayer.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]) {
} else if (isFrameChange) {
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[self configMaskWithLayer:shapeLayer];
}
@ -305,16 +311,6 @@ - (void)configMaskWithLayer:(CAShapeLayer *)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 {
CGSize measuredSize = [self.view sizeThatFits:targetSize];
if (self.widthSpec == DoricLayoutFit) {
@ -634,3 +630,4 @@ - (void)layoutHLayout {
}
@end