Swift 圆形渐变进度条 支持事件 支持XIB和StoryBoard
来源:互联网 发布:外贸b2b推广软件 编辑:程序博客网 时间:2024/05/17 23:30
最近项目用到了 一个经验条 要求颜色渐变 中间是用户头像 还要接受事件 然后自己写了个下面这个控件
//// XProgressView.swift// XProgressView//// Created by eduo_xiaoP on 15/4/11.// Copyright (c) 2015年 eduo. All rights reserved.//import Foundationimport 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) } }}
0 1
- Swift 圆形渐变进度条 支持事件 支持XIB和StoryBoard
- Swift 画线控件(支持xib和storyboard)
- Storyboard和Xib混用黑魔法-swift版
- swift 代码加载xib storyboard
- Android自定义圆形渐变进度条
- 【前端插件】渐变圆形进度条
- storyboard 和 xib
- Xib和storyboard对比
- Xib和storyboard对比
- Xib和storyboard对比
- Xib和storyboard对比
- Xib和storyboard对比
- ios-xib和storyboard
- 渐变色圆形流量球,渐变色圆形进度条
- Swift下自定义xib添加到Storyboard
- 自定义View之渐变色圆形进度条
- Android之自定义圆形渐变、条形进度条
- Xib和storyboard的比较
- C++内存分配方式 ——堆、栈、自由存储区、全局/静态存储区和常量存储区
- cocos2dx 3D战斗类游戏制作:【四】——一些零散笔记,3D小地图,android surfaceview等
- UVa 1586
- [Phonegap+Sencha Touch] 移动开发48 扩展的NavigationView控件,支持根据不同的View显示不同的顶栏按钮、不同的屏幕方向,增加视图缓存机制
- Windows多线程学习(二)多线程互斥同步 关键段解决互斥问题
- Swift 圆形渐变进度条 支持事件 支持XIB和StoryBoard
- jquery: 使用选择器获得要操作的元素
- 手势密码
- Iterm2 快捷键指南
- Linux中fork()函数详解
- Mac 开发配置手册
- 盘点即时战略游戏中高实用性寻路算法
- 计算机视觉相关材料
- hdu 4888 Redraw Beautiful Drawings(Dinic最大流+判断有没有环)