前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >swift 类似高德地图和58同城上下滑动view

swift 类似高德地图和58同城上下滑动view

作者头像
星宇大前端
发布2019-01-15 16:13:48
1.1K0
发布2019-01-15 16:13:48
举报
文章被收录于专栏:大宇笔记

演示:

代码地址: https://github.com/RainManGO/PanView

主要在Xib中使用:xib的view直接绑定panview即可。如图:

代码:

代码语言:javascript
复制
//
//  PanView.swift
//
//
//  Created by ZY on 2018/5/23.
//  Copyright © 2018年 zy. All rights reserved.
//

import UIKit

enum slideStates {
    case top
    case bottom
}

class PanView: UIView {
    
    var  panViewStates: slideStates = .bottom  //默认状态是在底部
    var upPointY = 0.0   //最高值
    var middleY = 0.0  //滑动view 中间判定值
    var bottionY = 0.0  //最低值
    var currentY = 0.0   //当前的Y值
    
    let maxtop = 80.0
    
    override func awakeFromNib() {
        super.awakeFromNib()
        countY()
        addPanRecoginer()
        addTapRecoginer()
    }
}


//计算数据值
extension PanView {
    func countY(){
        upPointY = maxtop   //最高值
        middleY = Double(((self.superview?.frame.height)! - CGFloat(maxtop))/2)  //滑动view 中间判定值
        bottionY = Double(self.frame.minY)  //最低值
        currentY = bottionY
    }
    
}

//手势
extension PanView {

    func addPanRecoginer(){
        let panRecoginer = UIPanGestureRecognizer.init(target: self, action: #selector(panHandle(pan:)))
        self.addGestureRecognizer(panRecoginer)
    }
    
    func addTapRecoginer(){
        let tapRecoginer = UITapGestureRecognizer.init(target: self, action: #selector(tapHandle(tap:)))
        self.addGestureRecognizer(tapRecoginer)
    }
    
}

//事件
extension PanView {
    
    @objc func panHandle(pan:UIPanGestureRecognizer){
        let  translation = pan.translation(in: self.superview)
        let  transY = Double(translation.y)
        
        if pan.state == .changed {
            if Double(self.frame.origin.y) < upPointY{
                currentY = upPointY
            }else if Double(self.frame.origin.y) > bottionY{
                currentY = bottionY
            }else{
                self.frame.origin.y = CGFloat(currentY + transY)
            }


        }else if pan.state == .ended {
            
            if (currentY + transY > upPointY) && (currentY + transY < middleY) { //如果当前位置在最上部和中线中间
                currentY = upPointY
            }else if (currentY + transY < bottionY) && (currentY + transY > middleY){
                currentY = bottionY
            }else if currentY + transY < upPointY{
                currentY = upPointY
            }else{
                currentY = bottionY
            }
            
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 10, initialSpringVelocity: 5, options: [], animations: {
                self.frame.origin.y = CGFloat(self.currentY)
            }) { (complete) in
                
            }
            
            currentY = Double(self.frame.origin.y)
        }
    
        
    }
    
    //待扩展tap
    @objc func tapHandle(tap:UITapGestureRecognizer){
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年05月31日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档