Files
yinmeng-ios/xplan-ios/Main/Room/View/BaseUIContainerView/XPRoomBackContainerView.m

505 lines
17 KiB
Mathematica
Raw Normal View History

2021-10-14 21:10:04 +08:00
//
// XPRoomBaseUIView.m
// xplan-ios
//
// Created by on 2021/10/11.
// View
#import "XPRoomBackContainerView.h"
2021-10-14 21:10:04 +08:00
///Third
#import <Masonry/Masonry.h>
#import <SVGA.h>
#import <NIMSDK/NIMSDK.h>
2021-11-18 18:43:20 +08:00
#import <POP.h>
///Tool
#import "XPGiftStorage.h"
#import "XPMacro.h"
#import "ThemeColor+Room.h"
///Model
2021-11-18 18:43:20 +08:00
#import "MicroQueueModel.h"
#import "MicroStateModel.h"
#import "UserInfoModel.h"
#import "GiftReceiveInfoModel.h"
#import "GiftInfoModel.h"
#import "RoomInfoModel.h"
#import "AttachmentModel.h"
#import "XPGiftBigPrizeModel.h"
2021-11-18 18:43:20 +08:00
#import "XPGiftBannerUserInfoModel.h"
///View
#import "XPRoomGiftBannerView.h"
@interface XPRoomBackContainerView ()<SVGAPlayerDelegate, NIMChatManagerDelegate>
2021-11-18 18:43:20 +08:00
///
///
@property (nonatomic,strong) UIView * lowLevelView;
///
@property (nonatomic,strong) UIView * middleLevelView;
///
@property (nonatomic,strong) UIView * highLevleView;
///
@property (strong, nonatomic) SVGAParser *parser;
///
@property (nonatomic,strong) SVGAImageView *giftEffectView;
2021-10-14 21:10:04 +08:00
///
@property (nonatomic,strong) UIImageView *backImageView;
///
@property (nonatomic,weak) id<RoomHostDelegate>delegate;
///
@property (nonatomic,strong) NSMutableArray<GiftInfoModel *> *giftEffectQueue;
///
@property (nonatomic,assign) BOOL isGiftEffectPlaying;
///520
@property (nonatomic,strong) NSMutableArray<GiftReceiveInfoModel *> *giftBannerQueue;;
2021-11-18 18:43:20 +08:00
///
@property (strong,nonatomic)NSMutableSet * bannerDequePool;
///
@property (strong,nonatomic)NSMutableSet * bannerVisiablePool;
///
@property (nonatomic,strong) NSMutableArray<XPGiftBigPrizeModel *> *bigPrizeGiftQueue;
///
@property (nonatomic,strong)dispatch_source_t luckyprizeTimer;
2021-10-14 21:10:04 +08:00
@end
@implementation XPRoomBackContainerView
2021-10-14 21:10:04 +08:00
- (instancetype)initWithdelegate:(id<RoomHostDelegate>)delegate {
self = [super init];
2021-10-14 21:10:04 +08:00
if (self) {
[[NIMSDK sharedSDK].chatManager addDelegate:self];
self.delegate = delegate;
2021-10-14 21:10:04 +08:00
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
2021-11-18 18:43:20 +08:00
- (void)superViewDidLayoutViews {
[self.superview bringSubviewToFront:self.highLevleView];
[self.superview insertSubview:self.middleLevelView belowSubview:self.highLevleView];
[self.superview insertSubview:self.lowLevelView belowSubview:self.middleLevelView];
}
2021-10-14 21:10:04 +08:00
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.backImageView];
2021-11-18 18:43:20 +08:00
///
[self.superview addSubview:self.lowLevelView];
[self.superview addSubview:self.middleLevelView];
[self.superview addSubview:self.highLevleView];
2021-10-14 21:10:04 +08:00
}
- (void)initSubViewConstraints {
[self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
2021-11-18 18:43:20 +08:00
[self.lowLevelView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.superview);
}];
[self.middleLevelView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.superview);
}];
[self.highLevleView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.superview);
}];
2021-10-14 21:10:04 +08:00
}
#pragma mark - NIMChatManagerDelegate
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages {
for (NIMMessage * message in messages) {
[self handleMessage:message];
}
}
//
- (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error {
[self handleMessage:message];
}
- (void)handleMessage:(NIMMessage *)message {
if (message.session.sessionType == NIMSessionTypeChatroom && message.messageType == NIMMessageTypeCustom) {
NIMCustomObject *obj = (NIMCustomObject *)message.messageObject;
if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) {
AttachmentModel *attachment = (AttachmentModel *)obj.attachment;
GiftReceiveInfoModel * receiveInfo = [GiftReceiveInfoModel modelWithJSON:attachment.data];
if (attachment.first == CustomMessageType_Gift) {
receiveInfo.isLuckyBagGift = attachment.second == Custom_Message_Sub_Gift_LuckySend;
2021-11-18 18:43:20 +08:00
receiveInfo.isBatch = YES;
[self receiveGift:receiveInfo];
} else if (attachment.first == CustomMessageType_ALLMicroSend) { //
receiveInfo.isLuckyBagGift = (attachment.second == Custom_Message_Sub_AllMicroLuckySend || attachment.second == Custom_Message_Sub_AllBatchMicroLuckySend);
2021-11-18 18:43:20 +08:00
receiveInfo.isBatch = (attachment.second == Custom_Message_Sub_AllBatchSend || attachment.second == Custom_Message_Sub_AllBatchMicroLuckySend);
[self receiveGift:receiveInfo];
} else if (attachment.first == CustomMessageType_LuckyBag) {//
[self receiveLuckyGiftBigPrize:attachment];
}
}
}
}
#pragma mark -
- (void)receiveLuckyGiftBigPrize:(AttachmentModel *)attachment {
if (attachment.second == Custom_Message_Sub_Room_Gift_LuckBag || attachment.second == Custom_Message_Sub_Room_Gift_LuckBag_Server) {
XPGiftBigPrizeModel * prizeModel = [[XPGiftBigPrizeModel alloc] init];
prizeModel.giftName = attachment.data[@"giftName"];
prizeModel.luckyBagName = attachment.data[@"luckyBagName"];
prizeModel.nick = attachment.data[@"nick"];
[self.bigPrizeGiftQueue addObject:prizeModel];
if (self.luckyprizeTimer == nil) {
[self startHandleBigPrizeGiftTimer];
}
}
}
- (void)startHandleBigPrizeGiftTimer {
NSTimeInterval period = 1.0; //
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0); //
@kWeakify(self);
dispatch_source_set_event_handler(_timer, ^{
@kStrongify(self)
if (self.bigPrizeGiftQueue.count > 0) {
dispatch_sync(dispatch_get_main_queue(), ^{
XPGiftBigPrizeModel * prizeModel = [self.bigPrizeGiftQueue firstObject];
[self createBigPrizeAnimation:prizeModel];
[self.bigPrizeGiftQueue removeObject:prizeModel];
});
}else {
dispatch_source_cancel(_timer);
self.luckyprizeTimer = nil;
}
});
dispatch_resume(_timer);
self.luckyprizeTimer = _timer;
}
- (void)createBigPrizeAnimation:(XPGiftBigPrizeModel *)prizeModel {
NSAttributedString *text = [self createBigPrizeAttribute:prizeModel];
UIImageView *luckyBgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScreenWidth, kNavigationHeight + 15, KScreenWidth, 45)];
luckyBgView.contentMode = UIViewContentModeScaleAspectFill;
luckyBgView.image= [UIImage imageNamed:@"room_lucky_gift_big_prize_bg"];
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, KScreenWidth - 160, 45)];
contentLabel.attributedText = text;
contentLabel.textAlignment = NSTextAlignmentCenter;
contentLabel.numberOfLines = 2;
[luckyBgView addSubview:contentLabel];
2021-11-18 18:43:20 +08:00
[self.middleLevelView addSubview:luckyBgView];
[UIView animateWithDuration:0.5 animations:^{
luckyBgView.frame = CGRectMake(0, kNavigationHeight + 15, KScreenWidth, 45);
} completion:^(BOOL finished) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.5 animations:^{
luckyBgView.hidden = YES;
} completion:^(BOOL finished) {
[luckyBgView removeFromSuperview];
}];
});
}];
}
- (NSAttributedString *)createBigPrizeAttribute:(XPGiftBigPrizeModel *)prizModel {
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
[attributedString appendAttributedString:
[self createTextAttribute:@"哇偶 ! "
color:[ThemeColor messageViewTipColor] font:12]];
[attributedString appendAttributedString:
[self createTextAttribute:prizModel.nick
color:[ThemeColor messageNickColor] font:12]];
[attributedString appendAttributedString:
[self createTextAttribute:@"通过"
color:[ThemeColor messageViewTipColor] font:12]];
[attributedString appendAttributedString:
[self createTextAttribute:prizModel.luckyBagName
color:[ThemeColor messageNickColor] font:12]];
[attributedString appendAttributedString:
[self createTextAttribute:@"开出了"
color:[ThemeColor messageNickColor] font:12]];
[attributedString appendAttributedString:
[self createTextAttribute:prizModel.giftName color:[ThemeColor messageNickColor] font:12]];
return attributedString;
}
///
/// @param text
/// @param color
/// @param font
- (NSMutableAttributedString *)createTextAttribute:(NSString *)text color:(UIColor *)color font:(CGFloat)font {
if (text == nil || text.length <= 0) {
text = @"";
}
NSDictionary * dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12], NSForegroundColorAttributeName:color};
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:text attributes:dic];
return attribute;
}
#pragma mark -
///
- (void)receiveGift:(GiftReceiveInfoModel *)receiveInfo {
///CPU
///TODO:
RoomInfoModel * roomInfor = [self.delegate getRoomInfo];
if (receiveInfo.isLuckyBagGift) {
// SVGA
if (receiveInfo.displayGift.count > 0) {
[receiveInfo.displayGift enumerateObjectsUsingBlock:^(GiftInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.hasVggPic && roomInfor.hasAnimationEffect) {
[self.giftEffectQueue addObject:obj];
}
}];
if (!self.isGiftEffectPlaying && self.giftEffectQueue.count > 0) {
GiftInfoModel * displayInfo = [self.giftEffectQueue firstObject];
[self playGiftEffect:displayInfo];
}
}
} else {
GiftInfoModel *giftInfo = receiveInfo.gift == nil ? receiveInfo.gift : receiveInfo.giftInfo;
if (!giftInfo) {
giftInfo = [[XPGiftStorage shareStorage] findGiftInfo: receiveInfo.giftId];
}
if (giftInfo.hasVggPic && roomInfor.hasAnimationEffect) {
[self.giftEffectQueue addObject:giftInfo];
if (!self.isGiftEffectPlaying && self.giftEffectQueue.count > 0) {
GiftInfoModel * displayInfo = [self.giftEffectQueue firstObject];
[self playGiftEffect:displayInfo];
}
}
if (receiveInfo.targetUsers.count > 0 && receiveInfo.targetUids.count <= 0) {
receiveInfo.targetUids = [receiveInfo.targetUsers valueForKeyPath:@"uid"];
}
NSInteger giftTotal = 0;
if (receiveInfo.targetUids.count > 0) {
giftTotal = receiveInfo.giftNum * giftInfo.goldPrice * receiveInfo.targetUids.count;
} else {
giftTotal = receiveInfo.giftNum * giftInfo.goldPrice;
}
2021-11-18 18:43:20 +08:00
///
if (giftTotal >= 520) {
if (self.giftBannerQueue.count == 0) {
[self createGiftBannerViewAnimation:receiveInfo];//
[self.giftBannerQueue addObject:receiveInfo];
} else {
[self.giftBannerQueue addObject:receiveInfo];
}
}
}
}
- (void)createGiftBannerViewAnimation:(GiftReceiveInfoModel *)receiveInfo {
__block XPRoomGiftBannerView *view = [self.bannerDequePool anyObject];
if (view == nil) {
view = [[XPRoomGiftBannerView alloc] init];
[self.bannerVisiablePool addObject:view];
}else{
[self.bannerDequePool removeObject:view];
}
2021-11-18 18:43:20 +08:00
[view configGiftBanner:receiveInfo users:[self makeBannerUsers]];
view.frame = CGRectMake(KScreenWidth, 100, KScreenWidth, 153);
[self.middleLevelView addSubview:view];
POPSpringAnimation *springAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewCenter];
springAnimation.springSpeed = 12;
springAnimation.springBounciness = 10.f;
springAnimation.fromValue = [NSValue valueWithCGPoint:view.center];
springAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(view.frame.size.width / 2, view.center.y)];
@kWeakify(self);
[springAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
@kStrongify(self);
if (finished) {
[self removeAnimation:view];
}
}];
[view pop_addAnimation:springAnimation forKey:@"spingOutAnimation"];
}
//
- (void)removeAnimation:(XPRoomGiftBannerView *)view{
POPBasicAnimation *moveAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewCenter];
moveAnimation.fromValue = [NSValue valueWithCGPoint:view.center];
moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(view.center.x - KScreenWidth, view.center.y)];
moveAnimation.beginTime = CACurrentMediaTime() +5;
moveAnimation.duration = 0.5;
moveAnimation.repeatCount = 1;
moveAnimation.removedOnCompletion = YES;
@kWeakify(self);
[moveAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
@kStrongify(self);
if (finished) {
[self.giftBannerQueue removeObjectAtIndex:0];
if (self.giftBannerQueue.count > 0) {
[self createGiftBannerViewAnimation:self.giftBannerQueue.firstObject];
}
[view removeFromSuperview];
[view resetData];
[self.bannerVisiablePool removeObject:view];
[self.bannerDequePool addObject:view];
}
}];
[view pop_addAnimation:moveAnimation forKey:@"moveOutAnimation"];
}
2021-11-18 18:43:20 +08:00
- (NSArray<XPGiftBannerUserInfoModel *> *)makeBannerUsers {
NSArray<MicroQueueModel *> * microArray = [self.delegate getMicroQueue].allValues;
NSMutableArray * array = [NSMutableArray array];
[microArray enumerateObjectsUsingBlock:^(MicroQueueModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
XPGiftBannerUserInfoModel * bannserUser = [[XPGiftBannerUserInfoModel alloc] init];
bannserUser.position = obj.microState.position;
bannserUser.uid = obj.userInfo.uid;
[array addObject:bannserUser];
}];
return array;
}
/// SVGA
- (void)playGiftEffect:(GiftInfoModel *)giftInfo {
self.isGiftEffectPlaying = YES;
self.giftEffectView.hidden = NO;
if (self.giftEffectView.superview == nil) {
[self.superview addSubview:self.giftEffectView];
[self.giftEffectView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.mas_equalTo(self.superview);
make.width.mas_equalTo(KScreenWidth);
make.height.mas_equalTo(KScreenHeight);
}];
}
2021-11-18 18:43:20 +08:00
[self.lowLevelView bringSubviewToFront:self.giftEffectView];
[self.parser parseWithURL:[NSURL URLWithString:giftInfo.vggUrl] completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
if (videoItem != nil) {
CGFloat width = videoItem.videoSize.width;
CGFloat height = videoItem.videoSize.height;
if (width > height) {
self.giftEffectView.contentMode = UIViewContentModeScaleAspectFit;
} else {//
CGFloat resizeH = KScreenWidth * height / width;//
if (resizeH > KScreenHeight) {//
self.giftEffectView.contentMode = UIViewContentModeScaleAspectFill;
} else {//
self.giftEffectView.contentMode = UIViewContentModeScaleAspectFit;
}
}
self.giftEffectView.alpha = 1;
self.giftEffectView.loops = 1;
self.giftEffectView.clearsAfterStop = YES;
self.giftEffectView.videoItem = videoItem;
[self.giftEffectView startAnimation];
}
} failureBlock:^(NSError * _Nullable error) {
}];
[self.giftEffectQueue removeObject:giftInfo];
}
#pragma mark - SVGAPlayerDelegate
- (void)svgaPlayerDidFinishedAnimation:(SVGAPlayer *)player {
if (player == self.giftEffectView) {
self.giftEffectView.hidden = YES;
self.isGiftEffectPlaying = NO;
if (self.giftEffectQueue.count > 0) {
GiftInfoModel * infor = [self.giftEffectQueue firstObject];
[self playGiftEffect:infor];
}
}
}
2021-10-14 21:10:04 +08:00
#pragma mark - Getters And Setters
- (UIImageView *)backImageView {
if (!_backImageView) {
_backImageView = [[UIImageView alloc] init];
_backImageView.userInteractionEnabled = YES;
_backImageView.image = [UIImage imageNamed:@"room_background"];
_backImageView.layer.masksToBounds = YES;
_backImageView.contentMode = UIViewContentModeScaleAspectFill;
}
return _backImageView;
}
- (SVGAImageView *)giftEffectView {
if (!_giftEffectView) {
_giftEffectView = [[SVGAImageView alloc]init];
_giftEffectView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
_giftEffectView.userInteractionEnabled = NO;
_giftEffectView.delegate = self;
_giftEffectView.hidden = YES;
}
return _giftEffectView;
}
- (SVGAParser *)parser {
if (!_parser) {
_parser = [[SVGAParser alloc]init];
}
return _parser;
}
- (NSMutableArray<GiftInfoModel *> *)giftEffectQueue {
if (!_giftEffectQueue) {
_giftEffectQueue = [NSMutableArray array];
}
return _giftEffectQueue;
}
- (NSMutableArray<XPGiftBigPrizeModel *> *)bigPrizeGiftQueue {
if (!_bigPrizeGiftQueue) {
_bigPrizeGiftQueue = [NSMutableArray array];
}
return _bigPrizeGiftQueue;
}
- (NSMutableArray<GiftReceiveInfoModel *> *)giftBannerQueue {
if (!_giftBannerQueue) {
_giftBannerQueue = [NSMutableArray array];
}
return _giftBannerQueue;
}
2021-11-18 18:43:20 +08:00
- (NSMutableSet *)bannerDequePool {
if (!_bannerDequePool) {
_bannerDequePool = [NSMutableSet set];
}
return _bannerDequePool;
}
- (NSMutableSet *)bannerVisiablePool {
if (!_bannerVisiablePool) {
_bannerVisiablePool = [NSMutableSet set];
}
return _bannerVisiablePool;
}
- (UIView *)lowLevelView {
if (!_lowLevelView) {
_lowLevelView = [[UIView alloc] init];
_lowLevelView.backgroundColor = [UIColor clearColor];
}
return _lowLevelView;
}
- (UIView *)middleLevelView {
if (!_middleLevelView) {
_middleLevelView = [[UIView alloc] init];
_middleLevelView.backgroundColor = [UIColor clearColor];
}
return _middleLevelView;
}
- (UIView *)highLevleView {
if (!_highLevleView) {
_highLevleView = [[UIView alloc] init];
_highLevleView.backgroundColor = [UIColor clearColor];
}
return _highLevleView;
}
2021-10-14 21:10:04 +08:00
@end