Skip to content
This repository was archived by the owner on Jun 3, 2021. It is now read-only.

Commit c778ff3

Browse files
wqyfavorjianhan-he
authored andcommitted
[iOS Use property to access 'backgroundColor'. (#2948)
1 parent c112f2b commit c778ff3

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

ios/sdk/WeexSDK/Sources/Component/WXComponent_internal.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,16 @@ typedef id (^WXDataBindingBlock)(NSDictionary *data, BOOL *needUpdate);
170170
NSMutableDictionary<NSString *, NSArray *> *_eventParameters;
171171
}
172172

173-
/* _transform may be modified in mutiple threads. DO NOT use "_transform = XXX" directly.
173+
/* DO NOT use "_transform = XXX" directly.
174174
Ivar access in ObjC is compiled to code with additional release or retain. So use Ivar in mutiple
175175
thread may lead to crash. Use an ATOMIC property is well enough. */
176176
@property (atomic, strong) WXTransform *transform;
177177

178+
/**
179+
DO NOT use "_backgroundColor" directly. The same reason as '_transform'.
180+
*/
181+
@property (atomic, strong) UIColor* backgroundColor;
182+
178183
///--------------------------------------
179184
/// @name Package Internal Methods
180185
///--------------------------------------

ios/sdk/WeexSDK/Sources/Display/WXComponent+Display.m

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ - (WXDisplayBlock)_compositeDisplayBlock
289289
- (void)_collectCompositingDisplayBlocks:(NSMutableArray *)displayBlocks context:(CGContextRef)context isCancelled:(BOOL(^)(void))isCancelled
290290
{
291291
// TODO: compositingChild has no chance to applyPropertiesToView, need update here?
292-
UIColor *backgroundColor = _backgroundColor;
292+
UIColor *backgroundColor = self.backgroundColor;
293293
BOOL clipsToBounds = _clipToBounds;
294294
CGRect frame = self.calculatedFrame;
295295
CGRect bounds = CGRectMake(0, 0, frame.size.width, frame.size.height);
@@ -349,15 +349,13 @@ - (void)_drawBorderWithContext:(CGContextRef)context size:(CGSize)size
349349

350350
CGContextSetAlpha(context, _opacity);
351351
// fill background color
352-
@synchronized (self) {
353-
if (_backgroundColor && CGColorGetAlpha(_backgroundColor.CGColor) > 0) {
354-
CGContextSetFillColorWithColor(context, _backgroundColor.CGColor);
355-
UIBezierPath *bezierPath = [UIBezierPath wx_bezierPathWithRoundedRect:rect topLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight];
356-
[bezierPath fill];
357-
WXPerformBlockOnMainThread(^{
358-
_view.backgroundColor = UIColor.clearColor;
359-
});
360-
}
352+
if (self.backgroundColor && CGColorGetAlpha(self.backgroundColor.CGColor) > 0) {
353+
CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
354+
UIBezierPath *bezierPath = [UIBezierPath wx_bezierPathWithRoundedRect:rect topLeft:topLeft topRight:topRight bottomLeft:bottomLeft bottomRight:bottomRight];
355+
[bezierPath fill];
356+
WXPerformBlockOnMainThread(^{
357+
_view.backgroundColor = UIColor.clearColor;
358+
});
361359
}
362360
// Top
363361
if (_borderTopWidth > 0) {
@@ -596,7 +594,7 @@ - (void)_handleBorders:(NSDictionary *)styles isUpdating:(BOOL)updating
596594
_layer.borderWidth = _borderTopWidth;
597595
_layer.borderColor = _borderTopColor.CGColor;
598596
if ((_transition.transitionOptions & WXTransitionOptionsBackgroundColor) != WXTransitionOptionsBackgroundColor ) {
599-
_layer.backgroundColor = _backgroundColor.CGColor;
597+
_layer.backgroundColor = self.backgroundColor.CGColor;
600598
}
601599
}
602600
}
@@ -608,7 +606,7 @@ - (BOOL)_bitmapOpaqueWithSize:(CGSize)size
608606
WXRoundedRect *borderRect = [[WXRoundedRect alloc] initWithRect:rect topLeft:_borderTopLeftRadius topRight:_borderTopRightRadius bottomLeft:_borderBottomLeftRadius bottomRight:_borderBottomRightRadius];
609607
WXRadii *radii = borderRect.radii;
610608
BOOL hasBorderRadius = [radii hasBorderRadius];
611-
return (!hasBorderRadius) && _opacity == 1.0 && CGColorGetAlpha(_backgroundColor.CGColor) == 1.0 && [self _needsDrawBorder];
609+
return (!hasBorderRadius) && _opacity == 1.0 && CGColorGetAlpha(self.backgroundColor.CGColor) == 1.0 && [self _needsDrawBorder];
612610
}
613611

614612
- (CAShapeLayer *)drawBorderRadiusMaskLayer:(CGRect)rect

ios/sdk/WeexSDK/Sources/Model/WXComponent.mm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ @implementation WXComponent
7070
}
7171

7272
@synthesize transform = _transform;
73+
@synthesize backgroundColor = _backgroundColor;
7374

7475
#pragma mark Life Cycle
7576

@@ -391,7 +392,7 @@ - (UIView *)view
391392
_layer.borderWidth = _borderTopWidth;
392393
[self _resetNativeBorderRadius];
393394
_layer.opacity = _opacity;
394-
_view.backgroundColor = _backgroundColor;
395+
_view.backgroundColor = self.backgroundColor;
395396
}
396397

397398
if (_backgroundImage) {
@@ -857,10 +858,8 @@ - (void)setGradientLayer
857858
UIColor * endColor = (UIColor*)linearGradient[@"endColor"];
858859
CAGradientLayer * gradientLayer = [WXUtility gradientLayerFromColors:@[startColor, endColor] locations:nil frame:strongSelf.view.bounds gradientType:(WXGradientType)[linearGradient[@"gradientType"] integerValue]];
859860
if (gradientLayer) {
860-
@synchronized (strongSelf) {
861-
_backgroundColor = [UIColor colorWithPatternImage:[strongSelf imageFromLayer:gradientLayer]];
862-
}
863-
strongSelf.view.backgroundColor = _backgroundColor;
861+
strongSelf.backgroundColor = [UIColor colorWithPatternImage:[strongSelf imageFromLayer:gradientLayer]];
862+
strongSelf.view.backgroundColor = strongSelf.backgroundColor;
864863
[strongSelf setNeedsDisplay];
865864
}
866865
}

ios/sdk/WeexSDK/Sources/View/WXComponent+ViewManagement.mm

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ - (void)viewDidUnload
174174

175175
- (void)_initViewPropertyWithStyles:(NSDictionary *)styles
176176
{
177-
_backgroundColor = styles[@"backgroundColor"] ? [WXConvert UIColor:styles[@"backgroundColor"]] : [UIColor clearColor];
177+
self.backgroundColor = styles[@"backgroundColor"] ? [WXConvert UIColor:styles[@"backgroundColor"]] : [UIColor clearColor];
178178
_backgroundImage = styles[@"backgroundImage"] ? [WXConvert NSString:styles[@"backgroundImage"]]: nil;
179179
_opacity = styles[@"opacity"] ? [WXConvert CGFloat:styles[@"opacity"]] : 1.0;
180180
_clipToBounds = styles[@"overflow"] ? [WXConvert WXClipType:styles[@"overflow"]] : NO;
@@ -193,9 +193,7 @@ - (void)_transitionUpdateViewProperty:(NSDictionary *)styles
193193
{
194194
WX_CHECK_COMPONENT_TYPE(self.componentType)
195195
if (styles[@"backgroundColor"]) {
196-
@synchronized (self) {
197-
_backgroundColor = [WXConvert UIColor:styles[@"backgroundColor"]];
198-
}
196+
self.backgroundColor = [WXConvert UIColor:styles[@"backgroundColor"]];
199197
}
200198
if (styles[@"opacity"]) {
201199
_opacity = [WXConvert CGFloat:styles[@"opacity"]];
@@ -213,9 +211,7 @@ - (void)_updateViewStyles:(NSDictionary *)styles
213211
}
214212

215213
if (styles[@"backgroundColor"]) {
216-
@synchronized (self) {
217-
_backgroundColor = [WXConvert UIColor:styles[@"backgroundColor"]];
218-
}
214+
self.backgroundColor = [WXConvert UIColor:styles[@"backgroundColor"]];
219215
[self setNeedsDisplay];
220216
}
221217

@@ -311,9 +307,7 @@ - (void)resetBorder:(NSArray *)styles
311307
- (void)_resetStyles:(NSArray *)styles
312308
{
313309
if (styles && [styles containsObject:@"backgroundColor"]) {
314-
@synchronized (self) {
315-
_backgroundColor = [UIColor clearColor];
316-
}
310+
self.backgroundColor = [UIColor clearColor];
317311
[self setNeedsDisplay];
318312
}
319313
if (styles && [styles containsObject:@"boxShadow"]) {

0 commit comments

Comments
 (0)