Skip to content

Instantly share code, notes, and snippets.

@traviskirton
Created April 2, 2016 23:32
Show Gist options
  • Select an option

  • Save traviskirton/09b46f001e436b84b236e44e09181733 to your computer and use it in GitHub Desktop.

Select an option

Save traviskirton/09b46f001e436b84b236e44e09181733 to your computer and use it in GitHub Desktop.
MMRun
//
// ViewController.swift
// MMRun
//
// Created by travis on 2016-03-28.
// Copyright © 2016 C4. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
var run: UIImageView!
var stop: UIImageView!
var stoppedRight: UIImage!
var stoppedLeft: UIImage!
var runRight: UIImage!
var runLeft: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
addGesture()
generateStoppedImages()
generateRunImages()
createImageViews()
}
func createImageViews() {
run = UIImageView(image: runRight)
stop = UIImageView(image: stoppedRight)
run.center = view.center
stop.center = view.center
view.addSubview(run)
view.addSubview(stop)
run.hidden = true
}
func generateStoppedImages() {
stoppedRight = UIImage(named: "mm_00" )!
stoppedLeft = UIImage(CGImage: stoppedRight.CGImage!, scale: stoppedRight.scale, orientation: .UpMirrored)
}
func generateRunImages() {
var runImages = [
UIImage(named: "mm_01")!,
UIImage(named: "mm_02")!,
UIImage(named: "mm_03")!
]
runRight = UIImage.animatedImageWithImages(runImages, duration: 0.33)
for i in 0..<runImages.count {
let currentImage = runImages[i]
let mirroredImage = UIImage(CGImage: currentImage.CGImage!, scale: currentImage.scale, orientation: .UpMirrored)
runImages[i] = mirroredImage
}
runLeft = UIImage.animatedImageWithImages(runImages, duration: 0.33)
}
func addGesture() {
let lp = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongPress(_:)))
lp.minimumPressDuration = 0.0
view.addGestureRecognizer(lp)
}
func handleLongPress(lp: UILongPressGestureRecognizer) {
let location = lp.locationInView(view)
switch lp.state {
case .Began:
stop.hidden = true
run.hidden = false
switchDirection(location.x > view.center.x)
run.startAnimating()
case .Cancelled, .Ended, .Failed:
stop.hidden = false
run.hidden = true
run.stopAnimating()
default:
break
}
}
func switchDirection(right: Bool) {
if right {
run.image = runRight
stop.image = stoppedRight
} else {
run.image = runLeft
stop.image = stoppedLeft
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment