Swift 圆形渐变进度条 支持事件 支持XIB和StoryBoard

最近项目用到了 一个经验条 要求颜色渐变 中间是用户头像 还要接受事件 然后自己写了个下面这个控件

//
//  XProgressView.swift
//  XProgressView
//
//  Created by eduo_xiaoP on 15/4/11.
//  copyright (c) 2015年 eduo. All rights reserved.
//

import Foundation
import UIKit

@objc protocol XCirleProgressViewDelegate: NSObjectProtocol {
    optional func progresstapped(progressView: XCirleProgressView!)
}

@IBDesignable class XCirleProgressView: UIView {
    
    struct Constant {
        static let linewidth: CGFloat = 10
        static let trackColor = UIColor(red: 245/255.0,green: 245/255.0,blue: 245/255.0,alpha: 1)
        static let endColor = UIColor.redColor().colorWithAlphaComponent(0.8)
        static let startColor = UIColor.greenColor().colorWithAlphaComponent(0.2)
    }
    
    let gradientLayer = CAGradientLayer()
    let trackLayer = CAShapeLayer()
    let progressLayer = CAShapeLayer()
    let path = UIBezierPath()
    
    @IBInspectable var progress: Int = 0
    
    @IBInspectable var image: UIImage? {
        didSet{
            setNeedsdisplay()
        }
    }
    
    weak var delegate: XCirleProgressViewDelegate?
    
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        initialization()
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initialization()
    }
    
    override func layoutSubviews() {
        path.movetoPoint(CGPointMake(CGRectGetMidX(bounds) + Constant.linewidth/2.0,Constant.linewidth))
        path
        path.addArcWithCenter(CGPointMake(CGRectGetMidX(bounds),CGRectGetMidY(bounds)),radius: bounds.size.width/2 - Constant.linewidth,startAngle: angletoRadian(-90),endAngle: angletoRadian(270),clockwise: true)
        
        trackLayer.frame = bounds
        trackLayer.path = path.CGPath
        
        progressLayer.frame = bounds
        progressLayer.path = path.CGPath
        
        gradientLayer.frame = bounds
    }
    
    func initialization() {
        trackLayer.fillColor = UIColor.clearColor().CGColor
        trackLayer.strokeColor = Constant.trackColor.CGColor
        trackLayer.linewidth = Constant.linewidth
        trackLayer.lineCap = kCALineCapRound
        layer.addSublayer(trackLayer)
        
        progressLayer.fillColor = UIColor.clearColor().CGColor
        progressLayer.strokeColor = UIColor.orangeColor().CGColor
        progressLayer.linewidth = Constant.linewidth
        progressLayer.lineCap = kCALineCapSquare
        progressLayer.strokeEnd = 0
        
        gradientLayer.colors = [Constant.startColor.CGColor,Constant.endColor.CGColor]
        layer.addSublayer(gradientLayer)
    }
    
    override func drawRect(rect: CGRect) {
        progressLayer.strokeEnd = CGFloat(progress)/200.0//好像是由于layoutsubviews调用2此导致的画出来长度是2倍  待解决
        gradientLayer.mask = progressLayer
        
        path.addClip()
        image?.drawInRect(bounds)
    }
    
    func setProgress(pro: Int,animated anim: Bool) {
        setProgress(pro,animated: anim,withDuration: 0.55)
    }
    
    func setProgress(pro: Int,animated anim: Bool,withDuration duration: Double) {
        progress = pro
        CATransaction.begin()
        CATransaction.setdisableActions(!anim)
        CATransaction.setAnimationTimingFunction(camediatimingFunction(name: kcamediatimingFunctionEaseInEaSEOut))
        CATransaction.setAnimationDuration(duration)
        progressLayer.strokeEnd = CGFloat(progress)/200.0//好像是由于layoutsubviews调用2此导致的画出来长度是2倍  待解决
        CATransaction.commit()
    }
    
    private func angletoRadian(angle: Double)->CGFloat {
        return CGFloat(angle/Double(180.0) * M_PI)
    }
    
    override func touchesBegan(touches: Set<NSObject>,withEvent event: UIEvent) {
        
        if let dele = delegate where dele.respondsToSelector("progresstapped:") {
            dele.progresstapped!(self)
        }
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...