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


演示:




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

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



代码:

//
//  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){
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星宇大前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值