IOS 开发之swift中手势的实例详解

来源:爱站网时间:2020-06-13编辑:网友分享
在这个IOS应用程序开发教程中,我们打算实现手势识别,众所周知,IOS支持大量手势操作,可以提供良好的应用控制和良好的用户体验,IOS 开发之swift中手势的实例详解,大家都了解吗?一起跟着爱站技术频道小编来了解一下吧。

在这个IOS应用程序开发教程中,我们打算实现手势识别,众所周知,IOS支持大量手势操作,可以提供良好的应用控制和良好的用户体验,IOS 开发之swift中手势的实例详解,大家都了解吗?一起跟着爱站技术频道小编来了解一下吧。

IOS 开发之swift中手势的实例详解

手势操作主要包括如下几类

 

 

手势 属性 说明
点击 UITapGestureRecognizer numberOfTapsRequired:点击的次数;numberOfTouchesRequired:点击时有手指数量 设置属性 numberOfTapsRequired 可以实现单击,或双击的效果
滑动 UISwipeGestureRecognizer direction:滑动方向 direction 滑动方向分为上Up、下Down、左Left、右Right
拖动 UIPanGestureRecognizer   在拖动过程中,通过方法 translationInView 获取拖动时的位移
长按 UILongPressGestureRecognizer minimumPressDuration:长按最少时间  
旋转 UIRotationGestureRecognizer    
缩放 UIPinchGestureRecognizer    

 

注意:手势效果在实施过程中,存在几种状态:

* Began
* Ended
* Cancelled
* Failed
* Possible

手势效果图

代码示例

// 点击
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手势操作-单指单击手势";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手势
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapClick:"))
tapRecognizer.numberOfTapsRequired = 1
tapRecognizer.numberOfTouchesRequired = 1
label.userInteractionEnabled = true
label.addGestureRecognizer(tapRecognizer)

// 手势响应方法
func tapClick(recognizer:UITapGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel
    label.textColor = UIColor.redColor()
}

// 滑动
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手势操作-左滑手势";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手势
let swipeLeftRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipeLeftClick:"))
swipeLeftRecognizer.direction = .Left
swipeLeftRecognizer.numberOfTouchesRequired = 1
label.userInteractionEnabled = true
label.addGestureRecognizer(swipeLeftRecognizer)

// 手势响应方法
func swipeLeftClick(recognizer:UISwipeGestureRecognizer)
{
    let label = recognizer.view
    label!.backgroundColor = UIColor.orangeColor()
}

 

// 拖动
let label = UILabel(frame: CGRectMake(10.0, 10.0, 100.0, 100.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手势操作-拖动手势";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手势
let panRecognizer = UIPanGestureRecognizer(target: self, action: Selector("panClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(panRecognizer)

var pointValue:CGPoint! = CGPointZero // 移动
// 手势响应方法
func panClick(recognizer:UIPanGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    let point = recognizer.translationInView(label)
    print("pan point = \(point)")

    // 移动
    label.transform = CGAffineTransformMakeTranslation(point.x + self.pointValue.x, point.y + self.pointValue.y)

    if recognizer.state == .Began
    {
      label.backgroundColor = UIColor.yellowColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()

      self.pointValue.x += point.x
      self.pointValue.y += point.y
    }
}

// 缩放
let label = UILabel(frame: CGRectMake(10.0, 10.0, 200.0, 200.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手势操作-捏合手势";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手势
let pinchRecognizer = UIPinchGestureRecognizer(target: self, action: Selector("pinchClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(pinchRecognizer)

// 手势响应方法
var scaleValue:CGFloat! = 1.0 // 缩放
func pinchClick(recognizer:UIPinchGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    let scale = recognizer.scale;
    if scale > 1.0
    {
      // 放大
      label.transform = CGAffineTransformMakeScale(self.scaleValue + scale - 1.0, self.scaleValue + scale - 1.0)
    }
    else
    {
      // 缩小
      label.transform = CGAffineTransformMakeScale(self.scaleValue * scale, self.scaleValue * scale)
    }

    if recognizer.state == .Began
    {
      label.backgroundColor = UIColor.greenColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()

      if scale > 1.0
      {
        self.scaleValue = self.scaleValue + scale - 1.0;
      }
      else
      {
        self.scaleValue = self.scaleValue * scale
      }
    }
}

// 旋转
let label = UILabel(frame: CGRectMake(10.0, 10.0, 200.0, 200.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手势操作-旋转手势";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手势
let rotationRecognizer = UIRotationGestureRecognizer(target: self, action: Selector("ratotionClick:"))
label.userInteractionEnabled = true
label.addGestureRecognizer(rotationRecognizer)

var rotationValue:CGFloat! = 1.0 // 旋转
// 手势响应方法
func ratotionClick(recognizer:UIRotationGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    let rotation = recognizer.rotation
    label.transform = CGAffineTransformMakeRotation(rotation + self.rotationValue)

    if recognizer.state == .Began
    {
      label.backgroundColor = UIColor.greenColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()

      self.rotationValue = self.rotationValue + rotation
    }
}

// 长按
let label = UILabel(frame: CGRectMake(10.0, 10.0, (self.view.frame.size.width - 10.0 * 2), 60.0))
self.view.addSubview(label)
label.backgroundColor = UIColor.lightGrayColor()
label.text = "手势操作-长按手势";
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .Center;
// 添加手势
let pressRecognizer = UILongPressGestureRecognizer(target: self, action: Selector("pressClick:"))
    pressRecognizer.minimumPressDuration = 3.0
    label.userInteractionEnabled = true
    label.addGestureRecognizer(pressRecognizer)

// 手势响应方法
func pressClick(recognizer:UILongPressGestureRecognizer)
{
    let label:UILabel = recognizer.view as! UILabel

    if recognizer.state == .Began
    {
      let alertView = UIAlertView(title: nil, message: "长按响应", delegate: nil, cancelButtonTitle: "知道了")
      alertView.show()

      label.backgroundColor = UIColor.orangeColor()
      self.view.bringSubviewToFront(label)
    }
    else if recognizer.state == .Ended
    {
      label.backgroundColor = UIColor.lightGrayColor()
    }
}

看完以上爱站技术频道小编的讲述,大家都知道IOS 开发之swift中手势的实例详解了吧,希望可以帮助到大家。

上一篇:IOS实现聊天界面底部菜单栏效果

下一篇:iOS中最全的各种定时器使用教程

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载