top of page

CAShapeLayer

// AnimatedVC.swift

// TipTax

//

// Created by Ru Ming Wu on 7/26/15.

// Copyright © 2015 Ru Ming Wu. All rights reserved.

//

import UIKit

class AnimatedVC: UIView {

var progressLayer: CAShapeLayer = CAShapeLayer()

override func layoutSubviews() {

self.backgroundColor = UIColor.clearColor()

progressLayer.lineWidth = 1

progressLayer.fillColor = UIColor.clearColor().CGColor

progressLayer.strokeColor = UIColor.whiteColor().CGColor

self.layer.addSublayer(progressLayer)

let radius:CGFloat = CGRectGetWidth(self.bounds)/2.0

let center = CGPointMake(radius, radius)

let startAngle = -M_PI_2

let endAngle = M_PI_2*3.0

let circlePath = UIBezierPath(arcCenter: center, radius: radius, startAngle: CGFloat(startAngle), endAngle: CGFloat(endAngle), clockwise: true)

progressLayer.path = circlePath.CGPath

}

func startDrawCircleAnimation(){

let pathAnimation:CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")

pathAnimation.fromValue = 0

pathAnimation.toValue = 1

pathAnimation.duration = 0.5

progressLayer.addAnimation(pathAnimation, forKey: "pathAnimation")

}

}

===================================

//

// CustomView.swift

// MyBook

//

// Created by Ru Ming Wu on 7/13/14.

// Copyright (c) 2014 Ru Ming Wu. All rights reserved.

//

import QuartzCore

import UIKit

@IBDesignable

class CustomView: UIView {

var backgroundRingLayer: CAShapeLayer!

var ringLayer:CAShapeLayer!

@IBInspectable var rating: Double = 0

@IBInspectable var lineWidth: Double = 0

override func layoutSubviews() {

super.layoutSubviews()

if !(backgroundRingLayer != nil) {

backgroundRingLayer = CAShapeLayer()

layer.addSublayer(backgroundRingLayer)

let rect = CGRectInset(bounds, CGFloat(lineWidth) / 2.0, CGFloat(lineWidth) / 2.0)

let path = UIBezierPath(ovalInRect: rect)

backgroundRingLayer.path = path.CGPath

backgroundRingLayer.fillColor = nil

backgroundRingLayer.lineWidth = CGFloat(lineWidth)

backgroundRingLayer.strokeColor = UIColor(white: 0.5, alpha: 0.05).CGColor

}

backgroundRingLayer.frame = layer.bounds

if !(ringLayer != nil) {

ringLayer = CAShapeLayer()

let innerRect = CGRectInset(bounds, CGFloat(lineWidth) / 2.0, CGFloat(lineWidth) / 2.0)

let innerPath = UIBezierPath(ovalInRect: innerRect)

ringLayer.path = innerPath.CGPath

ringLayer.fillColor = nil

ringLayer.lineWidth = CGFloat(lineWidth)

ringLayer.strokeColor = UIColor.blueColor().CGColor

ringLayer.anchorPoint = CGPointMake(0.5, 0.5)

ringLayer.transform = CATransform3DRotate(ringLayer.transform, CGFloat(-M_PI/2), 0, 0, 1)

layer.addSublayer(ringLayer)

}

ringLayer.frame = layer.bounds

updateLayerProperties()

}

func updateLayerProperties() {

if (ringLayer != nil) {

ringLayer.strokeEnd = CGFloat(rating)

var strokeColor = UIColor.lightGrayColor()

switch rating {

case let r where r >= 0.75:

strokeColor = UIColor(hue: 112.0/360.0, saturation: 0.39, brightness: 0.85, alpha: 1.0)

case let r where r >= 0.5:

strokeColor = UIColor(hue: 208.0/360.0, saturation: 0.51, brightness: 0.75, alpha: 1.0)

case let r where r >= 0.25:

strokeColor = UIColor(hue: 40.0/360.0, saturation: 0.39, brightness: 0.85, alpha: 1.0)

default:

strokeColor = UIColor(hue: 359.0/360.0, saturation: 0.48, brightness: 0.63, alpha: 1.0)

}

ringLayer.strokeColor = strokeColor.CGColor

}

}

}


bottom of page