Chapter 12
Small Stars

Estimated Time


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:

1
2
3
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//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 = Transform.makeTranslation(Vector(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 = Image("6smallStar")!
            var p = point
            p.transform(t)
            img.center = p
            add(img)
        }
    }
}

Siiiiiiiimple.

Download a copy of SmallStars.swift.

Test It

Check the orientation of the small stars by running the following from your project’s WorkSpace:

1
2
3
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.