现在我正在iOS中开发横幅显示功能 这是一个单例,当用户登录时在屏幕上方显示横幅。
基本上是具有类方法showWithName ...的共享视图。
find . "(" -name "*.ext" ")" -exec readlink -f {} \;|grep "/@"|xargs wc -l
当用户调用时,它将创建一个UIImageView和一个UILabel进行自我添加。并将其动画显示在屏幕上。
@interface XXUserWelcomeBanner ()
{
UIImageView *logoView;
UILabel *textLabel;
CGFloat _width;
}
@end
所以我刚刚发现了另一个错误,导致该方法被调用两次,并且第二次仅显示UIImageView 。
我不明白为什么会这样。 因为不会两次创建UIImageView和UILabel。
更多代码。
+ (XXUserWelcomeBanner *)shared {
static dispatch_once_t onceToken;
static XXUserWelcomeBanner *userWelcomBanner;
dispatch_once(&onceToken, ^{
userWelcomBanner = [[XXUserWelcomeBanner alloc] init];
});
return userWelcomBanner;
}
+ (void)showWithUserName:(NSString *)userID andLogo:(UIImage * _Nullable)logo {
dispatch_async(dispatch_get_main_queue(), ^{
[[self shared] createWithName:userID andLogo:logo];
});
}
谢谢。
答案 0 :(得分:0)
预计createWithName:andLogo:
在您的应用中多次调用。我猜您用不同的参数来调用它,这就是为什么它不应该放在dispatch_once(&onceToken, ^{ });
中的原因。但是您可以优化XXUserWelcomeBanner
的{{1}}:
init
在- (instancetype)init {
self = [super init];
if (self) {
logoView = [[UIImageView alloc] init];
[self addSubview:logoView];
textLabel = [[UILabel alloc] init];
[self addSubview:textLabel];
}
return self;
}
中,您将具有以下内容:
createWithName:andLogo: