-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHappinessViewController.swift
55 lines (45 loc) · 1.48 KB
/
HappinessViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// HappinessViewController.swift
// Happiness
//
// Created by Carlo Cossette on 2015-05-21.
// Copyright (c) 2015 Carlo Cossette. All rights reserved.
//
import UIKit
class HappinessViewController: UIViewController, FaceViewDataSource {
@IBOutlet weak var faceView: FaceView! {
didSet {
faceView.dataSource = self
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: "scale:"))
}
}
private struct Constants {
static let HappinessGestureScale: CGFloat = 4
}
@IBAction func changeHappiness(gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .Ended: fallthrough
case .Changed:
let translation = gesture.translationInView(faceView)
let happinessChange = -Int(translation.y / Constants.HappinessGestureScale)
if happinessChange != 0 {
happiness += happinessChange
gesture.setTranslation(CGPointZero, inView: faceView)
}
default: break
}
}
var happiness: Int = 100 {// 0 = very sad, 100 = ecstatic
didSet{
happiness = min(max(happiness, 0), 100)
println ("happiness = \(happiness)")
updateUI()
}
}
func updateUI() {
faceView.setNeedsDisplay()
}
func smilinessForFaceView(sender: FaceView) -> Double? {
return Double (happiness - 50)/50
}
}