Gold, Bronze, and Chapter 4 in Swift


#1

Please let me know if there’s anything I could have done better!

[code]//
// HypnosisView.swift
// Hypnosister
//
// Created by adam on 6/13/14.
// Copyright © 2014 Adam Schoonmaker. All rights reserved.
//

import UIKit

class HypnosisView: UIView {

init(frame: CGRect) {
    super.init(frame: frame)
    // Initialization code
    
    // All HypnosisViews start with a clear background color
    // (a view's backgroundColor is drawn regardless of what drawRect does,
    // so often custom views are set to have a transparent background so that
    // only the results of drawRect show
    self.backgroundColor = UIColor.clearColor()
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect)
{
    // First thing to do when overriding this: get the bounds
    let bounds = self.bounds
    
    // Figure out the center of the bounds rectangle
    let center = CGPoint(x: (bounds.origin.x + bounds.size.width / 2.0),
        y: (bounds.origin.y + bounds.size.height / 2.0))
    
    // The largest circle will circumscribe the view
    let maxRadius = hypot(bounds.size.width, bounds.size.height) / 2.0
    
    // Draw the circle using UIBezierPath; draws lines and curves to make shapes
    let path = UIBezierPath()
    
    // Could have multiple instances of UIBezierPath, each one being one circle, 
    // or add multiple circles to a single instance. slightly more effcient w/ single instance
    for var currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20 {
        
        // "pick up" the "pencil" and move it to the correct starting spot
        path.moveToPoint(CGPointMake(center.x + currentRadius, center.y))
        
        path.addArcWithCenter(center, radius: currentRadius, startAngle: 0,
            endAngle: M_PI * 2.0, clockwise: true)
        println("drawing circle... x:\(center.x)   y:\(center.y)   radius:\(currentRadius)")
    }
    
    // Make the line width 10 points
    path.lineWidth = 10
    
    // Configure the drawing color to light gray
    // Sets the color of subsequent stroke operations to the color that the receiver represents
    UIColor.lightGrayColor().setStroke()
    
    // Draw the line!
    path.stroke()
    
    
    
    // Shadows cannot be "unset", so the current context must be saved and
    // restored from after the shawdow is used
    var baseContext = UIGraphicsGetCurrentContext()
    CGContextSaveGState(baseContext)
    
    // Calculate values to use for drawing the logo
    let logoRectWidth = bounds.size.width / 2.0
    let logoRectHeight = bounds.size.height / 2.0
    let logoRectOriginX = logoRectWidth / 2.0
    let logoRectOriginY = logoRectHeight / 2.0
    
    
    
    // Draw a triangle path using the logo calculated values
    let triangleTop = CGPointMake(logoRectWidth, logoRectOriginY - 20.0)
    let triangleBottomLeft = CGPointMake(logoRectOriginX - 10.0,
        logoRectOriginY + logoRectHeight + 20.0)
    let triangleBottomRight = CGPointMake(logoRectOriginX + logoRectWidth + 10.0,
        logoRectOriginY + logoRectHeight + 20.0)
    let triangleBottomMiddle = CGPointMake(logoRectWidth,
        logoRectOriginY + logoRectHeight + 20.0)
    
    // Set "pencil" down at the top of the triangle, then draw the edges
    let trianglePath = UIBezierPath()
    trianglePath.moveToPoint(triangleTop)
    trianglePath.addLineToPoint(triangleBottomLeft)
    trianglePath.addLineToPoint(triangleBottomRight)
    trianglePath.addLineToPoint(triangleTop)
    //trianglePath.stroke()
    
    // Use the triangle path to draw a gradient
    // Gradients cover everything in the view, so a clipping path must be 
    // installed on the graphics context that defines what the gradient covers
    trianglePath.addClip()
    
    // Create the gradient
    let locations: CGFloat[] = [0.0, 1.0]
    let components: CGFloat[] = [ 0.0, 1.0, 0.0, 1.0, // Start color: green
                                    1.0, 1.0, 0.0, 1.0] // End color: yellow
    var colorspace = CGColorSpaceCreateDeviceRGB()
    var gradient = CGGradientCreateWithColorComponents(colorspace, components,
        locations, 2)
    
    // Draw the gradient
    CGContextDrawLinearGradient(baseContext, gradient, triangleTop,
        triangleBottomMiddle, 0)
    
    // Deallocate (note: make sure gradient and colorspace are defined with
    // "var" instead of "let", or else this will give an error
    //CGGradientRelease(gradient)
    //CGColorSpaceRelease(colorspace)
    
    // Restore the graphics state to clear the clip path
    CGContextRestoreGState(baseContext)

    
    
    baseContext = UIGraphicsGetCurrentContext()
    CGContextSaveGState(baseContext)
    // Now anything drawn will appear with a shadow
    CGContextSetShadow(baseContext, CGSizeMake(4, 7), 3)
    
    // Create UIImage object from logo.png imported file, draw it over circles
    let logoImage = UIImage(named: "logo.png")
    
    logoImage.drawInRect(CGRect(x: logoRectOriginX, y: logoRectOriginY,
        width: logoRectWidth, height: logoRectHeight))
    
    // Now anything drawn won't have a shadow
    CGContextRestoreGState(baseContext)
    
}

}
[/code]