Skip to content

Instantly share code, notes, and snippets.

@WorldDownTown
Last active August 29, 2015 14:14
Show Gist options
  • Save WorldDownTown/97ca1f5bfaccf3b17dac to your computer and use it in GitHub Desktop.
Save WorldDownTown/97ca1f5bfaccf3b17dac to your computer and use it in GitHub Desktop.
Swiftで受付システムを作った話とCADisplayLink ref: http://qiita.com/WorldDownTown/items/f66d283052f9809d21df
let displayLink = CADisplayLink(target: self, selector: Selector("update:"))
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
import UIKit
class ViewController: UIViewController {
private let secondLayer = CAShapeLayer()
override func viewDidLoad() {
super.viewDidLoad()
// 円のレイヤー
let frame = view.frame
let path = UIBezierPath()
path.addArcWithCenter(
CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)),
radius: frame.width / 2.0 - 20.0,
startAngle: CGFloat(-M_PI_2),
endAngle: CGFloat(M_PI + M_PI_2),
clockwise: true)
secondLayer.path = path.CGPath
secondLayer.strokeColor = UIColor.blackColor().CGColor
secondLayer.fillColor = UIColor.clearColor().CGColor
secondLayer.speed = 0.0 // ※1
view.layer.addSublayer(secondLayer)
// 円を描くアニメーション
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 60.0
secondLayer.addAnimation(animation, forKey: "strokeCircle")
// CADisplayLink設定
let displayLink = CADisplayLink(target: self, selector: Selector("update:"))
displayLink.frameInterval = 1 // ※2
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
}
func update(displayLink: CADisplayLink) {
// timeOffsetに現在時刻の秒数を設定
let time = NSDate().timeIntervalSince1970
let seconds = floor(time) % 60
let milliseconds = time - floor(time)
secondLayer.timeOffset = seconds + milliseconds // ※3
}
}
import UIKit
class ViewController: UIViewController {
private let secondLayer = CAShapeLayer()
override func viewDidLoad() {
super.viewDidLoad()
// 円のレイヤー
let frame = view.frame
let path = UIBezierPath()
path.addArcWithCenter(
CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame)),
radius: frame.width / 2.0 - 20.0,
startAngle: CGFloat(-M_PI_2),
endAngle: CGFloat(M_PI + M_PI_2),
clockwise: true)
secondLayer.path = path.CGPath
secondLayer.strokeColor = UIColor.blackColor().CGColor
secondLayer.fillColor = UIColor.clearColor().CGColor
secondLayer.speed = 0.0 // ※1
view.layer.addSublayer(secondLayer)
// 円を描くアニメーション
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 60.0
secondLayer.addAnimation(animation, forKey: "strokeCircle")
// CADisplayLink設定
let displayLink = CADisplayLink(target: self, selector: Selector("update:"))
displayLink.frameInterval = 1.0 // ※2
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
}
func update(displayLink: CADisplayLink) {
// timeOffsetに現在時刻の秒数を設定
let time = NSDate().timeIntervalSince1970
let seconds = floor(time) % 60
let milliseconds = time - floor(time)
secondLayer.timeOffset = seconds + milliseconds // ※3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment