工具扩展_8_EZSwiftExtensions_ View手势

  • 1.单击:
  • 2.长按:
  • 3.轻扫手势:
  • 4.拖拽手势:
  • 5.捏合手势:
import UIKit
class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        addLongPressGesture()
    }
    
    // 单击:
    func addTapGesture()
    {
        let mainview = UIView(x: 40, y: 80, w: 334, h: 334)
        mainview.backgroundColor = UIColor.orange
        self.view.addSubview(mainview)
        
        mainview.addTapGesture
        { (gesture) -> () in
            print("---1.单击手势:view tapped")
        }
    }
    
    // 长按:
    func addLongPressGesture()
    {
        let mainview = UIView(x: 40, y: 120, w: 334, h: 334)
        mainview.backgroundColor = UIColor.orange
        self.view.addSubview(mainview)
        
        mainview.addLongPressGesture
        { (long) -> () in
            print("---2.长按手势:view tapped")
            mainview.shakeViewForTimes(3) // 晃动3次;
            mainview.addShadow(offset: CGSize(width:10, height:10) , radius: 10, color: .brown, opacity: 0.75) // 添加阴影;
        }
    }
}
import UIKit
class ViewController: UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        addPinchGesture()
    }
    
    // 轻扫手势:
    func addSwipeGesture()
    {
        let mainview = UIView(x: 40, y: 80, w: 334, h: 334)
        mainview.backgroundColor = UIColor.orange
        self.view.addSubview(mainview)
        
        mainview.addSwipeGesture(direction: UISwipeGestureRecognizer.Direction.down)
        { (Swiped) -> () in
            print("---滑动的方向 \(Swiped.direction)--")
            print("---手指的数量 \(Swiped.numberOfTouchesRequired)--")
        }
    }
    
    // 拖拽手势:
    func addPanGesture()
    {
        let mainview = UIView(x: 40, y: 80, w: 150, h: 150)
        mainview.backgroundColor = UIColor.orange
        self.view.addSubview(mainview)
        
        var originP = mainview.center
        
        mainview.addPanGesture
        { (pan) -> () in
            if(pan.state == .began)
            {
                originP = mainview.center // 获得View的中心点;
            }
            
            // 获得拖拽手势在View中的移动距离, 通过计算得到View在经过拖拽后的目标位置:
            let distance = pan.translation(in: mainview)
            let result = CGPoint(x: originP.x+distance.x, y: originP.y+distance.y)
            
            mainview.center = result // View 跟随手指移动;
        }
    }
    
    // 捏合手势:
    func addPinchGesture()
    {
        let mainview = UIView(x: 40, y: 120, w: 334, h: 334)
        mainview.backgroundColor = UIColor.orange
        mainview.center = self.view.center
        self.view.addSubview(mainview)
        
        mainview.addPinchGesture
        { (pinch) -> () in
            print(pinch.scale) // 捏合比例;
            mainview.setScale(x: pinch.scale, y: pinch.scale) // 缩放;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值