博客> swift 学习(一) 手势
swift 学习(一) 手势
2019-10-18 13:39 评论:0 阅读:1242 飞哥
ios swift 自定义 手势

从swift 横空出世一来,其排名也在不断攀升,特别是苹果宣布将其开源以后,更是一步飙升,苹果的传统开发语言Object-C的排名已经开始下滑了好几位了。以后采用swift开发一个必然趋势,OK,废话不说,直接上干活吧。

今天来学学swift中的手势,在OC中的手势相比已经很熟悉了,其实swift中也差不多,只是变了一点写法,直接上代码吧,这是最新的swift写法,今年刚刚改版。

1、首先需要继承协议UIGestureRecognizerDelegate

   redView.backgroundColor = UIColor.redColor();

    self.view.addSubview(redView);

    let  tapGestures = UITapGestureRecognizer(target: self,action:#selector(gesturerStudy.tapAction(_:)));
    // 单击
    tapGestures.numberOfTapsRequired = 1;
    tapGestures.numberOfTouchesRequired = 1;
    tapGestures.delegate = self;

    redView.addGestureRecognizer(tapGestures);

    // 滑动手势
    let swipGesture = UISwipeGestureRecognizer(target: self,action: #selector(gesturerStudy.swipAction(_:)))

    swipGesture.delegate = self;
    // 设置滑动方向
    swipGesture.direction = UISwipeGestureRecognizerDirection.Right;

    redView.addGestureRecognizer(swipGesture)

    //长按手势
    let longpressGesutre = UILongPressGestureRecognizer(target: self, action: #selector(gesturerStudy.handleLongpressGesture(_:)))
    //长按时间为1秒
    longpressGesutre.minimumPressDuration = 1
    //允许15秒运动
    longpressGesutre.allowableMovement = 15
    //所需触摸1次
    longpressGesutre.numberOfTouchesRequired = 1
    self.view.addGestureRecognizer(longpressGesutre)

    // 拖动手势
    let panGes = UIPanGestureRecognizer(target: self,action: #selector(gesturerStudy.panGesAction(_:)))

    panGes.delegate = self;

    self.view.addGestureRecognizer(panGes)

    // 缩放手势
    let gesture = UIPinchGestureRecognizer(target: self, action: #selector(gesturerStudy.viewPinch(_:)))

    self.redView.addGestureRecognizer(gesture)

    // 旋转手势
    let roteGes = UIRotationGestureRecognizer(target: self, action: #selector(gesturerStudy.viewRotation(_:)))

    self.redView.addGestureRecognizer(roteGes)

}

// 旋转事件 func viewRotation(sender: UIRotationGestureRecognizer) { self.redView.transform = CGAffineTransformMakeRotation(sender.rotation) }

func viewPinch(sender: UIPinchGestureRecognizer) {

    let _width = self.redView.bounds.width
    let _height = self.redView.bounds.height
    self.redView.bounds.size = CGSize(width: _width * sender.scale, height: _height * sender.scale)
}

// 拖动手势

func panGesAction(sender:UIPanGestureRecognizer) {
    let _transX = sender.translationInView(self.redView).x
    let _transY = sender.translationInView(self.redView).y

    self.redView.transform = CGAffineTransformMakeTranslation(_transX, _transY)

}

//长按手势
func handleLongpressGesture(sender : UILongPressGestureRecognizer){

    if sender.state == UIGestureRecognizerState.Began{
        //创建警告
        print("长按");
    }
}

func tapAction(tap:UITapGestureRecognizer){

  print("单击手势")
}

func swipAction(swip:UISwipeGestureRecognizer){

    print("滑动手势")

}

这都是系统的提供的手势API 做的,但是系统提供的也不是万能的,总有不尽人意和不能瞒住实际需求的地方,有时候,就需要自定义手势,下面来看看自定义手势

新建一个类继承 UIGestureRecognizer,引入UIGestureRecognizerSubclass,否则将不能这么做,

import UIKit

import UIKit.UIGestureRecognizerSubclass

class CustomGestureRecognizer: UIGestureRecognizer {

var left = false;

var right = false;

override init(target: AnyObject?, action: Selector) {
    super.init(target: target, action: action)
}

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

}

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

    let myTouch = touches.first! as UITouch

    let myLocation = myTouch.locationInView(self.view)

    if myLocation.x < 10 xss=removed> self.view?.bounds.width  {
       // 已滑到右边,停止滑动
        right = true
    }

    if left && right {
        self.state = UIGestureRecognizerState.Ended
    }

}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
    self.reset()
}

}

至此,已经定义完成,这里定义的是一个从左滑到右的自定义手势,这里下面我们就来调用一下这个自定义的手势

// 自定义手势 let customGes = CustomGestureRecognizer(target: self, action: #selector(gesturerStudy.viewCustom(_:)))

    self.redView.addGestureRecognizer(customGes)

// 自定义手势处理 func viewCustom(sender: CustomGestureRecognizer) { print(" 需要的处理 逻辑") }

ok,到现在为止,手势已经基本学习完毕,其实这也只是冰山一角,能做的能学的还有很多,这里仅是抛砖引玉,学习的路上大家一起进步,欢迎大神指教。

关注公众号 qiansvv 回复 源码获取更多源码

收藏
1
sina weixin mail 回到顶部