Guest User

WCGraintCircleLayer swift

a guest
Apr 16th, 2016
1,382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.70 KB | None | 0 0
  1. // Created by Alessandro Ornano - free to use, no copyright.
  2. import UIKit
  3.  
  4. class WCGraintCircleLayer: CALayer {
  5.  
  6.     override init () {
  7.         super.init()
  8.     }
  9.    
  10.     convenience init(bounds:CGRect,position:CGPoint,fromColor:UIColor,toColor:UIColor,linewidth:CGFloat,toValue:CGFloat) {
  11.         self.init()
  12.         self.bounds = bounds
  13.         self.position = position
  14.         let colors : [UIColor] = self.graintFromColor(fromColor, toColor:toColor, count:4)
  15.         for i in 0..<colors.count-1 {
  16.             let graint = CAGradientLayer()
  17.             graint.bounds = CGRectMake(0,0,CGRectGetWidth(bounds)/2,CGRectGetHeight(bounds)/2)
  18.             let valuePoint = self.positionArrayWithMainBounds(self.bounds)[i]
  19.             graint.position = valuePoint
  20.             print("iesimo graint position: \(graint.position)")
  21.             let fromColor = colors[i]
  22.             let toColor = colors[i+1]
  23.             let colors : [CGColorRef] = [fromColor.CGColor,toColor.CGColor]
  24.             let stopOne: CGFloat = 0.0
  25.             let stopTwo: CGFloat = 1.0
  26.             let locations : [CGFloat] = [stopOne,stopTwo]
  27.             graint.colors = colors
  28.             graint.locations = locations
  29.             graint.startPoint = self.startPoints()[i]
  30.             graint.endPoint = self.endPoints()[i]
  31.             self.addSublayer(graint)
  32.             //Set mask
  33.             let shapelayer = CAShapeLayer()
  34.             let rect = CGRectMake(0,0,CGRectGetWidth(self.bounds) - 2 * linewidth, CGRectGetHeight(self.bounds) - 2 * linewidth)
  35.             shapelayer.bounds = rect
  36.             shapelayer.position = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2)
  37.             shapelayer.strokeColor = UIColor.blueColor().CGColor
  38.             shapelayer.fillColor = UIColor.clearColor().CGColor
  39.             shapelayer.path = UIBezierPath(roundedRect: rect, cornerRadius: CGRectGetWidth(rect)/2).CGPath
  40.             shapelayer.lineWidth = linewidth
  41.             shapelayer.lineCap = kCALineCapRound
  42.             shapelayer.strokeStart = 0.010
  43.             let finalValue = (toValue*0.99)
  44.             shapelayer.strokeEnd = finalValue//0.99;
  45.             self.mask = shapelayer
  46.         }
  47.     }
  48.    
  49.     required init?(coder aDecoder: NSCoder) {
  50.         fatalError("init(coder:) has not been implemented")
  51.     }
  52.    
  53.     func layerWithWithBounds(bounds:CGRect, position:CGPoint, fromColor:UIColor, toColor:UIColor, linewidth : CGFloat,toValue:CGFloat) -> WCGraintCircleLayer {
  54.         let layer = WCGraintCircleLayer(bounds: bounds,position: position,fromColor:fromColor, toColor: toColor,linewidth: linewidth,toValue:toValue )
  55.         return layer
  56.     }
  57.    
  58.     func graintFromColor(fromColor:UIColor, toColor:UIColor, count:Int) -> [UIColor]{
  59.         var fromR:CGFloat = 0.0,fromG:CGFloat = 0.0,fromB:CGFloat = 0.0,fromAlpha:CGFloat = 0.0
  60.         fromColor.getRed(&fromR,green: &fromG,blue: &fromB,alpha: &fromAlpha)
  61.  
  62.         var toR:CGFloat = 0.0,toG:CGFloat = 0.0,toB:CGFloat = 0.0,toAlpha:CGFloat = 0.0
  63.         toColor.getRed(&toR,green: &toG,blue: &toB,alpha: &toAlpha)
  64.  
  65.         var result : [UIColor]! = [UIColor]()
  66.        
  67.         for i in 0...count {
  68.             let oneR:CGFloat = fromR + (toR - fromR)/CGFloat(count) * CGFloat(i)
  69.             let oneG : CGFloat = fromG + (toG - fromG)/CGFloat(count) * CGFloat(i)
  70.             let oneB : CGFloat = fromB + (toB - fromB)/CGFloat(count) * CGFloat(i)
  71.             let oneAlpha : CGFloat = fromAlpha + (toAlpha - fromAlpha)/CGFloat(count) * CGFloat(i)
  72.             let oneColor = UIColor.init(red: oneR, green: oneG, blue: oneB, alpha: oneAlpha)
  73.             result.append(oneColor)
  74.             print(oneColor)
  75.            
  76.         }
  77.         return result
  78.     }
  79.    
  80.     func positionArrayWithMainBounds(bounds:CGRect) -> [CGPoint]{
  81.         let first = CGPointMake((CGRectGetWidth(bounds)/4)*3, (CGRectGetHeight(bounds)/4)*1)
  82.         let second = CGPointMake((CGRectGetWidth(bounds)/4)*3, (CGRectGetHeight(bounds)/4)*3)
  83.         let third = CGPointMake((CGRectGetWidth(bounds)/4)*1, (CGRectGetHeight(bounds)/4)*3)
  84.         let fourth = CGPointMake((CGRectGetWidth(bounds)/4)*1, (CGRectGetHeight(bounds)/4)*1)
  85.         print([first,second,third,fourth])
  86.         return [first,second,third,fourth]
  87.     }
  88.    
  89.     func startPoints() -> [CGPoint] {
  90.         return [CGPointMake(0,0),CGPointMake(1,0),CGPointMake(1,1),CGPointMake(0,1)]
  91.     }
  92.    
  93.     func endPoints() -> [CGPoint] {
  94.         return [CGPointMake(1,1),CGPointMake(0,1),CGPointMake(0,0),CGPointMake(1,0)]
  95.     }
  96.    
  97.     func midColorWithFromColor(fromColor:UIColor, toColor:UIColor, progress:CGFloat) -> UIColor {
  98.         var fromR:CGFloat = 0.0,fromG:CGFloat = 0.0,fromB:CGFloat = 0.0,fromAlpha:CGFloat = 0.0
  99.         fromColor.getRed(&fromR,green: &fromG,blue: &fromB,alpha: &fromAlpha)
  100.        
  101.         var toR:CGFloat = 0.0,toG:CGFloat = 0.0,toB:CGFloat = 0.0,toAlpha:CGFloat = 0.0
  102.         toColor.getRed(&toR,green: &toG,blue: &toB,alpha: &toAlpha)
  103.        
  104.         let oneR = fromR + (toR - fromR) * progress
  105.         let oneG = fromG + (toG - fromG) * progress
  106.         let oneB = fromB + (toB - fromB) * progress
  107.         let oneAlpha = fromAlpha + (toAlpha - fromAlpha) * progress
  108.         let oneColor = UIColor.init(red: oneR, green: oneG, blue: oneB, alpha: oneAlpha)
  109.         return oneColor
  110.     }
  111.    
  112.     // This is what you call if you want to draw a full circle.
  113.     func animateCircle(duration: NSTimeInterval) {
  114.         animateCircleTo(duration, fromValue: 0.010, toValue: 0.99)
  115.     }
  116.    
  117.     // This is what you call to draw a partial circle.
  118.     func animateCircleTo(duration: NSTimeInterval, fromValue: CGFloat, toValue: CGFloat){
  119.         // We want to animate the strokeEnd property of the circleLayer
  120.         let animation = CABasicAnimation(keyPath: "strokeEnd")
  121.         animation.removedOnCompletion = true
  122.         // Set the animation duration appropriately
  123.         animation.duration = duration
  124.        
  125.         // Animate from 0.010 (no circle) to 0.99 (full circle)
  126.         animation.fromValue = 0.010
  127.         animation.toValue = toValue
  128.        
  129.         // Do an easeout. Don't know how to do a spring instead
  130.         //animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
  131.         animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
  132.        
  133.         // Set the circleLayer's strokeEnd property to 0.99 now so that it's the
  134.         // right value when the animation ends.
  135.         let circleMask = self.mask as! CAShapeLayer
  136.         circleMask.strokeEnd = toValue
  137.        
  138.         // Do the actual animation
  139.         circleMask.removeAllAnimations()
  140.         circleMask.addAnimation(animation, forKey: "animateCircle")
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment