手势简单应用

 

  • 两指缩放
  • 上下滑动可以改变笑脸弧度

 

@IBDesignable 使View可以在storyBoard中实时预览

@IBInspectable 使属性可以在storyBoard中设置

 

HappinessViewController.swift

import UIKit

public class HappinessViewController: UIViewController ,FaceViewDataSource{
 
    var happiness:Int = 25 {  //0 = vary sad,100 = ecstatic
            didSet{
                happiness = min(max(happiness,0),100)
                println("happiness = \(happiness)")
                updateUI()
            }
    }
    
    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 happiness != 0 {
                happiness += happinessChange
                gesture.setTranslation(CGPointZero, inView: faceView)
            }
            
        default :break
        }
    }
    
    @IBOutlet weak var faceView: FaceUIView!{
        didSet{
            faceView.dataSource  = self
            // 添加缩放事件,调用的是daceView里面的scale方法
            faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: "scale:"))
//            faceView.addGestureRecognizer(UIPanGestureRecognizer(target: faceView, action: "scale:"))
        }
    }
    
    private func updateUI(){
        faceView.setNeedsDisplay()
    }
    
    func smilinessForFaceView(sender:FaceUIView) -> Double?{
        return Double(happiness-50)/50
    }
}

 

FaceUIView.swift

import UIKit

protocol FaceViewDataSource :class{
    func smilinessForFaceView(sender: FaceUIView) -> Double?
}

@IBDesignable
class FaceUIView: UIView {
 
    // @IBInspectable 使属性可以在storyBoard中设置
    @IBInspectable
     var lineWidth:CGFloat = 3{didSet{setNeedsDisplay()}}
    @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
        }
    }
    
    override func drawRect(rect: CGRect) {
        let facePath = UIBezierPath(arcCenter: faceCenter, radius: faceRadius, startAngle: 0, endAngle:CGFloat(2 * M_PI), clockwise: true)
        
        facePath.lineWidth = lineWidth
        color.set()
        facePath.stroke()
        
        bezierPathForEye(.Left).stroke()
        bezierPathForEye(.Right).stroke()
        
        // 此处改变笑脸弧度
        let smiliness = dataSource?.smilinessForFaceView(self) ?? 0.0
        let smilePath = bezierPathForSmile(smiliness)
        smilePath.stroke()
    }
    
    private struct Scaling {
        static let FaceRadiusToEyeRadiusRatio:CGFloat = 10
        static let FaceRadiusToEyeOffsetRatio:CGFloat = 3
        static let FaceRadiusToEyeSeparationRatio:CGFloat = 1.5
        static let FaceRadiusToMouthWidthRatio:CGFloat = 1
        static let FaceRadiusToMouthHeightRatio:CGFloat = 3
        static let FaceRadiusToMouthOffsetRatio:CGFloat = 3
    }
    private enum Eye {case Left,Right}

    // 画眼睛
    private func bezierPathForEye(whichEye:Eye) -> UIBezierPath{
        let eyeRadius = faceRadius / Scaling.FaceRadiusToEyeRadiusRatio
        let eyeVerticaloffset = faceRadius / Scaling.FaceRadiusToEyeOffsetRatio
        let eyeHorizontalSeparation = faceRadius / Scaling.FaceRadiusToEyeSeparationRatio
        
        var eyeCenter = faceCenter
        eyeCenter.y -= eyeVerticaloffset
        
        switch whichEye {
        case .Left: eyeCenter.x -= eyeHorizontalSeparation / 2
        case .Right: eyeCenter.x += eyeHorizontalSeparation / 2
        default :break
        }
        
        let path = UIBezierPath(arcCenter: eyeCenter, radius: eyeRadius, startAngle: 0, endAngle:CGFloat(2*M_PI), clockwise: true)
        
        path.lineWidth = lineWidth
        return path
    }
    
    // 画嘴角弧度
    private func bezierPathForSmile(fractionOfmaxSmile:Double ) -> UIBezierPath{
        let mouthWidth = faceRadius / Scaling.FaceRadiusToMouthWidthRatio
        let mouthHeight = faceRadius / Scaling.FaceRadiusToMouthHeightRatio
        let mouthVerticalOffset = faceRadius / Scaling.FaceRadiusToMouthOffsetRatio
        
        let smileheight = CGFloat(max(min(fractionOfmaxSmile,1), -1)) * mouthHeight
        
        let start = CGPoint(x: faceCenter.x - mouthWidth/2, y: faceCenter.y + mouthVerticalOffset)
        
        
        let end = CGPoint(x:start.x + mouthWidth, y:start.y)
        let cp1 = CGPoint(x:start.x + mouthWidth/3, y:start.y + smileheight)
        let cp2 = CGPoint(x:end.x - mouthWidth/3, y: cp1.y)
        
        let path = UIBezierPath()
        path.moveToPoint(start)
        path.addCurveToPoint(end, controlPoint1: cp1, controlPoint2: cp2)
        path.lineWidth = lineWidth
        return path
    }

}

 

转载于:https://www.cnblogs.com/huangzx/p/4672773.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python手势识别应用可以通过使用OpenCV和Python来实现。OpenCV是一个开源的计算机视觉库,可以用于图像处理和分析。通过结合OpenCV和Python,我们可以实现手势识别应用手势识别技术可以让我们通过手势来控制电脑或手机等设备,从而提供更加便捷的交互方式。在手势识别应用中,我们可以使用摄像头捕捉到的图像数据,通过图像处理和分析的方法来识别手势。 一个简单手势识别应用可以通过使用OpenCV和Python来实现。可以借鉴GitHub上的一个源程序,该程序提供了一个简单手势识别(或手势识别)的实现,使用了OpenCV和Python进行背景减法。\[1\] 在这个应用中,我们可以使用OpenCV来实现人脸检测功能,并在检测到人脸时进行人脸框图的绘制。这样可以帮助我们更好地理解手势的位置和动作。\[2\] 通过学习和参考更多的资料,我们可以扩展手势识别应用的功能。例如,我们可以实现手势识别的分类和动作识别,以实现更多的交互功能。\[3\] 总结来说,Python手势识别应用可以通过使用OpenCV和Python来实现。通过借鉴和学习相关的源代码和资料,我们可以构建一个功能丰富的手势识别应用,提供更加便捷的交互方式。 #### 引用[.reference_title] - *1* [Python手势识别](https://blog.csdn.net/FHHHC/article/details/127964488)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v4^insert_chatgpt"}} ] [.reference_item] - *2* [python 人脸识别和手势识别应用(face++)开发](https://blog.csdn.net/dongxiaodongvip/article/details/106839799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v4^insert_chatgpt"}} ] [.reference_item] - *3* [Python编程:手势识别控制技术实现(含完整源码)](https://blog.csdn.net/ai52learn/article/details/130374734)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值