成都创新互联网站制作重庆分公司

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

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

创新互联是一家集网站建设,林口企业网站建设,林口品牌网站建设,网站定制,林口网站建设报价,网络营销,网络优化,林口网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

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


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

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

* Began
* Ended
* Cancelled
* Failed
* Possible

手势效果图

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

代码示例

// 点击
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中手势的实例详解
转载来源:http://cxhlcq.com/article/godohh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部