美文网首页
Swift 功能实现之滑杆

Swift 功能实现之滑杆

作者: 岁变 | 来源:发表于2018-02-07 09:17 被阅读62次

  今天要搞一个日期滑杆,滑杆上显示当前日期的前15天和后15天。支持范围选取。项目中用到,就写了一下,这里记录一下,写个小demo。

首先,功能动态图:

实现

这东西也没有什么可拓展写的,所以可以直接上代码了。

  • 首先处理处理几个工具类:DateTool
import UIKit

class DateTool: NSObject {
    
    //当前时间
    class func getMidTimeText(_ dateCompentCount: Int) -> String {
        let currentDate = getTimeDateWith(dateCompentCount)
        return dateTurnString(date: currentDate)
    }
    
    //左侧时间
    class func getLeftTimeText(_ dateCompentCount: Int) -> String {
        let leftDate: Date = getTimeDateWith(dateCompentCount)
        return dateTurnString(date: leftDate)
    }
    
    //右侧时间
    class func getRightTimeText(_ dateCompentCount: Int) -> String {
        let rightDate: Date = getTimeDateWith(dateCompentCount)
        return dateTurnString(date: rightDate)
    }
    
    
    
    //data转时间
    class func dateTurnString(date: Date) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = "MM.dd"
        let dateStr = formatter.string(from:date)
        let returnStr = cleanString(dateStr: dateStr)
        return returnStr
    }
    
    //获取距离当前时间指定天数的时间
    class func getTimeDateWith(_ dateCompentCount: Int) -> Date {
        let currentDate = Date()
        var newcomponent = DateComponents()
        newcomponent.day = dateCompentCount
        let date = Calendar.current.date(byAdding: newcomponent, to: currentDate) ?? currentDate
        return date
    }

    
    //时间字符串清零
    class func cleanString(dateStr: String) -> String {
        let strArr = dateStr.components(separatedBy: ".")
        let monthStr: String = strArr.first ?? "0"
        let dayStr: String = strArr.last ?? "0"
        let monCount: Int = Int(monthStr) ?? 0
        let dayCount: Int = Int(dayStr) ?? 0
        let newMonStr = String(monCount)
        let newDayStr = String(dayCount)
        let returnStr = newMonStr + "." + newDayStr
        return returnStr
    }   
}
  • label扩展
import UIKit

extension UILabel {
    
    func setUpLabel(frame: CGRect, text: String, textColor: UIColor, textFont: CGFloat) {
        self.frame = frame
        self.text = text
        self.textColor = textColor
        self.font = UIFont.systemFont(ofSize: textFont)
        self.textAlignment = .center
    }
    
}
  • 工具创建完事,实现controller: DateSliderViewController
import UIKit

let WIDTH: CGFloat = UIScreen.main.bounds.width
let SLIDEW: CGFloat = 14
let SLIDEH: CGFloat = 20

let PRICEBGX: CGFloat = 25
let PRICEBGY: CGFloat = 200
let PRICEBGW: CGFloat = WIDTH - 50
let PRICEBGH: CGFloat = 20
let PRICEMAX: CGFloat = WIDTH * 0.5 + (PRICEBGW * 0.5 - SLIDEW * 0.5 - 5)
let PRICEMIN: CGFloat = WIDTH * 0.5 - (PRICEBGW * 0.5 - SLIDEW * 0.5 - 5)
let RECTWIDTH: CGFloat = PRICEMAX - PRICEMIN
let PROGRESSH: CGFloat = 2
let PROGRESSY: CGFloat = PRICEBGY + (PRICEBGH - 2) / 2




class DateSliderViewController: UIViewController {
    
    
    let leftSlide = UIImageView()
    let rigthSlide = UIImageView()
    
    let leftValue = UILabel()
    let rigthValue = UILabel()
    let midValue = UILabel()
    
    let progressBackView = UIView()
    
    let leftResultLabel = UILabel()
    let rightResultLabel = UILabel()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let imageView = UIImageView.init(frame: CGRect(x: PRICEBGX, y: PRICEBGY, width: PRICEBGW, height: PRICEBGH))
        imageView.image = UIImage.init(named: "WechatIMG3492")
        self.view.addSubview(imageView)
        
        let leftRect = CGRect(x: PRICEBGX , y: imageView.frame.maxY + 2, width: 30, height: 15)
        let leftText = DateTool.getLeftTimeText(-15)
        leftValue.setUpLabel(frame: leftRect, text: leftText, textColor: UIColor.gray, textFont: 10)
        self.view.addSubview(leftValue)
        
        let rightRect = CGRect(x: imageView.frame.maxX - 20, y: imageView.frame.maxY + 2, width: 30, height: 15)
        let rightText = DateTool.getRightTimeText(15)
        rigthValue.setUpLabel(frame: rightRect, text: rightText, textColor: UIColor.gray, textFont: 10)
        self.view.addSubview(rigthValue)
        
        let midRect = CGRect(x: imageView.frame.midX - 20, y: imageView.frame.maxY + 2, width: 40, height: 16)
        let midText = DateTool.getMidTimeText(0)
        midValue.setUpLabel(frame: midRect, text: midText, textColor: UIColor.black, textFont: 12)
        self.view.addSubview(midValue)
        
        
        //左滑块
        leftSlide.frame = CGRect(x: imageView.center.x - SLIDEW * 0.5 , y: imageView.frame.minY - SLIDEH, width: SLIDEW, height: SLIDEH)
        leftSlide.image = UIImage.init(named: "shangbashou")
        self.view.addSubview(leftSlide)
        
        //右滑块
        rigthSlide.frame = CGRect(x: imageView.center.x - SLIDEW * 0.5, y: imageView.frame.maxY, width: SLIDEW, height: SLIDEH)
        rigthSlide.image = UIImage.init(named: "xiabashou")
        self.view.addSubview(rigthSlide)
        
        //进度条颜色
        progressBackView.frame = CGRect(x: leftSlide.center.x, y: PROGRESSY, width: rigthSlide.center.x - leftSlide.center.x, height: PROGRESSH)
        progressBackView.backgroundColor = UIColor.gray
        self.view.addSubview(progressBackView)

        
        //添加滑动手势
        let leftPanRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(leftSlideMove(pan:)))
        leftPanRecognizer.maximumNumberOfTouches = 1
        leftPanRecognizer.minimumNumberOfTouches = 1
        leftSlide.isUserInteractionEnabled = true
        leftSlide.addGestureRecognizer(leftPanRecognizer)
        
        let rightPanRecognizer = UIPanGestureRecognizer.init(target: self, action: #selector(rightSlideMove(pan:)))
        rightPanRecognizer.maximumNumberOfTouches = 1
        rightPanRecognizer.minimumNumberOfTouches = 1
        rigthSlide.isUserInteractionEnabled = true
        rigthSlide.addGestureRecognizer(rightPanRecognizer)
        
        leftResultLabel.frame = CGRect(x: 20, y: 400, width: 100, height: 50)
        leftResultLabel.text = DateTool.getMidTimeText(0)
        
        rightResultLabel.frame = CGRect(x: WIDTH - 120, y: 400, width: 100, height: 50)
        rightResultLabel.text = DateTool.getMidTimeText(0)
        self.view.addSubview(leftResultLabel)
        self.view.addSubview(rightResultLabel)
    }
    
    
    @objc func leftSlideMove(pan: UIPanGestureRecognizer) {
        let point = pan.translation(in: leftSlide)
        var x: CGFloat = leftSlide.center.x + point.x
        if x > rigthSlide.center.x {
            x = rigthSlide.center.x
        } else if(x < PRICEMIN) {
            x = PRICEMIN
        }
        leftSlide.center = CGPoint(x: x, y: leftSlide.center.y)
        pan.setTranslation(.zero, in: self.view)
        updataData()
    }
    
    
    @objc func rightSlideMove(pan: UIPanGestureRecognizer) {
        let point = pan.translation(in: rigthSlide)
        var x: CGFloat = rigthSlide.center.x + point.x
        if x > PRICEMAX {
            x = PRICEMAX
        } else if(x < leftSlide.center.x) {
            x = leftSlide.center.x
        }
        rigthSlide.center = CGPoint(x: x, y: rigthSlide.center.y)
        pan.setTranslation(.zero, in: self.view)
        updataData()
    }
    
    func updataData() {
        let progressBackViewRect = CGRect(x: leftSlide.center.x, y: PROGRESSY, width: rigthSlide.center.x - leftSlide.center.x, height: PROGRESSH)
        progressBackView.frame = progressBackViewRect
        
        let leftText = coordinateTurnToNumber(axis: leftSlide.center.x)
        leftResultLabel.text = leftText
        
        let rigthText = coordinateTurnToNumber(axis: rigthSlide.center.x)
        rightResultLabel.text = rigthText
        
    }
    
    
    //坐标转换成数字
    func coordinateTurnToNumber(axis: CGFloat) -> String {
        //将滑条分成30份,取每份的长度
        let everyLenght = RECTWIDTH / 30.0
        //滑动的距离
        let lenght = axis - PRICEMIN
        let count = Int(lenght / everyLenght)
        
        //左面的日期date
        let minDate = DateTool.getTimeDateWith(-15)
        //滑动位置的日期
        var newComponent = DateComponents()
        newComponent.day = count
        let moveDate = Calendar.current.date(byAdding: newComponent, to: minDate) ?? Date()
        
        let returnstr = DateTool.cleanString(dateStr: DateTool.dateTurnString(date: moveDate))
        return returnstr
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
 
}

最后

ok 最后附上小demo链接:https://github.com/Sufviay/DateSlide,供参考。

相关文章

  • Swift 功能实现之滑杆

      今天要搞一个日期滑杆,滑杆上显示当前日期的前15天和后15天。支持范围选取。项目中用到,就写了一下,这里记录一...

  • swift 实现 LeetCode

    swift实现链表 swift实现队列功能 swift实现栈的功能 判断给定的一组数是否是回文结构。如:1、2、3...

  • iOS UISlider自定义渐变色滑杆

    前言 本文主要介绍继承实现自定义渐变色滑杆,空心滑杆 GitHub地址:KJExtensionHandler[ht...

  • 2019-12-03

    Swift使用UIActivityViewController实现分享文件功能

  • Swift学习:构造器(上)

    Swift之构造器(上)Swift之构造器(中)Swift之构造器(下)Swift赋予自定义类型更加丰富的功能,一...

  • Swift 功能实现之图表Charts

    前言   最近在项目中需要添加图表,柱状图和折线图,研究了一下,今天就给大家分享一个功能非常全,非常好用的图表制作...

  • 怎么给Slider添加刻度尺

    由于项目需求需要在一个滑杆,而且要求在滑杆上有刻度的显示。首先想到的是用UISlider来实现。 由于UISlid...

  • Android自定义滑杆控件SeekBar多功能版本

    在应用开发中有没有遇到过通过滑杆控件选择一些区间条件实现参数变化?今天我们就来自定义View实现一个多功能又实用的...

  • 基于swift4.0实现视频播放、屏幕旋转、倍速播放、手势调节,

    基于swift4.0实现视频播放、屏幕旋转、倍速播放、手势调节,锁屏面板等功能 基于swift4.0实现视频播放、...

  • Swift-面向协议编程

    Swift协议里的方法可以实现 OC只能声明方法所以Swift才能实现面向协议编程 需要这个功能的,只需要遵守这...

网友评论

      本文标题:Swift 功能实现之滑杆

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