美文网首页
手势拖拽 view 教程

手势拖拽 view 教程

作者: sing_crystal | 来源:发表于2016-12-12 11:31 被阅读355次

    原文链接
    作者:Arthur Knopper
    原文日期:2016-11-07
    译者:Crystal Sun

    iOS 的优势在于可以触摸和手势交互。在本节教程中,我们会展示一些借助手势识别器来进行拖拽的自定义视图(views),�本节教程使用 Xcode 8 和 iOS 10.

    打开 Xcode,创建一个 Single View Application。

    点击 Next,product name 一栏填写 IOS10DraggingViewsTutorial,填写好 Organization Name 和 Organization Identifier,Language 选择 Swift,Devices 选择 iPhone。

    首先,先创建随机出现在屏幕上的 views,菜单栏选择 File->New->File->iOS->Cocoa Touch Class,创建类文件,命名为 MyView,Subclass 选择 UIView。

    找到 MyView.swift 文件,添加下列属性:

    var lastLocation = CGPoint(x: 0, y: 0)
    

    这个变量记录用户触摸的最后位置。接下来实现 init 方法。

    override init(frame: CGRect) {
        super.init(frame: frame)
            
        // 初始化代码
        let panRecognizer = UIPanGestureRecognizer(target:self, action:#selector(MyView.detectPan(_:)))
        self.gestureRecognizers = [panRecognizer]
            
        //view 的颜色随机显示
        let blueValue = CGFloat(Int(arc4random() % 255)) / 255.0
        let greenValue = CGFloat(Int(arc4random() % 255)) / 255.0
        let redValue = CGFloat(Int(arc4random() % 255)) / 255.0
            
        self.backgroundColor = UIColor(red:redValue, green: greenValue, blue: blueValue, alpha: 1.0)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    首先给 view 添加一个 pan gesture recognizer(手势识别器),这样就可以点击选中并拖拽 view 到新的位置。接下来创建随机颜色,作为 view 的背景色。实现 detectPan 方法,每次识别到手势后,都会调用 detectPan 方法。

    func detectPan(_ recognizer:UIPanGestureRecognizer) {
        let translation  = recognizer.translation(in: self.superview)
        self.center = CGPoint(x: lastLocation.x + translation.x, y: lastLocation.y + translation.y)
    }
    

    translation 变量检测到新的坐标值,view 的中心将根据改变后的坐标值做出相应调整。当用户点击 view 时,调用 touchesBegan:event 方法,下面就来实现此方法。

    override func touchesBegan(_ touches: (Set<UITouch>!), with event: UIEvent!) {
        // 把当前被选中的 view 放到前面
        self.superview?.bringSubview(toFront: self)
          
        // 记住原来的位置
        lastLocation = self.center
    }
    

    选中某个 view 后,这个 view 会出现在其他 view 的前面,其中心位置的坐标值就是 lastlocation 变量值。现在,自定义的 view 差不多完成了,移植到 view controller 上吧。在 ViewController.swift 文件中实现 viewDidLoad 方法

    override func viewDidLoad() {
        super.viewDidLoad()
            
        let halfSizeOfView = 25.0
        let maxViews = 25
        let insetSize = self.view.bounds.insetBy(dx: CGFloat(Int(2 * halfSizeOfView)), dy: CGFloat(Int(2 * halfSizeOfView))).size
            
        // 添加 views
        for _ in 0..<maxViews {
            let pointX = CGFloat(UInt(arc4random() % UInt32(UInt(insetSize.width))))
            let pointY = CGFloat(UInt(arc4random() % UInt32(UInt(insetSize.height))))
                
            let newView = MyView(frame: CGRect(x: pointX, y: pointY, width: 50, height: 50))
            self.view.addSubview(newView)
        }
    }
    

    有 25 个 50*50 的 view 随机地出现在主界面上,运行工程,点击并拖动一个 view,这个 view 会一直在其他 view 上面。

    从 ioscreator 的 github 上可以下载到本节课程 IOS10DraggingViewsTutorial 的源代码。

    本文由 SwiftGG 翻译组翻译,已经获得作者翻译授权。

    相关文章

      网友评论

          本文标题:手势拖拽 view 教程

          本文链接:https://www.haomeiwen.com/subject/skondttx.html