当先锋百科网

首页 1 2 3 4 5 6 7

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

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

注意:手势效果在实施过程中,存在几种状态:
* Began
* Ended
* Cancelled
* Failed
* Possible

手势效果图

代码示例

// 点击
let label = UILabel(frame: CGRectMake(, , (self.view.frame.size.width -  * ), ))
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 = 
tapRecognizer.numberOfTouchesRequired = 
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(, , (self.view.frame.size.width -  * ), ))
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 = 
label.userInteractionEnabled = true
label.addGestureRecognizer(swipeLeftRecognizer)

// 手势响应方法
func swipeLeftClick(recognizer:UISwipeGestureRecognizer)
{
        let label = recognizer.view
        label!.backgroundColor = UIColor.orangeColor()
}
// 拖动
let label = UILabel(frame: CGRectMake(, , , ))
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(, , , ))
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! =   // 缩放
func pinchClick(recognizer:UIPinchGestureRecognizer)
{
        let label:UILabel = recognizer.view as! UILabel

        let scale = recognizer.scale;
        if scale > 
        {
            // 放大
            label.transform = CGAffineTransformMakeScale(self.scaleValue + scale - , self.scaleValue + scale - )
        }
        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 > 
            {
                self.scaleValue = self.scaleValue + scale - ;
            }
            else
            {
                self.scaleValue = self.scaleValue * scale
            }
        }
}
// 旋转
let label = UILabel(frame: CGRectMake(, , , ))
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! =  // 旋转
// 手势响应方法
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(, , (self.view.frame.size.width -  * ), ))
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 = 
        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()
        }
}