将一个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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。