Chapter 11
Small Stars
Bring those constellations to life with stars. We're starting small.
Chapter 11 Contents
Adding the small stars is almost the same as adding the background ones, except we know that the small stars layer needs to be based on its speed, and that it requires a different image.
Open StarsSmall.swift
and add the following to the init:
var signOrder = AstrologicalSignProvider.sharedInstance.order
contentSize = CGSizeMake(frame.size.width * (1.0 + CGFloat(signOrder.count) * gapBetweenSigns), 1.0)
signOrder.append(signOrder[0])
Then, simply create a for
loop that adds all the small stars based on the positions we retrieve from the provider, like so:
//adds all the small stars to the view
for i in 0..<signOrder.count {
//calculates the offset
let dx = Double(i) * Double(frame.size.width * speed * gapBetweenSigns)
//creates a transform
let t = C4Transform.makeTranslation(C4Vector(x: Double(center.x) + dx, y: Double(center.y), z: 0))
//grabs the current sign
if let sign = AstrologicalSignProvider.sharedInstance.get(signOrder[i]) {
//creates a new small star for each point
for point in sign.small {
let img = C4Image("6smallStar")!
var p = point
p.transform(t)
img.center = p
add(img)
}
}
}
Siiiiiiiimple.
Download CodeTest It
Check the orientation of the small stars by running the following from your main WorkSpace
:
canvas.backgroundColor = COSMOSbkgd
let smallStars = StarsSmall(frame: view.frame, speed: 1.0)
canvas.add(smallStars)
The background needs to be dark because the image itself is white, and otherwise it would not be possible to see.