-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHappinessViewController.swift
63 lines (50 loc) · 1.72 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
56
57
58
59
60
61
62
//
// ViewController.swift
// Happiness
//
// Created by Yapzi on 16/6/11.
// Copyright © 2016年 Yap. All rights reserved.
//
import UIKit
class HappinessViewController: UIViewController, FaceViewDataSource {
@IBOutlet weak var faceView: FaceView! {
didSet {
faceView.dataSource = self
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: NSSelectorFromString("scale:")))
faceView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: NSSelectorFromString("smilinessForGesture:")))
}
}
var happiness: Int = 10 {
didSet {
happiness = min(max(happiness, 0), 100)
updateUI()
}
}
func updateUI() {
faceView?.setNeedsDisplay()
}
func smilinessForFaceView(faceView: FaceView) -> Double? {
let k = Double(happiness - 50) / 50
return k
}
func smilinessForGesture(gesture: UIPanGestureRecognizer) {
let changingScale: CGFloat = 4
switch gesture.state {
case .Ended: fallthrough
case .Changed:
let translation = gesture.translationInView(faceView)
let smilinessChange = Int(translation.y / changingScale)
happiness += smilinessChange
gesture.setTranslation(CGPointZero, inView: faceView)
default: break
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}