手势、UILabel和UIImageView默认都是不能交互的。


//  所有继承自UIResponder的类(UIView和UIViewController)都可以去重写UITouch的相关方法,去检测视图的开始触摸、结束触摸以及移动等触摸相关事件
//  触摸只有在视图可以接收用户交互时才会有效,UILabel和UIImageView默认都是不能交互的。

class ViewController: UIViewController {
    
    let ball = UIView()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //创建小球
        ball.backgroundColor = UIColor.redColor()
        self.view.addSubview(ball)
        self.ball.frame = CGRectMake(-100, -100, 100, 100)
//        ball.frame.size = CGSizeMake(100, 100)
        
        //创建label
        
        let label = FlyLabel(frame: CGRectMake(100,100,100,50))
        label.text = "hello,world"
        label.textAlignment = .Center
        self.view.addSubview(label)
        
        //打开label的用户交互
        label.userInteractionEnabled = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


//MARK: - UITouch
extension ViewController {
    
    //1.每次开始触摸的时候会自动调用
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
 
//        print("开始触摸")
        
        //参数1.touches -> 在多个手指同时触摸屏幕的时候,只能获取到一个触摸对象
        //拿到当前的触摸对象
        let touch = touches.first
        
        //拿到当前触摸点的位置
        //参数:计算坐标的相对视图
        let location = touch?.locationInView(self.view)
        print(location)
        
        //参数2:event -> 可以拿到多个触摸对象
        let allTouches = event?.allTouches()
        print("ALL:\(allTouches?.count)")
        //遍历拿到每个触摸对象
        for item in allTouches! {
            
            print(item.locationInView(self.view))
        }
        
        
        //设置球的坐标,让其出现在触摸的位置
        self.ball.center = location!
    }
    
    //2.每次触摸结束的时候会自动调用
    override func touchesEnded(touches: Set, withEvent event: UIEvent?) {
//        print("结束触摸")
    }
    
    //3.手指在屏幕上移动的时候会实时调用
    override func touchesMoved(touches: Set, withEvent event: UIEvent?) {
//        print("移动")
        
        let touch = touches.first
        let location = touch?.locationInView(self.view)
        self.ball.center = location!
        
    }


////
  override func touchesMoved(touches: Set, withEvent event: UIEvent?) {

        print("在label上移动")
        self.center  = (touches.first?.locationInView(self.superview))!
    }
//        1.创建点击手势对象
//        UIGestureRecongnizer是所有手势的父类
//        参数1.调用方法的对象
//        参数2.方法对应的选择器 -> 这个方法如果带参只能带一个参数,参数的对象就是当前创建的点击手势对象
//        点击手势发生的时候让参数1调用参数2中的方法
    
        let tap = UITapGestureRecognizer.init(target: self, action: "tapActon:")
        
        //核心属性:点击次数(默认是1->单击)
        tap.numberOfTapsRequired = 2
        
//        2.将点击手势添加到视图上(要求:添加手势的视图必须可以进行用户交互)
        self.view.addGestureRecognizer(tap)
        
//        3.打开图片的用户交互,并添加点击手势
//        注意:同一个手势对象,只能被添加到一个视图
        self.imagView.addGestureRecognizer(tap)
    }
    
    
    
    
    func tapAction(tap:UITapGestureRecognizer) {
        
        print("点击")
        
        if self.imagView.frame.size.width == 200 {
            self.imagView.frame = self.view.bounds
    
        }
        else {
            self.imagView.frame = CGRectMake(0, 0, 200, 200)
            self.imagView.center = self.view.center
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
       
    }

//        1.创建手势对象
        let longPress = UILongPressGestureRecognizer.init(target: self, action: "longPressAction:")
//        2.添加到视图
        self.imagView.addGestureRecognizer(longPress)
        
//        3.核心属性:长按时间
        longPress.minimumPressDuration = 1
        
        
    }

    //在长按开始和长按结束的时候会调用 -> 一个长按手势发生这个方法会调用两次
    func longPressAction(longPress: UILongPressGestureRecognizer) {
        //所有的手势类都一个状态属性,用来获取手势的开始和结束状态
        if longPress.state == .Began {
            print("长按")
            
            self.imagView.transform = CGAffineTransformMakeRotation(-0.01)
            
            UIView.animateWithDuration(0.3, delay: 0, options: [.Repeat,.Autoreverse], animations: { () -> Void in
                self.imagView.transform = CGAffineTransformMakeRotation(0.01)
                }, completion: nil)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        
    }
    
    override func touchesBegan(touches: Set, withEvent event: UIEvent?) {
//      停止UIView动画
        
        self.imagView.layer.removeAllAnimations()
//        让形变清空
        self.imagView.transform = CGAffineTransformIdentity
    }

//        注意如果想要一个界面既有左滑又有右划,就添加两个方向不同的滑动手势
//       1.创建手势对象
        let swipe = UISwipeGestureRecognizer.init(target: self, action: "swipeAction:")
        //2.添加手势
        self.view.addGestureRecognizer(swipe)
        
        //3.核心属性:滑动方向(默认是右划->从左往右滑)
        swipe.direction = .Right
    }
    
    func swipeAction(swip: UISwipeGestureRecognizer) {
        
        print("滑动")
        self.navigationController?.popViewControllerAnimated(true)
    }



    //保留每次的缩放比
    var lasetScale: CGFloat = 1
    override func viewDidLoad() {
        super.viewDidLoad()

        //1.创建缩放对象
        let pinch = UIPinchGestureRecognizer.init(target: self, action: "pinchAction:")
        //2.添加手势
        self.imagView.addGestureRecognizer(pinch)
    }

    
    func pinchAction(pinch: UIPinchGestureRecognizer) {
        
        print("缩放")
        //3.核心属性:缩放比
        let scale = pinch.scale
        self.imagView.transform = CGAffineTransformMakeScale(scale*lasetScale, scale*lasetScale)
        //在缩放结束的时候更新lasetScale
        if pinch.state == .Ended {
            lasetScale *= scale
        }
        
        print(scale)
    }


 //保存原始的位移
    var lastTransForm = CGAffineTransformMakeTranslation(0, 0)
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.创建拖动手势对象
        let pan = UIPanGestureRecognizer.init(target: self, action: "panAction:")
        //2.添加手势
        self.view.addGestureRecognizer(pan)
        
        
    }
    
    func panAction(pan: UIPanGestureRecognizer) {
            print("拖动")
//       3.核心属性:在指定视图上的位移
        let translation = pan.translationInView(self.view)
//        注意算位移的时候是算的位移,相对于你的起始点的位移是多少
//        print(translation)
        self.imagView.transform = CGAffineTransformMakeTranslation(translation.x + self.lastTransForm.tx , translation.y + self.lastTransForm.ty)
        //在每次拖动结束的时候更新lastTransForm
        if pan.state == .Ended {
            self.lastTransForm = self.imagView.transform
        }
        
    }

 //保存上一次旋转结束的时候的旋转角度
    var lastRotation:CGFloat = 0
    override func viewDidLoad() {
        super.viewDidLoad()

        //1.创建旋转手势对象
        let rotationG = UIRotationGestureRecognizer.init(target: self, action: "rotationActon:")
        //2.添加手势
        self.view.addGestureRecognizer(rotationG)
        
       
    }
    
    func rotationActon(rotationG: UIRotationGestureRecognizer) {
       
        //3.核心属性:旋转角度
        let rotation = rotationG.rotation
        self.imagView.transform = CGAffineTransformMakeRotation(lastRotation + rotation)
        
        if rotationG.state == .Ended {
            lastRotation += rotation
        }
        
        print(rotation)
        
    }

你可能感兴趣的:(手势、UILabel和UIImageView默认都是不能交互的。)