// HappinessViewController.swift
// Happiness
//
// Created by longrain on 15/12/16.
// Copyright © 2015年 longrain. All rights reserved.
//
//import Cocoa
import UIKit
class HappinessViewController: UIViewController, FaceViewDataSource
{
@IBOutlet weak var faceView: FaceView!{
didSet {
faceView.dataSource = self
<u> faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: "scale:"))
</u> }
}
}
///Users/longrain/Documents/Practice/Fourier Transform/Happiness/Happiness/FaceView.swift
// FaceView.swift
// Happiness
//
// Created by longrain on 15/12/16.
// Copyright © 2015年 longrain. All rights reserved.
//
import UIKit
protocol FaceViewDataSource: class{ // 协议只能被类引用
func smilinessForFaceView(sender: FaceView) -> Double?
}
@IBDesignable
class FaceView: UIView {
@IBInspectable
var lineWidth: CGFloat = 3 {didSet{setNeedsDisplay()}} // Marks the receiver’s entire bounds rectangle as needing to be redrawn.
@IBInspectable
var color: UIColor = UIColor.blueColor(){didSet{setNeedsDisplay()}}
<u> @IBInspectable
var scale: CGFloat = 0.90{didSet{setNeedsDisplay()}}</u>
var faceCenter: CGPoint{
return convertPoint(center, fromView: superview)
}
var faceRadius: CGFloat{
return min(bounds.size.width, bounds.size.height)/2 * scale
}
weak var dataSource: FaceViewDataSource?
<u> func scale(gesture: UIPinchGestureRecognizer){
if gesture.state == .Changed{
scale *= gesture.scale
gesture.scale = 1
}
}</u>
}
gesture.scale = 1的原因是,每次识别手势,完成显示的切换后,都应该重置