直播火箭逻辑整理

//
//  AutoRocket.swift

import Foundation
import UIKit

class AutoRocket: UIView {
    @objc var autoWillShowBlock:blcok?
    @objc var autoWillHideBlock:blcok?
    @objc var tapWillHideBlock:blcok?
    @objc var tapBlock:blcok?
    @objc var dic:NSDictionary?
    @objc var messageArr:NSMutableArray!
    @objc var showTextTimer:Timer?
    @objc var SendShowType : Int
    //是否立即结束当前礼物动画:
    //收到一条礼物数值更大的信息时,需要当前动画到屏幕中时立即滑出屏幕,开始
    var status : RocketStatus
    @objc var nowShowTime : Int
    var autoType : RocketAutoType
    @objc var anchorNickName : NSString
    
    var bgV : UIImageView!
    var bgVWordMiddleBgv : UIImageView!
    var sendPersonPhoto : UIImageView!
    var contentLB : STRAutoScrollLabel!
    var giftImg : UIImageView!
    var Ximg : UIImageView!
    var giftNumLb : UILabel!
    var GoBtn : CTBtn!
    var bgImage : UIImage! = UIImage.init(named: "liveroom_rocketbackgroundimage")
    
    
    @objc init(frame: CGRect,SendShowType:Int) {
        self.SendShowType = SendShowType
        self.status = RocketStatus.RocketCanStartNewAnimate
        self.nowShowTime = 0
        self.autoType = RocketAutoType.RocketTypeAuto
        self.anchorNickName = ""
        super.init(frame: frame)
        
        self.clipsToBounds = true
        self.isUserInteractionEnabled = true
        
        self.bgV = UIImageView.init(frame: CGRect.init(x: frame.size.width, y: 0, width: frame.size.width, height: frame.size.height));
        self.bgV.isUserInteractionEnabled = true
        self.bgV.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapToDo)))
        self.addSubview(self.bgV)
        
        self.bgVWordMiddleBgv = UIImageView.init()
        self.bgV.addSubview(self.bgVWordMiddleBgv)
        
        self.sendPersonPhoto = UIImageView.init()
        self.bgV.addSubview(self.sendPersonPhoto)
        
        self.contentLB = STRAutoScrollLabel.init()
        self.bgV.addSubview(self.contentLB)
        self.contentLB.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
        
        self.giftImg = UIImageView.init()
        self.bgV.addSubview(self.giftImg)
        
        self.Ximg = UIImageView.init()
        self.bgV.addSubview(self.Ximg)
        
        self.giftNumLb = UILabel.init()
        self.giftNumLb.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
        self.bgV.addSubview(self.giftNumLb)
        
        self.GoBtn = CTBtn.init(frame: CGRect.zero)
        self.GoBtn.isUserInteractionEnabled = false
        self.GoBtn.clipsToBounds = true
        self.bgV.addSubview(self.GoBtn)
        
        
        self.messageArr = NSMutableArray.init()
        
    }
    @objc func tapToDo() -> Void {
        if (tapBlock != nil) {
            tapBlock!();
        }
    }
    
    @objc func showWith( infoDic : NSDictionary ) -> Void {
        self.dic = infoDic
        self.isHidden = false
        
        if self.superview != nil {
            self.superview?.bringSubview(toFront: self)
        }
        var originY:CGFloat = 0
        
        if self.SendShowType == 1 {
            //观众端
            if SYBScreenWidth() > SYBScreenHeight() {
                originY =  CGFloat(25)+CGFloat(35)+CGFloat(12.5)
            }else{
                originY = (CGFloat(25)+CGFloat(35)+CGFloat(12.5)+CGFloat(SNvBarYMargen()))
            }
        }else if(self.SendShowType == 2){
            //主播端
            originY = CGFloat(5) + CGFloat(X_SafeAreaStatusBarHeight())+CGFloat(35)+CGFloat(7)
        }
        //自动:聊天服务器、runway接口原生数据
        //手动:跑道固定位封装过的数据
        //需要做区别
        if (self.autoType == RocketAutoType.RocketTypeAuto) {
            
            //自动的跑道,通知跑道固定位
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: RocketViewNotifatiPMTStr), object: nil, userInfo: (infoDic as! [AnyHashable : Any]))
            
            //自动的,有显示文字的时间
            var protecttime : NSNumber = NSNumber.init(value: 20)
            
            if  infoDic["isGuard"] != nil && NSString.init(string: "\(infoDic["isGuard"]!)").isEqual(to: "1") {
                //守护
                originY -= CGFloat(guardOrNormalGiftOffsetY)
                let avatar = infoDic["avatar"] != nil ? "\(infoDic["avatar"]!)" : ""
                let usernNickName = infoDic["nickname"] != nil ? NSString.init(string: "\( infoDic["nickname"]!)"):""
                let liverNickName = infoDic["anchornickname"] != nil ?  "\( infoDic["anchornickname"]!)":""
                
                let roomNumber = infoDic["roomnumber"] != nil ?  NSString.init(string: "\( infoDic["roomnumber"]!)"):""
                self.tag = roomNumber.integerValue
                let content = NSString.init(string: "\(usernNickName)成功守护了\(liverNickName)")
                
                self.sendPersonPhoto.frame = CGRect.init(x: 2, y: yellowHeight - avatarHeight - 2 - 2, width: avatarHeight, height: avatarHeight)
                self.sendPersonPhoto.layer.masksToBounds = true
                self.sendPersonPhoto.layer.cornerRadius = CGFloat(avatarHeight / 2)
                self.sendPersonPhoto.sd_setImage(with: URL.init(string: "\(avatar)"), placeholderImage: UIImage.init(named: "avatarPlaceholder"))
                
                let contentLblWidth = JJHUtil.textWidth(content as String, font: CGFloat(fontSize), textMaxWidth: 150)
                self.contentLB.frame = CGRect.init(x: self.sendPersonPhoto.frame.origin.x + self.sendPersonPhoto.bounds.size.width+CGFloat(padding), y: CGFloat((yellowHeight - 10)/2), width: contentLblWidth, height: CGFloat(10))
                let attributedString = NSMutableAttributedString.init(string: content as String)
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black, range: NSMakeRange(0, content.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0x8B5004), range: NSMakeRange(usernNickName.length, 5))
                self.contentLB.attributedText = attributedString
                self.contentLB.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
                self.contentLB.sizeToFit()
                
                
                let boundsWidth = self.contentLB.frame.origin.x + self.contentLB.bounds.size.width + CGFloat(padding * 2 + 5 * 2)
                self.frame = CGRect.init(x: SYBScreenWidth()-CGFloat(10)-boundsWidth, y: originY, width: boundsWidth, height: CGFloat(yellowHeight))
                self.bgV.frame = CGRect.init(x: self.bounds.size.width, y: 0, width: self.bounds.size.width, height: CGFloat(yellowHeight - 2))
                self.bgV.image = self.bgImage.stretchableImage(withLeftCapWidth: Int(self.bgImage.size.width/2), topCapHeight: 0)
                
                self.bgVWordMiddleBgv.isHidden = true;
                self.giftImg.frame = CGRect.zero;
                self.Ximg.frame = CGRect.zero;
                self.giftNumLb.frame = CGRect.zero;
                self.GoBtn.frame = CGRect.zero;
                
                protecttime  = infoDic["protecttime"] as? NSNumber ?? NSNumber.init(value: 20)
                self.anchorNickName = liverNickName as NSString
            }else if(infoDic["info"] != nil && infoDic["info"] is NSDictionary && ( ((infoDic["info"] as! NSDictionary)["giftMold"] != nil && NSString.init(string: "\((infoDic["info"] as! NSDictionary)["giftMold"]!)").isEqual(to: "1")) || ((infoDic["info"] as! NSDictionary)["isCommand"] != nil && NSString.init(string: "\( (infoDic["info"] as! NSDictionary)["isCommand"]!)").isEqual(to: "1")) || ((infoDic["info"] as! NSDictionary)["isCommand"] != nil && NSString.init(string: "\( (infoDic["info"] as! NSDictionary)["isCommand"]!)").isEqual(to: "3")) )) {
                //世界礼物小跑道
                originY -= CGFloat(worldGiftOffsetY)
                let Width = 2+25+250+15,height = 11+3+28+4+4
                // 0 -9 40 45
                self.sendPersonPhoto.frame = CGRect.init(x: 0, y: 0, width: 45, height: 50);
                self.sendPersonPhoto.image = UIImage.init(named: "allGiftHead")
                self.sendPersonPhoto.layer.masksToBounds = false
                
                self.bgVWordMiddleBgv.image = UIImage.init(named: "AllWorldGiftBg")
                
                let userInfo = infoDic["userInfo"] as? NSDictionary ?? NSDictionary.init()
                let roomInfo = infoDic["roomInfo"] as? NSDictionary ?? NSDictionary.init()
                let info = infoDic["info"] as? NSDictionary ?? NSDictionary.init()
                
                //信息,老铁们一起来
                ///设置房间号
                let roomNumber = roomInfo["roomnumber"] != nil ?  NSString.init(string: "\( roomInfo["roomnumber"]!)"):""
                self.tag = roomNumber.integerValue
                let usernNickName = userInfo["nickname"] != nil ?  NSString.init(string: "\( userInfo["nickname"]!)"):""
                let liverNickName = roomInfo["anchornickname"] != nil ?  NSString.init(string: "\( roomInfo["anchornickname"]!)"):""
                let content = NSString.init(string: "\(usernNickName) 在 \(liverNickName)的直播间召唤老铁们一起来挖宝")
                
                let attributedString = NSMutableAttributedString.init(string: content as String)
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0x92FBFF), range: NSMakeRange(0, content.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0xFFFC14), range: NSMakeRange(0, usernNickName.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0xFFFC14), range: NSMakeRange(usernNickName.length+3, liverNickName.length))
                
                self.contentLB.frame = CGRect.init(x: 2+25+26, y: 11+2, width: Width - (2+25+26+7+45+15), height: 28)
                self.bgVWordMiddleBgv.frame = CGRect.init(x: 2+25, y: 11, width: Width-(2+25), height: 3+28+4);
                
                //计算布局 26, 0 ,width - (26 +62),height
                self.GoBtn.frame = CGRect.init(x: self.contentLB.frame.origin.x+self.contentLB.frame.size.width+CGFloat(4), y: CGFloat(11+3+(28 - Int(17.5))/2), width: CGFloat(45), height: CGFloat(17.5))
                self.GoBtn.ctImgV.frame = self.GoBtn.bounds
                self.GoBtn.ctImgV.image = UIImage.init(named: "AllWorldGiftGo")
                
                self.contentLB.font = UIFont.init(name: "ShiShangZhongHei", size: CGFloat(11))
                self.contentLB.attributedText = attributedString
                
                
                self.frame = CGRect.init(x: SYBScreenWidth() - 10 - CGFloat(Width), y: originY, width: CGFloat(Width), height: CGFloat(height))
                
                self.bgV.frame = CGRect.init(x: self.bounds.size.width, y: 0.0, width: self.bounds.size.width, height: self.bounds.size.height)
                
                self.bgV.image = UIImage.init()
                self.bgVWordMiddleBgv.isHidden = false
                self.giftImg.frame = CGRect.zero
                self.giftNumLb.frame = CGRect.zero
                
                protecttime  = info["protecttime"] as? NSNumber ?? NSNumber.init(value: 20)
                self.anchorNickName = liverNickName as NSString
            }else{
                //普通礼物跑道
                originY -= CGFloat(guardOrNormalGiftOffsetY)
                
                
                let userInfo = infoDic["userInfo"] as? NSDictionary ?? NSDictionary.init()
                let roomInfo = infoDic["roomInfo"] as? NSDictionary ?? NSDictionary.init()
                let info = infoDic["info"] as? NSDictionary ?? NSDictionary.init()
                
                
                let roomNumber = roomInfo["roomnumber"] != nil ?  NSString.init(string: "\( roomInfo["roomnumber"]!)"):""
                self.tag = roomNumber.integerValue
                
                let avatar = userInfo["avatar"] != nil ? NSString.init(string: "\( userInfo["avatar"]!)") : ""
                let usernNickName = userInfo["nickname"] != nil ? NSString.init(string: "\( userInfo["nickname"]!)") : ""
                let liverNickName = roomInfo["nickname"] != nil ? NSString.init(string: "\( roomInfo["nickname"]!)") : ""
                let content =  NSString.init(string: "\(usernNickName)送给\(liverNickName)")
                let giftIcon = info["giftSmallImage"] != nil ? NSString.init(string: "\(info["giftSmallImage"]!)") : ""
                let giftNum = info["giftNum"] != nil ? info["giftNum"]!:""
                let giftNumContent = NSString.init(string: "x\( giftNum)")
                
                //计算布局
                self.sendPersonPhoto.frame = CGRect.init(x: 2, y: yellowHeight - avatarHeight - 2 - 2, width: avatarHeight, height: avatarHeight)
                self.sendPersonPhoto.layer.masksToBounds = true
                self.sendPersonPhoto.layer.cornerRadius = CGFloat(avatarHeight / 2)
                self.sendPersonPhoto.sd_setImage(with: URL.init(string: "\(avatar)"), placeholderImage: UIImage.init(named: "avatarPlaceholder"))
                
                let contentLblWidth = JJHUtil.textWidth("\(content)", font: UIFont.systemFont(ofSize: CGFloat(fontSize)))
                self.contentLB.frame = CGRect.init(x:  self.sendPersonPhoto.frame.origin.x +  self.sendPersonPhoto.bounds.size.width + CGFloat(padding), y: CGFloat((yellowHeight - 10 ) / 2), width: CGFloat(contentLblWidth), height: CGFloat(10))
                let attributedString = NSMutableAttributedString.init(string: "\(content)")
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black, range: NSRange.init(location: 0, length: content.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0x8b5004), range: NSMakeRange(usernNickName.length, 2))
                self.contentLB.attributedText = attributedString
                self.contentLB.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
                self.contentLB.sizeToFit()
                
                
                self.giftImg.frame = CGRect.init(x: self.contentLB.frame.origin.x + self.contentLB.bounds.size.width+CGFloat(padding), y: CGFloat((yellowHeight - 24)/2 - 1), width: CGFloat(21), height: CGFloat(24))
                self.giftImg.sd_setImage(with: URL.init(string: "\(giftIcon)"), placeholderImage: UIImage.init(named: "YBLogo"))
                
                
                
                let giftNumberWidth = JJHUtil.textWidth("\(giftNumContent)", font: CGFloat(fontSize), textMaxWidth: 100)
                self.giftNumLb.frame = CGRect.init(x: self.giftImg.frame.origin.x + self.giftImg.bounds.size.width + CGFloat(padding), y: CGFloat((yellowHeight - Int(fontSize+2))/2), width: CGFloat(giftNumberWidth), height: CGFloat((fontSize+2)))
                self.giftNumLb.text = giftNumContent as String
                
                
                
                let boundsWidth = CGFloat(self.giftNumLb.frame.origin.x) + CGFloat(self.giftNumLb.bounds.size.width) + CGFloat(padding*2) + CGFloat(5*2)
                
                self.frame = CGRect.init(x: SYBScreenWidth() - 10 - boundsWidth, y: originY, width: boundsWidth, height: CGFloat(yellowHeight))
                
                self.bgV.frame = CGRect.init(x: self.bounds.size.width, y: 0.0, width: self.bounds.size.width, height: CGFloat(yellowHeight) - 2)
                
                self.bgV.image = self.bgImage.stretchableImage(withLeftCapWidth: Int(self.bgImage.size.width/2), topCapHeight: 0)
                self.GoBtn.frame = CGRect.zero
                self.bgVWordMiddleBgv.isHidden = true
                
                protecttime  = info["protecttime"] as? NSNumber ?? NSNumber.init(value: 20)
                self.anchorNickName = liverNickName as NSString
            }
            
            protecttime = protecttime.intValue != 0 ? protecttime :NSNumber.init(value: 20)
            self.nowShowTime = protecttime.intValue
            self.scrollShow(show: true)
        }else if (self.autoType == RocketAutoType.RocketTypeTap){
            
            if  infoDic["isGuard"] != nil && NSString.init(string: "\(infoDic["isGuard"]!)").isEqual(to: "1") {
                //守护
                originY -= CGFloat(guardOrNormalGiftOffsetY)
                let avatar = infoDic["avatar"] != nil ? "\(infoDic["avatar"]!)" : ""
                let usernNickName = infoDic["nickname"] != nil ? NSString.init(string: "\( infoDic["nickname"]!)"):""
                let liverNickName = infoDic["anchornickname"] != nil ?  "\( infoDic["anchornickname"]!)":""
                
                let roomNumber = infoDic["roomnumber"] != nil ?  NSString.init(string: "\( infoDic["roomnumber"]!)"):""
                self.tag = roomNumber.integerValue
                let content = NSString.init(string: "\(usernNickName)成功守护了\(liverNickName)")
                
                self.sendPersonPhoto.frame = CGRect.init(x: 2, y: yellowHeight - avatarHeight - 2 - 2, width: avatarHeight, height: avatarHeight)
                self.sendPersonPhoto.layer.masksToBounds = true
                self.sendPersonPhoto.layer.cornerRadius = CGFloat(avatarHeight / 2)
                self.sendPersonPhoto.sd_setImage(with: URL.init(string: "\(avatar)"), placeholderImage: UIImage.init(named: "avatarPlaceholder"))
                
                let contentLblWidth = JJHUtil.textWidth(content as String, font: CGFloat(fontSize), textMaxWidth: 150)
                self.contentLB.frame = CGRect.init(x: self.sendPersonPhoto.frame.origin.x + self.sendPersonPhoto.bounds.size.width+CGFloat(padding), y: CGFloat((yellowHeight - 10)/2), width: contentLblWidth, height: CGFloat(10))
                let attributedString = NSMutableAttributedString.init(string: content as String)
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black, range: NSMakeRange(0, content.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0x8B5004), range: NSMakeRange(usernNickName.length, 5))
                self.contentLB.attributedText = attributedString
                self.contentLB.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
                self.contentLB.sizeToFit()
                
                
                let boundsWidth = self.contentLB.frame.origin.x + self.contentLB.bounds.size.width + CGFloat(padding * 2 + 5 * 2)
                self.frame = CGRect.init(x: SYBScreenWidth()-CGFloat(10)-boundsWidth, y: originY, width: boundsWidth, height: CGFloat(yellowHeight))
                self.bgV.frame = CGRect.init(x: self.bounds.size.width, y: 0, width: self.bounds.size.width, height: CGFloat(yellowHeight - 2))
                self.bgV.image = self.bgImage.stretchableImage(withLeftCapWidth: Int(self.bgImage.size.width/2), topCapHeight: 0)
                
                self.bgVWordMiddleBgv.isHidden = true;
                self.giftImg.frame = CGRect.zero;
                self.Ximg.frame = CGRect.zero;
                self.giftNumLb.frame = CGRect.zero;
                self.GoBtn.frame = CGRect.zero;
                
                self.anchorNickName = liverNickName as NSString
            }else if((infoDic["giftMold"] != nil && NSString.init(string: "\(infoDic["giftMold"]!)").isEqual(to: "1")) || (infoDic["isCommand"] != nil && NSString.init(string: "\( infoDic["isCommand"]!)").isEqual(to: "1")) || (infoDic["isCommand"] != nil && NSString.init(string: "\( infoDic["isCommand"]!)").isEqual(to: "3")) ) {
                //世界礼物小跑道
                originY -= CGFloat(worldGiftOffsetY)
                let Width = 2+25+250+15,height = 11+3+28+4+4
                // 0 -9 40 45
                self.sendPersonPhoto.frame = CGRect.init(x: 0, y: 0, width: 45, height: 50);
                self.sendPersonPhoto.image = UIImage.init(named: "allGiftHead")
                self.sendPersonPhoto.layer.masksToBounds = false
                
                self.bgVWordMiddleBgv.image = UIImage.init(named: "AllWorldGiftBg")
                
                //信息,老铁们一起来
                ///设置房间号
                let roomNumber = infoDic["roomnumber"] != nil ?  NSString.init(string: "\( infoDic["roomnumber"]!)"):""
                self.tag = roomNumber.integerValue
                let usernNickName = infoDic["nickname"] != nil ?  NSString.init(string: "\( infoDic["nickname"]!)"):""
                let liverNickName = infoDic["anchornickname"] != nil ?  NSString.init(string: "\( infoDic["anchornickname"]!)"):""
                let content = NSString.init(string: "\(usernNickName) 在 \(liverNickName)的直播间召唤老铁们一起来挖宝")
                
                let attributedString = NSMutableAttributedString.init(string: content as String)
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0x92FBFF), range: NSMakeRange(0, content.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0xFFFC14), range: NSMakeRange(0, usernNickName.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0xFFFC14), range: NSMakeRange(usernNickName.length+3, liverNickName.length))
                
                self.contentLB.frame = CGRect.init(x: 2+25+26, y: 11+2, width: Width - (2+25+26+7+45+15), height: 28)
                self.bgVWordMiddleBgv.frame = CGRect.init(x: 2+25, y: 11, width: Width-(2+25), height: 3+28+4);
                
                //计算布局 26, 0 ,width - (26 +62),height
                self.GoBtn.frame = CGRect.init(x: self.contentLB.frame.origin.x+self.contentLB.frame.size.width+CGFloat(4), y: CGFloat(11+3+(28 - Int(17.5))/2), width: CGFloat(45), height: CGFloat(17.5))
                self.GoBtn.ctImgV.frame = self.GoBtn.bounds
                self.GoBtn.ctImgV.image = UIImage.init(named: "AllWorldGiftGo")
                
                self.contentLB.font = UIFont.init(name: "ShiShangZhongHei", size: CGFloat(11))
                self.contentLB.attributedText = attributedString
                
                
                self.frame = CGRect.init(x: SYBScreenWidth() - 10 - CGFloat(Width), y: originY, width: CGFloat(Width), height: CGFloat(height))
                
                self.bgV.frame = CGRect.init(x: self.bounds.size.width, y: 0.0, width: self.bounds.size.width, height: self.bounds.size.height)
                
                self.bgV.image = UIImage.init()
                self.bgVWordMiddleBgv.isHidden = false
                self.giftImg.frame = CGRect.zero
                self.giftNumLb.frame = CGRect.zero
                
                self.anchorNickName = liverNickName as NSString
            }else{
                //普通礼物跑道
                originY -= CGFloat(guardOrNormalGiftOffsetY)
                let roomNumber = infoDic["avatar"] != nil ?  NSString.init(string: "\( infoDic["roomnumber"]!)"):""
                self.tag = roomNumber.integerValue
                
                let avatar = infoDic["avatar"] != nil ? NSString.init(string: "\( infoDic["avatar"]!)") : ""
                let usernNickName = infoDic["nickname"] != nil ? NSString.init(string: "\( infoDic["nickname"]!)") : ""
                let liverNickName = infoDic["anchornickname"] != nil ? NSString.init(string: "\( infoDic["anchornickname"]!)") : ""
                let content =  NSString.init(string: "\(usernNickName)送给\(liverNickName)")
                let giftIcon = infoDic["gifticon"] != nil ? NSString.init(string: "\(infoDic["gifticon"]!)") : ""
                let giftNum = infoDic["giftnum"] != nil ? infoDic["giftnum"]!:""
                let giftNumContent = NSString.init(string: "x\( giftNum)")
                
                //计算布局
                self.sendPersonPhoto.frame = CGRect.init(x: 2, y: yellowHeight - avatarHeight - 2 - 2, width: avatarHeight, height: avatarHeight)
                self.sendPersonPhoto.layer.masksToBounds = true
                self.sendPersonPhoto.layer.cornerRadius = CGFloat(avatarHeight / 2)
                self.sendPersonPhoto.sd_setImage(with: URL.init(string: "\(avatar)"), placeholderImage: UIImage.init(named: "avatarPlaceholder"))
                
                let contentLblWidth = JJHUtil.textWidth("\(content)", font: UIFont.systemFont(ofSize: CGFloat(fontSize)))
                self.contentLB.frame = CGRect.init(x:  self.sendPersonPhoto.frame.origin.x +  self.sendPersonPhoto.bounds.size.width + CGFloat(padding), y: CGFloat((yellowHeight - 10 ) / 2), width: CGFloat(contentLblWidth), height: CGFloat(10))
                let attributedString = NSMutableAttributedString.init(string: "\(content)")
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.black, range: NSRange.init(location: 0, length: content.length))
                attributedString.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColorFromRGB(rgbValue: 0x8b5004), range: NSMakeRange(usernNickName.length, 2))
                self.contentLB.attributedText = attributedString
                self.contentLB.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
                self.contentLB.sizeToFit()
                
                
                self.giftImg.frame = CGRect.init(x: self.contentLB.frame.origin.x + self.contentLB.bounds.size.width+CGFloat(padding), y: CGFloat((yellowHeight - 24)/2 - 1), width: CGFloat(21), height: CGFloat(24))
                self.giftImg.sd_setImage(with: URL.init(string: "\(giftIcon)"), placeholderImage: UIImage.init(named: "YBLogo"))
                
                
                
                let giftNumberWidth = JJHUtil.textWidth("\(giftNumContent)", font: CGFloat(fontSize), textMaxWidth: 100)
                self.giftNumLb.frame = CGRect.init(x: self.giftImg.frame.origin.x + self.giftImg.bounds.size.width + CGFloat(padding), y: CGFloat((yellowHeight - Int(fontSize+2))/2), width: CGFloat(giftNumberWidth), height: CGFloat((fontSize+2)))
                self.giftNumLb.text = giftNumContent as String
                
                
                
                let boundsWidth = CGFloat(self.giftNumLb.frame.origin.x) + CGFloat(self.giftNumLb.bounds.size.width) + CGFloat(padding*2) + CGFloat(5*2)
                
                self.frame = CGRect.init(x: SYBScreenWidth() - 10 - boundsWidth, y: originY, width: boundsWidth, height: CGFloat(yellowHeight))
                
                self.bgV.frame = CGRect.init(x: self.bounds.size.width, y: 0.0, width: self.bounds.size.width, height: CGFloat(yellowHeight) - 2)
                
                self.bgV.image = self.bgImage.stretchableImage(withLeftCapWidth: Int(self.bgImage.size.width/2), topCapHeight: 0)
                self.GoBtn.frame = CGRect.zero
                self.bgVWordMiddleBgv.isHidden = true
                
                self.anchorNickName = liverNickName as NSString
            }
            self.scrollShow(show: true)
        }
        
    }
    
    @objc func scrollShow(show:Bool) -> Void {
        if show {
            if self.superview != nil{
                self.superview!.bringSubview(toFront: self)
            }
            self.isHidden = false
            self.status = RocketStatus.RocketWillShowText
            if(self.autoType == RocketAutoType.RocketTypeAuto){
                if (self.autoWillShowBlock != nil){
                    self.autoWillShowBlock!()
                }
            }
            
            UIView.setAnimationsEnabled(true)
            UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
                self.bgV.frame = CGRect.init(x: 0, y: self.bgV.frame.origin.y, width: self.bgV.frame.size.width, height: self.bgV.frame.size.height)
            }) { (_) in
                self.contentLB.startAnimate()
                
                if (self.showTextTimer != nil){
                    self.showTextTimer!.invalidate()
                    self.showTextTimer = nil
                }
                self.status = RocketStatus.RockeShowingText
                
                //自动:会自动执行隐藏动画
                //手动:不执行隐藏动画
                if (self.autoType == RocketAutoType.RocketTypeAuto){
                    //返回时间若有负值,虽然理论上不会出现,但以防万一
                    self.showTextTimer = Timer.scheduledTimer(timeInterval: TimeInterval( self.nowShowTime >= 0 ? self.nowShowTime : 0), target: self, selector: #selector(self.hideWithBlock), userInfo: nil, repeats: false)
                }
                
                
            }
        }else{
            //自动的不隐藏,等倒计时自动隐藏
            //手动的隐藏
            if (self.autoType == RocketAutoType.RocketTypeTap){
                self.hideWithBlock()
            }
        }
    }
    
    @objc func tapShowPaoDaoInfoView(info : NSDictionary?) -> Void {
        //点击跑道固定位
        //自动|手动无关
        //只判断状态是否可以直接执行动画,并改变为手动类型
        
        if (self.status == RocketStatus.RocketCanStartNewAnimate) {
            //根本没有动画中
            //显示字典礼物,但是不add到字典
            self.autoType = RocketAutoType.RocketTypeTap
            if (info != nil){
               self.showWith(infoDic:info!)
            }
        }else if(self.status == RocketStatus.RocketWillShowText){
            //在滑行显示中,
            //理论上,固定位被挡着,不做操作
        }else if(self.status == RocketStatus.RockeShowingText){
            //显示文字滚动中
            //理论上,固定位被挡着,不做操作
            
        }else if(self.status == RocketStatus.RockeShowTextOver){
            //在滑行隐藏中
            //理论上,固定位被挡着,不做操作
        }
    }
    
    @objc func hideWithBlock() -> Void {
        self.status = RocketStatus.RockeShowTextOver
        if (self.showTextTimer != nil){
            self.showTextTimer!.invalidate()
            self.showTextTimer = nil
        }
        if (self.autoType == RocketAutoType.RocketTypeAuto){
            
            //没有自动滑行的礼物了,在执行显示跑道位动画
            if (self.autoWillHideBlock != nil){
                if (self.messageArr.count <= 1){
                    self.autoWillHideBlock!()
                }
            }
        }
        if (self.autoType == RocketAutoType.RocketTypeTap){
            //没有自动滑行的礼物了,在执行显示跑道位动画
            if (self.tapWillHideBlock != nil){
                self.tapWillHideBlock!()
            }
        }
        
        UIView.setAnimationsEnabled(true)
        UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.curveLinear, animations: {
            self.bgV.frame = CGRect.init(x: self.frame.size.width, y: self.bgV.frame.origin.y, width: self.bgV.frame.size.width, height: self.bgV.frame.size.height)
        }) { (_) in
            self.isHidden = true
            self.status = RocketStatus.RocketCanStartNewAnimate
            self.contentLB.stopAnimate()
            
            //手动:只隐藏,不执行删除新动画
            //自动:删除元素,执行新动画
            if (self.autoType == RocketAutoType.RocketTypeAuto){
                if (self.messageArr.count > 0){
                    self.messageArr.remove(self.dic as Any)
                }
                
                self.dic = nil
                
                if self.messageArr.count > 0 {
                    self.showWith(infoDic: self.messageArr.lastObject as! NSDictionary)
                }
            }
        }
        
        
        
    }
    
    @objc func add(message : NSDictionary!) -> Void {
        //这次的跑道金额小于等于上次的,就return
        
        if (self.messageArr.count > 0 && self.messageArr.lastObject is NSDictionary ) {
            let lastDic : NSDictionary = self.messageArr.lastObject! as! NSDictionary
            //聊天服务器守护有这个cost数据
            if (message["cost"] != nil && lastDic["cost"] != nil) {
                //两个都是守护
                //要加入的开通守护跑道信息,比最后一个跑道信息花费小,不显示
                let nowCost = NSString.init(string: "\(message["cost"] ?? "0")").integerValue
                let lastCost = NSString.init(string: "\(lastDic["cost"] ?? "0")").integerValue
                if( nowCost <= lastCost){
                    //两个守护,并且这次的守护金额不大于之前的守护金额,放弃掉价格跑道信息
                    return
                }
                
            }else if (lastDic["cost"] != nil && message["info"] != nil && message["info"] is NSDictionary &&  (message["info"] as! NSDictionary)["cost"] != nil){
                //前一个守护,本次礼物
                let nowCost = NSString.init(string: "\((message["info"] as! NSDictionary)["cost"] ?? "0")").integerValue
                let lastCost = NSString.init(string: "\(lastDic["cost"] ?? "0")").integerValue
                if( nowCost <= lastCost ){
                    return
                }
            }else if(lastDic["info"] != nil && lastDic["info"] is NSDictionary && (lastDic["info"] as! NSDictionary)["cost"] != nil && message["cost"] != nil){
                //前一个礼物,本次守护
                let lastCost = NSString.init(string: "\((lastDic["info"] as! NSDictionary)["cost"] ?? "0")").integerValue
                let nowCost = NSString.init(string: "\(message["cost"] ?? "0")").integerValue
                if( nowCost <= lastCost ){
                    return
                }
            }else if(lastDic["info"] != nil && lastDic["info"] is NSDictionary && (lastDic["info"] as! NSDictionary)["cost"] != nil && message["info"] != nil && message["info"] is NSDictionary &&  (message["info"] as! NSDictionary)["cost"] != nil){
                //前一个礼物,本次礼物
                let lastCost = NSString.init(string: "\((lastDic["info"] as! NSDictionary)["cost"] ?? "0")").integerValue
                let nowCost = NSString.init(string: "\((message["info"] as! NSDictionary)["cost"] ?? "0")").integerValue
                if( nowCost <= lastCost ){
                    return
                }
            }
        }
        
        
        //新的要显示跑道数值比之前的大,小的话不加入,顶掉了不显示,
        //之前数值小的跑道也被本次大的顶掉
        self.messageArr.add(message)
        
        //判断是否立即显示本次的跑道
        //判断:手动、自动
        //再判断:根本没有动画中、在滑行显示中、显示文字滚动中、在滑行隐藏中
        if (self.autoType == RocketAutoType.RocketTypeAuto) {
            if (self.status == RocketStatus.RocketCanStartNewAnimate) {
                //根本没有动画中
                self.showWith(infoDic: message)
            }else if(self.status == RocketStatus.RocketWillShowText){
                //在滑行显示中,设置到达显示文字滚动时间为0后直接滑行隐藏
                self.nowShowTime = 0
            }else if(self.status == RocketStatus.RockeShowingText){
                //显示文字滚动中
                if (self.showTextTimer != nil){
                    self.showTextTimer!.invalidate()
                    self.showTextTimer = nil
                }
                self.hideWithBlock()
            }else if(self.status == RocketStatus.RockeShowTextOver){
                //在滑行隐藏中,不用管,自动开始下个跑道动画
            }
        }else if (self.autoType == RocketAutoType.RocketTypeTap){
            //设置为自动模式
            self.autoType = RocketAutoType.RocketTypeAuto
            if (self.status == RocketStatus.RocketCanStartNewAnimate) {
                //根本没有动画中
                self.showWith(infoDic: message)
            }else if(self.status == RocketStatus.RocketWillShowText){
                //在滑行显示中,设置到达显示文字滚动时间为0后直接滑行隐藏
                self.nowShowTime = 0
            }else if(self.status == RocketStatus.RockeShowingText){
                //显示文字滚动中
                if (self.showTextTimer != nil){
                    self.showTextTimer!.invalidate()
                    self.showTextTimer = nil
                }
                self.hideWithBlock()
            }else if(self.status == RocketStatus.RockeShowTextOver){
                //在滑行隐藏中,不用管,自动开始下个跑道动画
            }
        }
        
    }
    @objc func deleteConfig() -> Void {
        if (self.showTextTimer != nil){
            self.showTextTimer!.invalidate()
            self.showTextTimer = nil
        }
        self.isHidden = true
        self.removeFromSuperview()
    }
    
    @objc func changeOrientation() -> Void {
        //横竖屏切换时
        //根据当前动画状态改变,无需考虑手动自动类型
        
        var originY:CGFloat = 0
        if self.SendShowType == 1 {
            //观众端
            if (SYBScreenWidth() > SYBScreenHeight()) {
                originY =  CGFloat(25)+CGFloat(35)+CGFloat(12.5)
            }else{
                originY = (CGFloat(25)+CGFloat(35)+CGFloat(12.5)+CGFloat(SNvBarYMargen()))
            }
        }else if(self.SendShowType == 2){
            //主播端
            originY = CGFloat(5) + CGFloat(X_SafeAreaStatusBarHeight())+CGFloat(35)+CGFloat(7)
        }
        
        if (self.GoBtn.frame.size.width > CGFloat(20)){
            //世界礼物y值有偏移
            originY -= CGFloat(worldGiftOffsetY)
        }else{
            //守护\普通礼物
            originY -= CGFloat(guardOrNormalGiftOffsetY)
        }
        
        
        if (self.status == RocketStatus.RocketCanStartNewAnimate) {
            //根本没有动画中
            self.isHidden = true
            self.frame = CGRect.init(x: SYBScreenWidth() - 10 - self.frame.size.width, y: originY, width: self.frame.size.width, height: self.frame.size.height)
            self.bgV.frame = CGRect.init(x: self.bgV.frame.size.width, y: self.bgV.frame.origin.y, width: self.bgV.frame.size.width, height: self.bgV.frame.size.height)
        }else if(self.status == RocketStatus.RocketWillShowText){
            //在滑行显示中,
            if (self.showTextTimer != nil){
                self.showTextTimer!.invalidate()
                self.showTextTimer = nil
            }
            self.isHidden = true
            self.frame = CGRect.init(x: SYBScreenWidth() - 10 - self.frame.size.width, y: originY, width: self.frame.size.width, height: self.frame.size.height)
            self.bgV.frame = CGRect.init(x: self.bgV.frame.size.width, y: self.bgV.frame.origin.y, width: self.bgV.frame.size.width, height: self.bgV.frame.size.height)
            
            //重新开始本次动画
            self.showWith(infoDic: self.dic!)
        }else if(self.status == RocketStatus.RockeShowingText){
            //显示文字滚动中
            self.frame = CGRect.init(x: SYBScreenWidth() - 10 - self.frame.size.width, y: originY, width: self.frame.size.width, height: self.frame.size.height)
            self.bgV.frame = CGRect.init(x: 0, y: self.bgV.frame.origin.y, width: self.bgV.frame.size.width, height: self.bgV.frame.size.height)
        }else if(self.status == RocketStatus.RockeShowTextOver){
            //在滑行隐藏中,不用管,自动开始下个跑道动画
            self.isHidden = true
            self.frame = CGRect.init(x: SYBScreenWidth() - 10 - self.frame.size.width, y: originY, width: self.frame.size.width, height: self.frame.size.height)
            self.bgV.frame = CGRect.init(x: self.bgV.frame.size.width, y: self.bgV.frame.origin.y, width: self.bgV.frame.size.width, height: self.bgV.frame.size.height)
        }
        
    }
    
    deinit {
        NSLog("AutoRocket deinit")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}








逻辑:送礼自动进场跑道手势不相应隐藏事件,倒计时结束收回;手动跑道相应隐藏手势;此外更竖屏切换、手机点击重现跑动动画都要考虑当前跑道的状态位置等。当前做的是横竖屏时,如果跑道动画未完成,隐藏视图以及后续操作,取消本次动画,等待横竖屏切换完成,会重现新位置的跑道动画。

 

 

更多问题加群讨论:

565191947

 

 

 

你可能感兴趣的:(app苹果iOS类)