git.iko.soy/metal-metaballs
on master help sign in
Added animating function.
519f4dcvinivendra
PixelImage/ViewController.swift+25
@@ -43,6 +43,31 @@ class ViewController: UIViewController, MetaballDataSource {
43 metaballView.addSubview(metaball)
43 metaballView.addSubview(metaball)
44 }
44 }
45
45
46+ let parameters = EdgeAnimationParameters(startDate: NSDate(), duration: 3, fadeIn: true, i: 0, j: 2)
47+ NSTimer.scheduledTimerWithTimeInterval(1.0/60.0, target: self, selector: "animateEdgeWithTimer:", userInfo: parameters, repeats: true)
48+
49+ renderer.state = .Running
50+ }
51+
52+ func animateEdge(withTimer timer: NSTimer) {
53+ let (animationStart, duration, fadeIn, i, j) = (timer.userInfo as! EdgeAnimationParameters).unpack()
54+
55+ let now = NSDate()
56+ var timeElapsed = Float(now.timeIntervalSinceDate(animationStart))
57+
58+ if timeElapsed > duration {
59+ timer.invalidate()
60+ timeElapsed = duration
61+ }
62+
63+ let interpolee = timeElapsed / duration
64+ let interpolated = interpolee ^ 3
65+
66+ let newValue = fadeIn ? interpolated : (1 - interpolated)
67+
68+ metaballGraph.adjacencyMatrix.set(i, j, value: newValue)
69+ metaballGraph.adjacencyMatrix.set(j, i, value: newValue)
70+
46 renderer.state = .Running
71 renderer.state = .Running
47 }
72 }
48
73