Files
peko-ios/YuMi/Modules/YMRoom/View/Common/BaseRoomBannerView.m
2025-04-02 11:04:07 +08:00

120 lines
3.1 KiB
Objective-C

#import "BaseRoomBannerView.h"
@interface BaseRoomBannerView ()
@property (nonatomic, assign) BOOL isShowing;
@end
@implementation BaseRoomBannerView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setupDefaultValues];
[self setupUI];
[self setupAnimation];
}
return self;
}
- (void)setupDefaultValues {
self.showDuration = 0.3;
self.hideDuration = 0.3;
self.stayDuration = 2.0;
self.isShowing = NO;
}
- (void)setupUI {
// 容器视图
self.containerView = [[UIView alloc] init];
[self addSubview:self.containerView];
// 背景图
self.backgroundImageView = [[UIImageView alloc] init];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.containerView addSubview:self.backgroundImageView];
// 设置初始状态
self.alpha = 0;
self.hidden = YES;
self.transform = CGAffineTransformMakeScale(0.8, 0.8);
}
- (void)setupAnimation {
// 子类可以重写此方法设置特定的动画参数
}
- (void)show {
[self showWithCompletion:nil];
}
- (void)showWithCompletion:(void(^)(void))completion {
if (self.isShowing) {
return;
}
self.isShowing = YES;
self.hidden = NO;
// 显示动画
[UIView animateWithDuration:self.showDuration
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:0.5
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.alpha = 1;
self.transform = CGAffineTransformMakeScale(1.2, 1.2);
} completion:^(BOOL finished) {
// if (finished) {
// // 恢复原始大小
// [UIView animateWithDuration:0.2 animations:^{
// self.transform = CGAffineTransformIdentity;
// } completion:^(BOOL finished) {
// if (finished) {
// // 延迟隐藏
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.stayDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [self hideWithCompletion:completion];
// });
// }
// }];
// }
}];
}
- (void)hide {
[self hideWithCompletion:nil];
}
- (void)hideWithCompletion:(void(^)(void))completion {
if (!self.isShowing) {
return;
}
// 隐藏动画
[UIView animateWithDuration:self.hideDuration
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:0.5
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.alpha = 0;
self.transform = CGAffineTransformMakeScale(0.8, 0.8);
} completion:^(BOOL finished) {
if (finished) {
self.hidden = YES;
self.isShowing = NO;
if (completion) {
completion();
}
}
}];
}
- (void)updateContent:(NSDictionary *)content {
// 子类实现具体内容更新
}
@end