IOS初学-简单的手势操作(单点,双击,长按)

手势之前先来个旋转。其它的简单动画同理

对视图进行旋转操作

func test8() {
        let view1=UIView(frame: CGRect(x: 50, y: 150, width: 200, height: 50));
        view1.backgroundColor=UIColor.brown;
        self.view.addSubview(view1);
        
        //创建一个放射变换变量  可用于平移 旋转 缩放 换路径   图形上下文
        var transform=view1.transform;
        //旋转 45度
        transform=transform.rotated(by: 3.14/4);
        view1.transform=transform;
    }

使用手势功能给视图添加点击事件实现交互

func test9() {
        let  imageView=UIImageView(frame: CGRect(x: 32, y: 80, width: 256, height: 256));
        imageView.image=UIImage(named: "Pic4");
        //开启视图对象的交互功能
        imageView.isUserInteractionEnabled=true;
        self.view.addSubview(imageView);
        
        //创建手势检测类  抽象类  定义了所有手势的基本行为。有6个子类
        let guesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.test9OnClick));
        imageView.addGestureRecognizer(guesture);
    }
    @objc func test9OnClick(){
        //创建一个弹窗
        let alertView = UIAlertController(title: "title", message: "test9的点击事件", preferredStyle: UIAlertControllerStyle.alert);
        //为窗口添加一个按钮。用来关闭弹窗
        let okAction=UIAlertAction(title: "关闭窗口", style:.default,handler:nil);
        alertView.addAction(okAction);
        self.present(alertView,animated: true,completion: nil);
        
        
    }

使用手势功能给视图添加长按事件实现交互

func test10() {
        let view = UIImageView(frame: CGRect(x: 32, y: 80, width: 256, height: 256))
        view.image=UIImage(named: "Pic5");
        view.isUserInteractionEnabled=true;
        self.view.addSubview(view);
        //长按手势对象
        let guesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.test10LongOnClick));
        view.addGestureRecognizer(guesture);
        
    }
    @objc func test10LongOnClick(guesture:UILongPressGestureRecognizer){
        if (guesture.state==UIGestureRecognizerState.began) {
            let dialog=UIAlertController(title: "标题", message: "内容", preferredStyle: UIAlertControllerStyle.alert);
            let okAction=UIAlertAction(title: "ok", style: .default, handler: nil);
            dialog.addAction(okAction);
            self.present(dialog,animated: true,completion: nil);
        }
    }

手势双击操作

func test11()  {
        let view = UIImageView(frame: CGRect(x: 32, y: 80, width: 256, height: 256))
        view.image=UIImage(named: "Pic1");
        view.isUserInteractionEnabled=true;
        self.view.addSubview(view);
        
        let guesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.test11DoubleOnClick));
        //设置点击次数
        guesture.numberOfTapsRequired=2;
        //设置为单次双击事件
        guesture.numberOfTouchesRequired=1
        view.addGestureRecognizer(guesture);
        
        
    }
    @objc func test11DoubleOnClick(){
        let alert = UIAlertController(title: "标题", message: "content", preferredStyle: UIAlertControllerStyle.alert);
        alert.addAction(UIAlertAction(title: "ok", style: .default, handler: nil));
        self.present(alert,animated: true,completion: nil);
        
    }

 

你可能感兴趣的:(IOS学习笔记)