Last active
August 29, 2015 14:11
-
-
Save wintermuted/d0675f5d08a08c89eaf9 to your computer and use it in GitHub Desktop.
Swift Shapes exercises
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Playground - noun: a place where people can play | |
| import UIKit | |
| import XCPlayground | |
| var str = "Hello, playground" | |
| class Shape: UIView { | |
| var color:UIColor | |
| init(color:UIColor) { | |
| self.color = color | |
| super.init(frame: CGRectZero) | |
| self.backgroundColor = color | |
| } | |
| func getArea() -> Double { | |
| return 0 | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| } | |
| class Square: Shape { | |
| var width:Int | |
| init(width:Int, color:UIColor) { | |
| self.width = width | |
| super.init(color: color) | |
| self.frame.size = CGSize(width: width, height: width) | |
| } | |
| override func getArea() -> Double { | |
| return(Double(width * width)) | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| } | |
| class Circle: Shape { | |
| var radius: Int | |
| init(color: UIColor, radius: Int) { | |
| self.radius = radius | |
| super.init(color: color) | |
| self.frame.size = CGSize(width: radius * 2, height: radius * 2) | |
| self.layer.cornerRadius = CGFloat(radius) | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| override func getArea() -> Double { | |
| return(M_PI * Double(radius * radius)) | |
| } | |
| } | |
| class Rect: Shape { | |
| var width:Int | |
| var height:Int | |
| init(width:Int, height:Int, color:UIColor) { | |
| self.width = width // Circle sets is own width | |
| self.height = height | |
| super.init(color: color) // But it asks the superclass to set the color, since Shape already knows how to do that | |
| self.frame.size = CGSize(width: width, height: height) | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| override func getArea() -> Double { | |
| return(Double(width * height)) | |
| } | |
| } | |
| class RoundedRect: Rect { | |
| var radius: Int | |
| init(width: Int, height: Int, color: UIColor, radius: Int) { | |
| self.radius = radius | |
| super.init(width: width, height: height, color: color) | |
| self.layer.cornerRadius = CGFloat(radius) | |
| } | |
| required init(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| } | |
| var circle = Circle(color: UIColor.blueColor(), radius: 30) | |
| var square = Square(width: 40, color: UIColor.redColor()) | |
| var roundedRect = RoundedRect(width: 200, height: 500, color: UIColor.yellowColor(), radius: 20) | |
| var rect = Rect(width: 30, height: 70, color: UIColor.greenColor()) | |
| circle.center = CGPoint(x: 100, y: 100) | |
| square.center = CGPoint(x: 200, y: 200) | |
| rect.center = CGPoint(x: 100, y: 500) | |
| roundedRect.center = CGPoint(x: 400, y: 400) | |
| let view = UIView(frame: CGRect(x:0, y: 0, width: 1000, height: 1000)) | |
| XCPShowView("Shapes!", view) | |
| view.addSubview(circle) | |
| view.addSubview(square) | |
| view.addSubview(rect) | |
| view.addSubview(roundedRect) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks good!