iOS多点触控与手势识别

0 解析关于手势操做事件

1>UI时间分类
swift

(1)touch:各类手势ide

(2)motion:例如到传感器,例如摇晃ui

(3)Remote control:利用外部设备,例如插入耳机this

2>Touch事件阶段spa

touch begin --> touch move --> touch end --> touch cancelcode

多指手势流程:orm

3>Recognize UIView ViewController的关系
事件

 

点击-UITapGestureRecognizer

    override func viewDidLoad() {
        super.viewDidLoad()
               
        let rect=CGRect(x:80,y:200,width:200,height:200);
        var view1 = UIView(frame: rect)
        view1.backgroundColor = UIColor.redColor()
        self.view.addSubview(view1)
        
        //1,创建手势识别器
        var gesture = UITapGestureRecognizer(target: self, action: "viewAction:")
#warning 若是想要识别器可以识别多种触控,例如1点和2点,那就要创建2个UITapGestureRecognizer
        gesture.numberOfTapsRequired = 2    //点击次数
       // gesture.numberOfTouchesRequired = 2 //多点触摸
        
        //2,关联识别器到视图
        view1.addGestureRecognizer(gesture)
        
    }
    //3,手势引用的动做
    func viewAction(sender:UITapGestureRecognizer){
        //得到点击处的位置
        var point:CGPoint = sender.locationInView(self.view)
        println("\(point)")
        println("clicked")
    }


放大缩放UIPinchGestureRecognizer

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1,创建手势识别器
        var gesture = UIPinchGestureRecognizer(target: self, action: "viewAction:")
        
        //2,关联识别器到视图
        view1.addGestureRecognizer(gesture)
        
    }
    //3,手势引用的动做
    func viewAction(sender:UIPinchGestureRecognizer){
        
        var _height = view1.bounds.height
        var _width  = view1.bounds.width
        
       view1.bounds.size = CGSize(width: _width * sender.scale,
                                        height: _height * sender.scale)
    }


旋转UIRotationGestureRecognizer

  override func viewDidLoad() {
        super.viewDidLoad()
        
        //1,创建手势识别器
        var gesture = UIRotationGestureRecognizer(target: self, action: "viewAction:")
        
        //2,关联识别器到视图
        view1.addGestureRecognizer(gesture)
        
    }
    //3,手势引用的动做
    func viewAction(sender:UIRotationGestureRecognizer){
        
      view1.transform = CGAffineTransformMakeRotation(sender.rotation)
    }


4 滑动UISwipeGestureRecognizer

 var offsetX:CGFloat = 0.0
  override func viewDidLoad() {
        super.viewDidLoad()
        
        //1,创建手势识别器
        var gesture = UISwipeGestureRecognizer(target: self, action: "viewAction:")
         //设置多指
        gesture.numberOfTouchesRequired = 2;
        //2,关联识别器到视图
        view1.UISwipeGestureRecognizer(gesture)
        
    }
    //3,手势引用的动做
    func viewAction(sender:UISwipeGestureRecognizer){
        
        offsetX += 20.0
        //方向属性
        if(sender.direction == UISwipeGestureRecognizerDirection.Right){
            
            view1.transform = CGAffineTransformMakeTranslation(offsetX, 0)
        }
    }


5  平移UIPanGestureRecognizer

  override func viewDidLoad() {
        super.viewDidLoad()
        
        var gesture = UIPanGestureRecognizer(target: self, action: "view1Tap:")
        
        //支持的多指范围
        gesture.minimumNumberOfTouches = 1;
        gesture.maximumNumberOfTouches = 2;
        
        view1.addGestureRecognizer(gesture)
    }
    //3,手势引用的动做
    func viewAction(sender:UIPanGestureRecognizer){
        
        //相对于view1视图偏移的位置
        var _transX = sender.translationInView(view1).x
        var _transY = sender.translationInView(view1).y
        
        view1.transform = CGAffineTransformMakeTranslation(_transX, _transY)
    }


长按UILongPressGestureRecognizer

  override func viewDidLoad() {
        super.viewDidLoad()
        
        var gesture = UILongPressGestureRecognizer(target: self, action: "view1Tap:")
        
        //须要的点数和点击次数
       // gesture.numberOfTouchesRequired
       // gesture.numberOfTapsRequired
        
        //最短长按时间
        gesture.minimumPressDuration = 1
        
        //运行移动的点数,在这个范围内不发生动做。默认是10
        gesture.allowableMovement = 10
        
        view1.addGestureRecognizer(gesture)
    }
    //3,手势引用的动做
    func viewAction(sender:UILongPressGestureRecognizer){
        
        UIAlertView(title: "longpress", message: "你长按了", delegate: self, cancelButtonTitle: "肯定").show()
        
    }
    
    //2.获取手势的view的坐标点
        CGPoint location = [recognizer locationInView:recognizer.view];
      
    //3.判断点是否在rect范围
    BOOL isyes = CGRectContainsPoint(btn.frame, location);


UIScreenEdgePanGestureRecognizer

class UIScreenEdgePanGestureRecognizer : UIPanGestureRecognizer {
    var edges: UIRectEdge //< The edges on which this gesture recognizes, relative to the current interface orientation
}
是pan的子类,从边缘出来


8 自定义手势

//
//  UICustomGestureRecognizer.swift
//  ttt
//
//  Created by ling on 15/8/26.
//  Copyright (c) 2015年 ling. All rights reserved.
//

import UIKit
import UIKit.UIGestureRecognizerSubclass


class UICustomGestureRecognizer: UIGestureRecognizer {
   
    
    var leftTop = false
    var rightBttom = false
    
    //初始化
    override init(target: AnyObject, action: Selector) {
       super.init(target: target, action: action)
    }
    
    
    override func touchesBegan(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        return
    }
    
    override func touchesMoved(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        
        var myTouch = (touches as NSSet).anyObject() as! UITouch
        var myLocation = myTouch.locationInView(self.view)
        
        if(myLocation.x < 10 && myLocation.y < 10){
            leftTop = true;
        }
        
        if((myLocation.x + 10) > self.view?.bounds.width && (myLocation.y + 10) > self.view?.bounds.height){
            rightBttom = true
        }
        
        if(leftTop && rightBttom){
            self.state = UIGestureRecognizerState.Ended
        }
        
        println("\(myLocation)")
        
    }
    
    override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        
        self.reset()
        
    }
    
    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        return
    }
    
}



//
//  UICustomGestureRecognizer.swift
//  ttt
//
//  Created by ling on 15/8/26.
//  Copyright (c) 2015年 ling. All rights reserved.
//

import UIKit
import UIKit.UIGestureRecognizerSubclass


class UICustomGestureRecognizer: UIGestureRecognizer {
   
    
    var leftTop = false
    var rightBttom = false
    
    //初始化
    override init(target: AnyObject, action: Selector) {
       super.init(target: target, action: action)
    }
    
    
    override func touchesBegan(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        return
    }
    
    override func touchesMoved(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        
        var myTouch = (touches as NSSet).anyObject() as! UITouch
        var myLocation = myTouch.locationInView(self.view)
        
        if(myLocation.x < 10 && myLocation.y < 10){
            leftTop = true;
        }
        
        if((myLocation.x + 10) > self.view?.bounds.width && (myLocation.y + 10) > self.view?.bounds.height){
            rightBttom = true
        }
        
        if(leftTop && rightBttom){
            self.state = UIGestureRecognizerState.Ended
        }
        
        println("\(myLocation)")
        
    }
    
    override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        
        self.reset()
        
    }
    
    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        return
    }
    
}
相关文章
相关标签/搜索