swift添加手势操作

//  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
            faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: "scale:"))
        }
    }
}


///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()}}
    
    @IBInspectable
    var scale: CGFloat = 0.90{didSet{setNeedsDisplay()}}
    
    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?
    
    func scale(gesture: UIPinchGestureRecognizer){
        if gesture.state == .Changed{
            scale *= gesture.scale
            gesture.scale = 1
        }
    }
}


gesture.scale = 1的原因是,每次识别手势,完成显示的切换后,都应该重置


你可能感兴趣的:(学习iOS)