• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift版的CircleView

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

swift版的CircleView

 

效果图

 

源码

//
//  CircleView.swift
//  CircleView
//
//  Created by YouXianMing on 15/10/7.
//  Copyright © 2015年 YouXianMing. All rights reserved.
//

import UIKit

class CircleView: UIView {
    
    // MARK: - 变量
    
    var lineWidth  : CGFloat        = 1
    var lineColor  : UIColor        = UIColor.blackColor()
    var clockWise  : Bool           = false
    var startAngle : CGFloat        = 0
    var duration   : NSTimeInterval = 0.2
    
    private var circleLayer : CAShapeLayer!
    
    // MARK: - Public Method
    
    /**
    构建view,让参数生效
    */
    func buildView() {
        
        let size   = bounds.size
        let point  = CGPoint(x: size.height / 2, y: size.width / 2)
        let radius = size.width / 2 - lineWidth / 2
        
        var tmpStartAngle : CGFloat = 0
        var tmpEndAngle   : CGFloat = 0
        
        if (clockWise == true) {
            
            tmpStartAngle = -radian(Double(180 - startAngle));
            tmpEndAngle   = radian(Double(180 + self.startAngle));
            
        } else {
            
            tmpStartAngle = radian(Double(180 - self.startAngle));
            tmpEndAngle   = -radian(Double(180 + self.startAngle));
        }
        
        let circlePath = UIBezierPath(arcCenter: point, radius: radius, startAngle: tmpStartAngle, endAngle: tmpEndAngle, clockwise: clockWise)
        
        circleLayer.path        = circlePath.CGPath
        circleLayer.strokeColor = lineColor.CGColor
        circleLayer.fillColor   = UIColor.clearColor().CGColor
        circleLayer.lineWidth   = lineWidth
        circleLayer.strokeEnd   = 0
    }
    
    /**
    绘制圆形百分比
    
    - parameter percent:  百分比
    - parameter animated: 是否开启动画
    */
    func changeToPercent(var percent : CGFloat, animated : Bool) {
        
        if (percent <= 0) {
            
            percent = 0;
            
        } else if (percent >= 1) {
            
            percent = 1;
        }
        
        if (animated) {
            
            let basicAnimation : CABasicAnimation! = CABasicAnimation()
            basicAnimation.keyPath                 = "strokeEnd"
            basicAnimation.duration                = (duration <= 0 ? 0.2 : duration)
            basicAnimation.fromValue               = circleLayer.strokeEnd
            basicAnimation.toValue                 = percent
            circleLayer.strokeEnd                  = percent
            circleLayer.addAnimation(basicAnimation, forKey: nil)
            
        } else {
            
            CATransaction.setDisableActions(true)
            circleLayer.strokeEnd = percent
            CATransaction.setDisableActions(false)
        }
    }
    
    // MARK: - System Method
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        createCircleLayer()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - Private Method
    
    private func radian(degrees : Double) -> CGFloat {
        
        return CGFloat((M_PI * degrees) / 180)
    }
    
    private func createCircleLayer() {
        
        circleLayer       = CAShapeLayer()
        circleLayer.frame = self.bounds
        self.layer.addSublayer(circleLayer)
    }
}
//
//  ViewController.swift
//  CircleView
//
//  Created by YouXianMing on 15/10/7.
//  Copyright © 2015年 YouXianMing. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var eventTimer : NSTimer!
    var circleView : CircleView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
     
        eventTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "timerEvent", userInfo: nil, repeats: true)
        
        circleView            = CircleView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        circleView.lineWidth  = 1
        circleView.lineColor  = UIColor.blackColor()
        circleView.duration   = 0.25
        circleView.clockWise  = true
        circleView.startAngle = 90
        circleView.center     = view.center
        circleView.buildView()
        
        view.addSubview(circleView)
    }
    
    func timerEvent() {
        
        circleView.changeToPercent(CGFloat(arc4random() % 101) / 100, animated: true)
    }
}

 

说明

参数查看并没有OC那么直白.

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap