加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift 拖动view代码

发布时间:2020-12-14 06:26:47 所属栏目:百科 来源:网络整理
导读:将一个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 dr

将一个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
    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读