• swift 触摸与手势


    class MyView: UIView {

        var lView:UIView!

        

        var time:NSTimer!

        override init(frame: CGRect) {

            super.init(frame: frame)

            

            //开启多点触控

            self.multipleTouchEnabled = true

            

            lView = UIView(frame: CGRect(x: 0, y: 0, 50, height: 50));

            lView.backgroundColor = UIColor.yellowColor();

            self.addSubview(lView)

        }

        required init?(coder aDecoder: NSCoder) {

            super.init(coder: aDecoder)

        }

        //开始点击时,调用的方法

        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

            //        println(count)

            //拿到一个UITouch对象

    //        let touch:UITouch = touches.anyObject() as! UITouch;

            let touch = touches.first as UITouch!

            //拿到点击次数

            let tapCount = touch.tapCount;

            if (tapCount == 1)

            {

                //  单击 singleTap()

                //使用定时器延时调用

                time = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector:"singleTap", userInfo: nil, repeats: false)

            }else

            {

                //终止定时器

                time.invalidate()

                doubleTap()

            }

        }

        

        func singleTap() {

            print("单击")

        }

        

        func doubleTap() {

            print("双击")

        }

        

        //触摸移动时,调用的方法

        

        override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

            // var touch = touches.anyObject() as! UITouch

            let touch = touches.first as UITouch!

            

            let location = touch.locationInView(self)

            

            // println("location: (location.x), (location.y), (location)")

            let lastLocation = touch.previousLocationInView(self)

            

            // println("lastLocation: (lastLocation.x), (lastLocation.y), (lastLocation)")

            let sub = location.x - lastLocation.x

            if sub > 0{

                print("right")

            }else if sub < 0 {

                print("left")

            }else {

                print("move")

            }

            

            //让小矩形跟着手势移动  lView.center = location

            lView.frame.origin = location

        }

    }

  • 相关阅读:
    aws-lambda之异步实现文件的下载上传
    aws实例部署flask报错script-timed-out-before-returning-headers-application-py
    aws上部署scrapy,出现 Out of Memory,内存溢出
    在线UserAgent,爬虫UA
    ubuntu10.24 下安装 unixODBC coreseek4.1 手记
    ubuntu12.04 安装 python2.6
    coreseek/sphinx CentOS6.4下安装
    Elasticsearch 6.2.3 崩溃经历
    (转)梳理在线教育的几大金矿
    kangaroo-open 开源在线公开课平台
  • 原文地址:https://www.cnblogs.com/ZGSmile/p/5726352.html
Copyright © 2020-2023  润新知