git.iko.soy/metal-metaballs
on master help sign in
Refactored metaballs.
8b54ecfvinivendra
PixelImage/Metaball.swiftnew file+20
@@ -0,0 +1,20 @@
1+
2+import UIKit
3+
4+class Metaball: UIView {
5+
6+ var color: UIColor
7+
8+ init(position: CGPoint, color: UIColor = UIColor(randomFlatColorOfShadeStyle: .Light)) {
9+ self.color = color
10+ super.init(frame: CGRect.zero)
11+
12+ size = CGSize(50)
13+ middle = position
14+ }
15+
16+ required init?(coder aDecoder: NSCoder) {
17+ color = UIColor(randomFlatColorOfShadeStyle: .Light)
18+ super.init(coder: aDecoder)
19+ }
20+}
PixelImage/MetalMetaballRenderer.swift+1−1
@@ -179,7 +179,7 @@ class MetalMetaballRenderer {
179 array.append(Float(x))
179 array.append(Float(x))
180 array.append(Float(y))
180 array.append(Float(y))
181
181
182− let color = metaball.backgroundColor!
182+ let color = metaball.color
183 let components = color.components
183 let components = color.components
184 array.append(Float(components.red))
184 array.append(Float(components.red))
185 array.append(Float(components.green))
185 array.append(Float(components.green))
PixelImage/UIColor.swift+6
@@ -7,3 +7,9 @@ extension UIColor {
7 return (r, g, b, a)
7 return (r, g, b, a)
8 }
8 }
9}
9}
10+
11+extension UIColor {
12+ convenience init(red255 red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
13+ self.init(red: red / 255, green: green / 255, blue: blue / 255, alpha: alpha / 255)
14+ }
15+}
PixelImage/ViewController.swift+16−18
@@ -23,9 +23,22 @@ class ViewController: UIViewController, MetaballDataSource {
23 let recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
23 let recognizer = UIPanGestureRecognizer(target: self, action: "handlePan:")
24 view.addGestureRecognizer(recognizer)
24 view.addGestureRecognizer(recognizer)
25
25
26− let metaballs = [CGPoint(x: 70, y: 70), CGPoint(x: 270, y: 70), CGPoint(x: 270, y: 470), CGPoint(x: 70, y: 470)].map {
26+ let lowX = screenWidth / 3
27+ let highX = screenWidth - lowX
28+ let lowY = screenHeight / 4
29+ let highY = screenHeight - lowY
30+ let positions =
31+ [CGPoint(x: lowX, y: lowY),
32+ CGPoint(x: highX, y: lowY),
33+ CGPoint(x: highX, y: highY),
34+ CGPoint(x: lowX, y: highY)]
35+ let colors =
36+ [UIColor(red255: 90, green: 170, blue: 170, alpha: 1.0),
37+ UIColor(red255: 90, green: 170, blue: 170, alpha: 1.0),
38+ UIColor(red255: 110, green: 220, blue: 220, alpha: 1.0),
39+ UIColor(red255: 250, green: 235, blue: 50, alpha: 1.0)]
27− Metaball(position: $0)
40+ let metaballs = zip(positions, colors).map { Metaball(position: $0.0, color: $0.1) }
28− }
41+
29 metaballGraph = Graph(vertices: metaballs)
42 metaballGraph = Graph(vertices: metaballs)
30 metaballGraph.addEdge(0, 1)
43 metaballGraph.addEdge(0, 1)
31 metaballGraph.addEdge(0, 3)
44 metaballGraph.addEdge(0, 3)
@@ -104,18 +117,3 @@ class ViewController: UIViewController, MetaballDataSource {
104 }
117 }
105
118
106}
119}
107−
108−class Metaball: UIView {
109−
110− init(position: CGPoint) {
111− super.init(frame: CGRect.zero)
112−
113− size = CGSize(50)
114− middle = position
115− backgroundColor = UIColor(randomFlatColorOfShadeStyle: .Light)
116− }
117−
118− required init?(coder aDecoder: NSCoder) {
119− super.init(coder: aDecoder)
120− }
121−}