码迷,mamicode.com
首页 > 编程语言 > 详细

swift版的CircleView

时间:2015-10-07 21:34:43      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

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那么直白.

 

swift版的CircleView

标签:

原文地址:http://www.cnblogs.com/YouXianMing/p/4859518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!