Skip to content

Instantly share code, notes, and snippets.

@traviskirton
Last active September 19, 2017 15:35
Show Gist options
  • Save traviskirton/305d049e3a3da31477a1 to your computer and use it in GitHub Desktop.
Save traviskirton/305d049e3a3da31477a1 to your computer and use it in GitHub Desktop.
Code from the Basics Tutorial
// Copyright © 2016 C4
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: The above copyright
// notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//import C4
import UIKit
class ViewController: CanvasController {
override func setup() {
line()
}
func sequenceAnimation() {
let square = Rectangle(frame: Rect(0, 0, 100, 100))
square.center = canvas.center
canvas.add(square)
let circle = Circle(center: canvas.center, radius: 50)
canvas.add(circle)
let squareAnim = ViewAnimation(duration: 1.0) {
square.transform = Transform.makeRotation(M_PI)
}
let circleAnim = ViewAnimation(duration: 1.0) {
let randomColor = Color(red: random01(), green: random01(), blue: random01(), alpha: 1.0)
circle.fillColor = randomColor
}
let sequence = ViewAnimationSequence(animations: [squareAnim, circleAnim])
wait(1.0) {
sequence.animate()
}
}
func groupAnimation() {
let square = Rectangle(frame: Rect(0, 0, 100, 100))
square.center = canvas.center
canvas.add(square)
let circle = Circle(center: canvas.center, radius: 50)
canvas.add(circle)
let squareAnim = ViewAnimation(duration: 1.0) {
square.transform = Transform.makeRotation(M_PI)
}
let circleAnim = ViewAnimation(duration: 1.0) {
let randomColor = Color(red: random01(), green: random01(), blue: random01(), alpha: 1.0)
circle.fillColor = randomColor
}
let group = ViewAnimationGroup(animations: [squareAnim, circleAnim])
wait(1.0) {
group.animate()
}
}
func propertyAnimation() {
let anim = ViewAnimation(duration: 1.0) {
self.canvas.backgroundColor = C4Blue
}
anim.curve = .EaseOut
anim.repeats = true
wait(1.0) {
anim.animate()
}
}
func panGesture() {
let square = Rectangle(frame: Rect(0, 0, 100, 100))
square.center = canvas.center
canvas.add(square)
canvas.addPanGestureRecognizer { location, translation, velocity, state in
square.center = location
}
}
func tapGesture() {
let square = Rectangle(frame: Rect(0, 0, 100, 100))
square.center = canvas.center
canvas.add(square)
square.addTapGestureRecognizer { location, state in
let randomColor = Color(red: random01(), green: random01(), blue: random01(), alpha: 1.0)
self.canvas.backgroundColor = randomColor
}
}
let player = AudioPlayer("C4Loop.aif")
func audioPlayer() {
player?.loops = true
player?.play()
}
func movie() {
let movie = Movie("halo.mp4")
movie?.center = canvas.center
canvas.add(movie)
movie?.play()
}
func generator() {
let image = Image()
image.frame = Rect(0, 0, 100, 100)
let checkerBoard = Checkerboard()
image.generate(checkerBoard)
image.center = canvas.center
canvas.add(image)
}
func filter() {
let image = Image("chop")
image?.center = canvas.center
canvas.add(image)
var dotScreen = DotScreen()
dotScreen.width = 10.0
image?.apply(dotScreen)
}
func image() {
let image = Image("chop")!
image.center = canvas.center
canvas.add(image)
}
func patterns() {
let r = Rectangle(frame: Rect(0, 0, 96, 96))
r.center = canvas.center
r.fillColor = Color("pattern1")
r.lineWidth = 8.0
r.strokeColor = Color("pattern2")
canvas.add(r)
}
func colorsCustom() {
canvas.backgroundColor = Color(red: 0.25, green: 0.5, blue: 0.75, alpha: 1.0)
}
func colorsDefault() {
let r = Rectangle(frame: Rect(0, 0, 100, 100))
r.center = canvas.center
r.lineWidth = 8
r.strokeColor = green
r.fillColor = blue
canvas.add(r)
canvas.backgroundColor = red
}
func colors() {
let r = Rectangle(frame: Rect(0, 0, 100, 100))
r.center = canvas.center
r.lineWidth = 8
r.strokeColor = C4Blue
r.fillColor = C4Pink
canvas.add(r)
canvas.backgroundColor = C4Purple
}
func textShape() {
let string = "C4"
let textShape = TextShape(text: string)!
textShape.center = canvas.center
canvas.add(textShape)
}
func wedge() {
let wedge = Wedge(center: canvas.center, radius: 50, start: 1.25 * M_PI, end: 1.75 * M_PI)
canvas.add(wedge)
}
func arc() {
let arc = Arc(center: canvas.center, radius: 50, start: M_PI, end: 2 * M_PI)
canvas.add(arc)
}
func star() {
let star = Star(center: canvas.center, pointCount: 5, innerRadius: 25.0, outerRadius: 50.0)
canvas.add(star)
}
func regularPolygon() {
let regularPolyon = RegularPolygon(center: canvas.center, radius: 50.0, sides: 6, phase: 0.0)
canvas.add(regularPolyon)
}
func polygon() {
let points = [Point(), Point(100, 100), Point(200, 0), Point(300, 100)]
let polygon = Polygon(points)
polygon.center = canvas.center
canvas.add(polygon)
}
func triangle() {
let points = [Point(), Point(100, 100), Point(200, 0)]
let triangle = Triangle(points)
triangle.center = canvas.center
canvas.add(triangle)
}
func ellipse() {
let ellipse = Ellipse(frame: Rect(0, 0, 200, 100))
ellipse.center = canvas.center
canvas.add(ellipse)
}
func circle() {
let circle = Circle(center: canvas.center, radius: 50)
canvas.add(circle)
}
func square() {
let square = Rectangle(frame: Rect(0, 0, 100, 100))
square.center = canvas.center
canvas.add(square)
}
func line() {
let points = (Point(), Point(100, 100))
let line = Line(points)
line.center = canvas.center
canvas.add(line)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment