cornerRadius 导致加载时间过长 iOS 10
cornerRadius causes long load time in iOS 10
我的应用程序有问题。我有一个带有 25 个按钮的 View,并且 cornerRadius 发生了变化。由于 iOS 10 加载此视图大约需要 5-6 秒。
这是相关代码:
- (void)setupTiles {
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
}
[self colorTilesWithArray:currentTileColors];
}
当我删除以下行时,会立即加载视图:
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
Buttons 分组在一个 outlet 集合中,以备不时之需。
有人有解决办法吗?
提前致谢!
妮可
Image 1: This takes about 5-6 seconds
Image 2: This is the View that will be loaded
TileButton.h-文件:
@interface TileButton : UIButton
@property (nonatomic) int colorMode;
+ (UIColor *)blue;
+ (UIColor *)red;
+ (UIColor *)green;
+ (UIColor *)yellow;
+(UIColor *)colorForColorCode:(int)colorCode;
-(int)colorMode;
@end
TileButton.m-文件:
@implementation TileButton
- (instancetype)init {
self = [super init];
if (self) {
self.layer.cornerRadius = self.frame.size.width*0.3;
}
return self;
}
+(UIColor *)blue {
return [UIColor colorWithRed:41.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1];
}
+(UIColor *)red {
return [UIColor colorWithRed:255.0/255.0 green:71.0/255.0 blue:109.0/255.0 alpha:1];
}
+(UIColor *)green {
return [UIColor colorWithRed:0.0/255.0 green:185.0/255.0 blue:30.0/255.0 alpha:1];
}
+(UIColor *)yellow {
return [UIColor colorWithRed:255.0/255.0 green:198.0/255.0 blue:26.0/255.0 alpha:1];
}
+(UIColor *)colorForColorCode:(int)colorCode {
switch (colorCode) {
case 1:
return [TileButton blue];
break;
case 2:
return [TileButton red];
break;
case 3:
return [TileButton green];
break;
case 4:
return [TileButton yellow];
break;
default:
return [UIColor blackColor];
break;
}
}
-(int)colorMode {
if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton blue].CGColor))
return 1;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton red].CGColor))
return 2;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton green].CGColor))
return 3;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton yellow].CGColor))
return 4;
else
return -1;
}
@end
为什么不能在for循环外设置圆角半径?
UIButton *btn = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; // set your frame here
bin.layer.cornerRadius = btn.frame.size.width*0.3; // width stays the same, set it here
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
// you need to set the button frame here
btn.frame = CGRectMake(0,0,0,0);
[btn layoutIfNeeded];
}
[self colorTilesWithArray:currentTileColors];
假设 TileButton 是 UIButton 的子类,在初始化时,您可以将 TileButton 设置为具有自定义颜色、宽度和圆角半径。这样一来,您的所有按钮在创建按钮时都已经具有指定的自定义设置。
由于您是在 Interface Builder 中创建按钮,因此您需要在 awakeFromNib
方法中设置 cornerRadius
。它应该看起来像这样:
@implementation TileButton
-(void) awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = self.frame.size.width*0.3;
}
https://developer.apple.com/reference/objectivec/nsobject/1402907-awakefromnib
我的应用程序有问题。我有一个带有 25 个按钮的 View,并且 cornerRadius 发生了变化。由于 iOS 10 加载此视图大约需要 5-6 秒。 这是相关代码:
- (void)setupTiles {
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
}
[self colorTilesWithArray:currentTileColors];
}
当我删除以下行时,会立即加载视图:
[btn layoutIfNeeded];
bin.layer.cornerRadius = btn.frame.size.width*0.3;
Buttons 分组在一个 outlet 集合中,以备不时之需。
有人有解决办法吗? 提前致谢!
妮可
Image 1: This takes about 5-6 seconds
Image 2: This is the View that will be loaded
TileButton.h-文件:
@interface TileButton : UIButton
@property (nonatomic) int colorMode;
+ (UIColor *)blue;
+ (UIColor *)red;
+ (UIColor *)green;
+ (UIColor *)yellow;
+(UIColor *)colorForColorCode:(int)colorCode;
-(int)colorMode;
@end
TileButton.m-文件:
@implementation TileButton
- (instancetype)init {
self = [super init];
if (self) {
self.layer.cornerRadius = self.frame.size.width*0.3;
}
return self;
}
+(UIColor *)blue {
return [UIColor colorWithRed:41.0/255.0 green:161.0/255.0 blue:255.0/255.0 alpha:1];
}
+(UIColor *)red {
return [UIColor colorWithRed:255.0/255.0 green:71.0/255.0 blue:109.0/255.0 alpha:1];
}
+(UIColor *)green {
return [UIColor colorWithRed:0.0/255.0 green:185.0/255.0 blue:30.0/255.0 alpha:1];
}
+(UIColor *)yellow {
return [UIColor colorWithRed:255.0/255.0 green:198.0/255.0 blue:26.0/255.0 alpha:1];
}
+(UIColor *)colorForColorCode:(int)colorCode {
switch (colorCode) {
case 1:
return [TileButton blue];
break;
case 2:
return [TileButton red];
break;
case 3:
return [TileButton green];
break;
case 4:
return [TileButton yellow];
break;
default:
return [UIColor blackColor];
break;
}
}
-(int)colorMode {
if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton blue].CGColor))
return 1;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton red].CGColor))
return 2;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton green].CGColor))
return 3;
else if (CGColorEqualToColor(self.backgroundColor.CGColor, [TileButton yellow].CGColor))
return 4;
else
return -1;
}
@end
为什么不能在for循环外设置圆角半径?
UIButton *btn = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]; // set your frame here
bin.layer.cornerRadius = btn.frame.size.width*0.3; // width stays the same, set it here
for (TileButton *btn in tiles_btn) {
btn.alpha = 0.0;
btn.layer.borderColor = [UIColor blackColor].CGColor;
btn.layer.borderWidth = 1.0f;
// you need to set the button frame here
btn.frame = CGRectMake(0,0,0,0);
[btn layoutIfNeeded];
}
[self colorTilesWithArray:currentTileColors];
假设 TileButton 是 UIButton 的子类,在初始化时,您可以将 TileButton 设置为具有自定义颜色、宽度和圆角半径。这样一来,您的所有按钮在创建按钮时都已经具有指定的自定义设置。
由于您是在 Interface Builder 中创建按钮,因此您需要在 awakeFromNib
方法中设置 cornerRadius
。它应该看起来像这样:
@implementation TileButton
-(void) awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = self.frame.size.width*0.3;
}
https://developer.apple.com/reference/objectivec/nsobject/1402907-awakefromnib