Skip to content

Instantly share code, notes, and snippets.

@zdying
Created December 1, 2014 05:26
Show Gist options
  • Save zdying/e82cbe92efe23959359b to your computer and use it in GitHub Desktop.
Save zdying/e82cbe92efe23959359b to your computer and use it in GitHub Desktop.
Path Animate
//
// ViewController.swift
// test
//
// Created by 张代应 on 14/11/28.
// Copyright (c) 2014年 imf2e. All rights reserved.
//
import UIKit
//import CoreText
//import QuartzCore
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.purpleColor()
// set up some values to use in the curve
let ovalStartAngle = CGFloat(90.01 * M_PI/180)
//let ovalEndAngle = CGFloat(90 * M_PI/180)
let ovalEndAngle = CGFloat(135 * M_PI/180)
let ovalRect = CGRectMake(97.5, 58.5, 125, 125)
// create the bezier path
let ovalPath = UIBezierPath()
// ovalPath.addArcWithCenter(CGPointMake(CGRectGetMidX(ovalRect), CGRectGetMidY(ovalRect)),
// radius: CGRectGetWidth(ovalRect) / 2,
// startAngle: ovalStartAngle,
// endAngle: ovalEndAngle, clockwise: true)
ovalPath.addArcWithCenter(CGPointMake(10, 126), radius: 7, startAngle: CGFloat(90 * M_PI/180), endAngle: CGFloat(266 * M_PI/180), clockwise: true)
ovalPath.addArcWithCenter(CGPointMake(22, 120), radius: 12, startAngle: CGFloat(192 * M_PI/180), endAngle: CGFloat(332 * M_PI/180), clockwise: true)
ovalPath.addArcWithCenter(CGPointMake(32, 124), radius: 9, startAngle: CGFloat(280 * M_PI/180), endAngle: CGFloat(90 * M_PI/180), clockwise: true)
ovalPath.addLineToPoint(CGPointMake(10, 133))
// ovalPath.addArcWithCenter(CGPointMake(30, 120), radius: 30, startAngle: CGFloat(90 * M_PI/180), endAngle: CGFloat(270 * M_PI/180), clockwise: true)
// ovalPath.addArcWithCenter(CGPointMake(90, 90), radius: 60, startAngle: CGFloat(180 * M_PI/180), endAngle: CGFloat(0 * M_PI/180), clockwise: true)
// ovalPath.addArcWithCenter(CGPointMake(150, 120), radius: 30, startAngle: CGFloat(270 * M_PI/180), endAngle: CGFloat(90 * M_PI/180), clockwise: true)
// ovalPath.addLineToPoint(CGPointMake(30, 150))
// create an object that represents how the curve
// should be presented on the screen
//let progressLine = p_setupTextLayer("a")
let progressLine = CAShapeLayer()
progressLine.path = ovalPath.CGPath
progressLine.strokeColor = UIColor.whiteColor().CGColor
progressLine.fillColor = UIColor.clearColor().CGColor
progressLine.lineWidth = 2.0
progressLine.lineCap = kCALineCapRound
//progressLine.strokeEnd = 0.3
// add the curve to the screen
self.view.layer.addSublayer(progressLine)
// create a basic animation that animates the value 'strokeEnd'
// from 0.0 to 1.0 over 3.0 seconds
let animateStrokeEnd = CABasicAnimation(keyPath: "strokeEnd")
animateStrokeEnd.duration = 3.0
animateStrokeEnd.fromValue = 0.0
animateStrokeEnd.toValue = 1.0
// add the animation
progressLine.addAnimation(animateStrokeEnd, forKey: "animate stroke end animation")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func p_setupTextLayer(text: String) -> CAShapeLayer {
var letters = CGPathCreateMutable()
println("text == > \(text)")
let font = CTFontCreateWithName("Courier", 202, nil)
let attrs = [kCTFontAttributeName as String: font]
var attrString = NSAttributedString(string: text, attributes: attrs)
let line = CTLineCreateWithAttributedString(attrString)
let runArray = CTLineGetGlyphRuns(line)
//var run: CTRun = unsafeBitCast(CFArrayGetValueAtIndex(runArray, 0))
//unsafeBitCast(CFArrayGetValueAtIndex(runArray, 0), CTRun())
var run: CTRun = unsafeBitCast(CFArrayGetValueAtIndex(runArray, 0), CTRun.self)
//var run : CTRun = CFArrayGetValueAtIndex(runArray, 0)
//var runFont: CTFont = unsafeBitCast(CFDictionaryGetValue(CTRunGetAttributes(run), unsafeBitCast(kCTFontAttributeName)))
//var runFont: CTFont = unsafeBitCast(CFDictionaryGetValue(<#theDict: CFDictionary!#>, <#key: UnsafePointer<Void>#>), <#U.Type#>)
var runFont = font
for runGlyphIndex in 0 ..< CTRunGetGlyphCount(run){
var thisGlyphRange = CFRangeMake(runGlyphIndex, 1)
var glyph = CGGlyph()
var position = CGPointZero
CTRunGetGlyphs(run, thisGlyphRange, &glyph)
CTRunGetPositions(run, thisGlyphRange, &position)
let letter = CTFontCreatePathForGlyph(runFont, glyph, nil)
var t = CGAffineTransformMakeTranslation(position.x, position.y)
CGPathAddPath(letters, &t, letter);
}
var path = UIBezierPath()
path.moveToPoint(CGPointZero)
path.appendPath(UIBezierPath(CGPath: letters))
let layer = CAShapeLayer()
layer.frame = CGRectMake(10, 50, 180, 100)
layer.backgroundColor = nil
layer.geometryFlipped = true
layer.path = path.CGPath
layer.strokeColor = UIColor.whiteColor().CGColor
layer.fillColor = nil
layer.lineWidth = 3.0
layer.lineJoin = kCALineJoinBevel
return layer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment