swift 拖动view代码

前端之家收集整理的这篇文章主要介绍了swift 拖动view代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

将一个View拖动到SB,创建一个redView继承自UIView,

//
// RedView.swift
// DragView
//
// Created by luoriver on 2017/2/15.
// Copyright © 2017年 SocererGroup. All rights reserved.
//

import UIKit

class RedView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

    override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
        //获取手指
        let touch = (touches as NSSet).anyObject() as! UITouch
        let nowLocation = touch.location(in: self)
        let preLocation = touch.prevIoUsLocation(in: self)

        //获取两个手指的偏移量
        let offsetX = nowLocation.x - preLocation.x
        let offsetY = nowLocation.y - preLocation.y

        var center = self.center
        center.x += offsetX
        center.y += offsetY

        self.center = center
    }

}
原文链接:https://www.f2er.com/swift/322156.html

猜你在找的Swift相关文章