swift-05

作者: 竹尖 | 来源:发表于2017-09-05 16:29 被阅读0次

    PageView  bug修正  1.连续滑动上标不正确  2.滑动到边缘判断

    // 2.定义需要的参数

    var progress : CGFloat = 0

    var targetIndex = 0

    var sourceIndex = 0

    // 3.计算进度

    progress = scrollView.contentOffset.x.truncatingRemainder(dividingBy: scrollView.bounds.width) / scrollView.bounds.width

    if progress == 0 {

    return

    }

    // 4.计算下标值

    let index = Int(scrollView.contentOffset.x / scrollView.bounds.width)

    // 5.判断用户是左滑动还是右滑动

    if collectionView.contentOffset.x > startOffsetX { // 左滑动

    sourceIndex = index

    targetIndex = index + 1

    if targetIndex > childVcs.count - 1 {

    return

    }

    } else { // 右滑动

    sourceIndex = index + 1

    targetIndex = index

    progress = 1 - progress

    if targetIndex < 0 {

    return}

    2.Socket 客户端  服务端 第三方 ysocket

    2.1客户端

    -1.根据ip 和 端口号创建客户端

    fileprivate lazy var tcpClient : TCPClient = TCPClient(addr: "192.168.37.247",port :2828)

    0建立连接

    func connnect() -> Bool {

    return tcpClient.connect(timeout: 5).0

    }

    1.断开连接

    func disconnect() {

    tcpClient.close()

    }

    数据的读取

    2.开始读数据: 1.数据内容长度(和服务器沟通好用几个字节4)  +  数据类型长度(和服务器沟通好用几个字节2--根据长度获取类型数据) + 具体内容数据(根据数据内容长度获取)

    2.1 处理具体内容数据

    func startReadMsg() {

    DispatchQueue.global().async {

    while true {

    if let lengthByte = self.tcpClient.read(4) {

    // 1.获取数据的长度

    let lengthData = NSData(bytes: lengthByte, length: 4)

    var length : Int = 0

    lengthData.getBytes(&length, length: 4)

    // 2.读取消息的类型

    guard let typeBytes = self.tcpClient.read(2) else {

    self.tcpClient.read(length)

    continue

    }

    let typeData = NSData(bytes: typeBytes, length: 2)

    var type : Int = 0

    typeData.getBytes(&type, length: 2)

    // 3.获取具体的内容

    guard let dataBytes = self.tcpClient.read(length) else {

    return

    }

    let msgData = Data(bytes: dataBytes, count: length)

    // 4.处理消息

    DispatchQueue.main.async {

    self.handleMsg(MessageType(rawValue: type)!, msgData)

    }

    3.处理消息: 根据不同消息类型定义一个枚举--相应处理---用代理回调到控制器做相应的ui处理

    *这里的消息类型是自己定义的方便处理相应的ui刷新---这个类型和服务器约定好  int   1   2   3   4.. 也就是数据类型

    enum MessageType : Int {

    case enterRoom = 0

    case leaveRoom = 1

    case chatMessage = 2

    case giftMessage = 3

    }

    extension HYSocket {

    fileprivate func handleMsg(_ type : MessageType, _ msgData : Data) {

    switch type {

    case .enterRoom:

     let userInfo = try! UserInfo.parseFrom(data: msgData)

    delegate?.hySocket(self, enterRoom: userInfo)

    case .leaveRoom: 

    let userInfo = try! UserInfo.parseFrom(data: msgData)

    delegate?.hySocket(self, leaveRoom: userInfo)

    case .chatMessage:

    let chatMessage = try! ChatMessage.parseFrom(data: msgData)

    delegate?.hySocket(self, receiveMsg: chatMessage)

    case .giftMessage:

    let giftMessage = try! GiftMessage.parseFrom(data: msgData)

    delegate?.hySocket(self, receiveGift: giftMessage)

    数据发送 : 最终是 : sendData = 内容长度data + 内容类型data +  内容data

    func enterRoom() {

    sendMessage(type: .enterRoom, msgData: userInfo.data())

    }

    func leaveRoom() {

    sendMessage(type: .leaveRoom, msgData: userInfo.data())

    }

    func sendChatMessage(_ messsage : String) {

    // 1.获取聊天的数据

    let chatMsg = ChatMessage.Builder()

    chatMsg.userInfo = userInfo

    chatMsg.message = messsage

    let msgData = (try! chatMsg.build()).data()

    // 2.发送数据

    sendMessage(type: .chatMessage, msgData: msgData)

    }

    func sendGift(_ giftName : String, _ giftURL : String, _ giftID : String, _ giftCount : Int) {

    // 1.获取礼物的数据

    let giftMsg = GiftMessage.Builder()

    giftMsg.userInfo = userInfo

    giftMsg.giftName = giftName

    giftMsg.giftUrl = giftURL

    giftMsg.giftId = giftID

    giftMsg.giftCount = Int32(giftCount)

    let msgData = (try! giftMsg.build()).data()

    // 2.发送数据

    sendMessage(type: .giftMessage, msgData: msgData)

    }

    fileprivate func sendMessage(type : MessageType, msgData : Data) {

    // 1.获取字符串的长度

    var length = msgData.count

    let lengthData = Data(bytes: &length, count: 4)

    // 2.获取消息的类型

    var tempType = type.rawValue

    let typeData = Data(bytes: &tempType, count: 2)

    // 3.将message转成Data类型

    tcpClient.send(data: lengthData + typeData + msgData)

    }

    4.ProtocolBuffers的使用

    1.为什么用这个,怎么用--------用这个和后台沟通方便以对象的形式可以展示,一般是服务器写完这个文件 --我们再生成一个相应的swift或者oc文件,

    返回值中有 throw  必须得 try!

    let msgData = (try! giftMsg.build()).data()

    优点:可以直接转成data

    syntax = "proto2";

    message UserInfo {

    required int32 userID = 1;

    required string username = 2;

    required string iconURL = 3;

    required int32 level = 4;

    }

    message ChatMessage {

    required string message = 1;

    required UserInfo userInfo = 2;

    }

    message GiftMessage {

    required string giftName = 1;

    required string giftURL = 2;

    required string giftID = 3;

    required int32 giftCount = 4;

    required UserInfo userInfo = 5;

    }

    2.cocopods直接集成

    3.上两部做完就可直接取  文件中相应的对象使用例如

    // 1.获取礼物的数据

    let giftMsg = GiftMessage.Builder()

    giftMsg.userInfo = userInfo

    giftMsg.giftName = giftName

    giftMsg.giftUrl = giftURL

    giftMsg.giftId = giftID

    giftMsg.giftCount = Int32(giftCount)

    let msgData = (try! giftMsg.build()).data()

    // 2.发送数据

    sendMessage(type: .giftMessage, msgData: msgData)

    }

    相关文章

      网友评论

          本文标题:swift-05

          本文链接:https://www.haomeiwen.com/subject/jrlujxtx.html