从子视图复制对象然后放置对象
copy object from a subview then place object
我希望我的 swift 代码复制棕色视图的对象。所以用户点击复制按钮,然后用户点击他们想要复制的对象,然后他们指向他们想要放置对象的区域。基本上下面的 gif 正是我所看到的我不知道你是否会做类似这样的克隆。
import UIKit;import CoreData
class ViewController: UIViewController {
var addBoxes = [UIImageView]()
let subview = UIImageView()
var currentView: UIView?
var box = UIButton()
var copyButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[box,copyButton].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
}
box.backgroundColor = .red
box.setTitle("Add", for: .normal)
copyButton.setTitle("Copy", for: .normal)
NSLayoutConstraint.activate([
box.leadingAnchor.constraint(equalTo: copyButton.trailingAnchor),
box.bottomAnchor.constraint(equalTo: view.bottomAnchor),
box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
box.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
copyButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
copyButton.bottomAnchor.constraint(equalTo: view.bottomAnchor),
copyButton.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
copyButton.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
])
copy.addTarget(self, action: #selector(copyBox), for: .touchDown)
box.addTarget(self, action: #selector(addQuarter), for: .touchDown)
copyButton.backgroundColor = .blue
}
override var prefersStatusBarHidden: Bool {
return true
}
@objc func copyBox() {
}
@objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
currentView = gesture.view
let draggedView = gesture.view!
view.bringSubviewToFront(draggedView)
let translation = gesture.translation(in: view)
draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
gesture.setTranslation(.zero, in: view)
}
@objc func addQuarter(){
let subview = UIImageView()
subview.isUserInteractionEnabled = true
addBoxes.append(subview)
view.addSubview(subview)
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))
subview.addGestureRecognizer(pan)
subview.frame = CGRect(x: view.bounds.midX , y: view.bounds.midY + CGFloat(100), width: CGFloat(100), height: 100)
subview.backgroundColor = .brown
addBoxes.append(subview)
}
}
一种实现方式copy
。
您可以在 Model
级别完成。
你的情况,复制View,就是获取View 属性 ( Frame Size, Color ),
然后创建一个新的。
这里是代码说明
模型部分
// you can add info , in your needs
struct ViewModel{
let color: UIColor
let size: CGSize
}
逻辑部分:
向基础视图添加一个UITapGestureRecognizer
(添加部分视图)
在复制模式下,视图isUserInteractionEnabled
false,
在添加模式下,视图 isUserInteractionEnabled
true,
var selectModel: ViewModel?
@objc func copyBox() {
// now in the copy mode
}
func selectView(){
// get current model
selectModel = ViewModel(color: yourView.backgroundColor, size: yourView.frame.size)
}
@objc func AddBox() {
// now in the add mode / paste mode
}
func touchToAdd(_ gesture: UITapGestureRecognizer){
// get the tap location,
// the location point is the new view's center
// use the model from `func selectView(){`
// now you got center . frame size 、color, enough to create a new one ( a copy)
}
通过代码实现您的要求
class ViewController: UIViewController {
var addBoxes = [UIImageView]()
let subview = UIImageView()
var currentView: UIView?
var location: CGPoint = CGPoint.zero
var box = UIButton()
var copyButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[box,copyButton].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
}
box.backgroundColor = .red
box.setTitle("Add", for: .normal)
copyButton.setTitle("Copy", for: .normal)
NSLayoutConstraint.activate([
box.leadingAnchor.constraint(equalTo: copyButton.trailingAnchor),
box.bottomAnchor.constraint(equalTo: view.bottomAnchor),
box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
box.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
copyButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
copyButton.bottomAnchor.constraint(equalTo: view.bottomAnchor),
copyButton.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
copyButton.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
])
copyButton.addTarget(self, action: #selector(copyBox), for: .touchDown)
box.addTarget(self, action: #selector(addQuarter), for: .touchDown)
copyButton.backgroundColor = .blue
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:))))
}
override var prefersStatusBarHidden: Bool {
return true
}
@objc func copyBox() {
if let copyObject = currentView?.copyView as? UIImageView{
copyObject.center = location
copyObject.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:))))
view.addSubview(copyObject)
}
}
@objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
currentView = gesture.view
let draggedView = gesture.view!
view.bringSubviewToFront(draggedView)
let translation = gesture.translation(in: view)
draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
gesture.setTranslation(.zero, in: view)
}
@objc func handleTapGestured(_ gesture: UITapGestureRecognizer) {
if self.view == gesture.view{
let loc = gesture.location(in: self.view)
location = loc
}
}
@objc func addQuarter(){
let subview = UIImageView()
subview.isUserInteractionEnabled = true
addBoxes.append(subview)
view.addSubview(subview)
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))
subview.addGestureRecognizer(pan)
subview.frame = CGRect(x: view.bounds.midX , y: view.bounds.midY + CGFloat(100), width: CGFloat(100), height: 100)
subview.backgroundColor = .brown
addBoxes.append(subview)
}
}
extension UIView{
var copyView : UIView?{
if let archiv = try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false){
if let view = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archiv) as? UIView{
return view
}
}
return nil
}
}
我希望我的 swift 代码复制棕色视图的对象。所以用户点击复制按钮,然后用户点击他们想要复制的对象,然后他们指向他们想要放置对象的区域。基本上下面的 gif 正是我所看到的我不知道你是否会做类似这样的克隆。
import UIKit;import CoreData
class ViewController: UIViewController {
var addBoxes = [UIImageView]()
let subview = UIImageView()
var currentView: UIView?
var box = UIButton()
var copyButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[box,copyButton].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
}
box.backgroundColor = .red
box.setTitle("Add", for: .normal)
copyButton.setTitle("Copy", for: .normal)
NSLayoutConstraint.activate([
box.leadingAnchor.constraint(equalTo: copyButton.trailingAnchor),
box.bottomAnchor.constraint(equalTo: view.bottomAnchor),
box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
box.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
copyButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
copyButton.bottomAnchor.constraint(equalTo: view.bottomAnchor),
copyButton.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
copyButton.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
])
copy.addTarget(self, action: #selector(copyBox), for: .touchDown)
box.addTarget(self, action: #selector(addQuarter), for: .touchDown)
copyButton.backgroundColor = .blue
}
override var prefersStatusBarHidden: Bool {
return true
}
@objc func copyBox() {
}
@objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
currentView = gesture.view
let draggedView = gesture.view!
view.bringSubviewToFront(draggedView)
let translation = gesture.translation(in: view)
draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
gesture.setTranslation(.zero, in: view)
}
@objc func addQuarter(){
let subview = UIImageView()
subview.isUserInteractionEnabled = true
addBoxes.append(subview)
view.addSubview(subview)
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))
subview.addGestureRecognizer(pan)
subview.frame = CGRect(x: view.bounds.midX , y: view.bounds.midY + CGFloat(100), width: CGFloat(100), height: 100)
subview.backgroundColor = .brown
addBoxes.append(subview)
}
}
一种实现方式copy
。
您可以在 Model
级别完成。
你的情况,复制View,就是获取View 属性 ( Frame Size, Color ),
然后创建一个新的。
这里是代码说明
模型部分
// you can add info , in your needs
struct ViewModel{
let color: UIColor
let size: CGSize
}
逻辑部分:
向基础视图添加一个UITapGestureRecognizer
(添加部分视图)
在复制模式下,视图isUserInteractionEnabled
false,
在添加模式下,视图 isUserInteractionEnabled
true,
var selectModel: ViewModel?
@objc func copyBox() {
// now in the copy mode
}
func selectView(){
// get current model
selectModel = ViewModel(color: yourView.backgroundColor, size: yourView.frame.size)
}
@objc func AddBox() {
// now in the add mode / paste mode
}
func touchToAdd(_ gesture: UITapGestureRecognizer){
// get the tap location,
// the location point is the new view's center
// use the model from `func selectView(){`
// now you got center . frame size 、color, enough to create a new one ( a copy)
}
通过代码实现您的要求
class ViewController: UIViewController {
var addBoxes = [UIImageView]()
let subview = UIImageView()
var currentView: UIView?
var location: CGPoint = CGPoint.zero
var box = UIButton()
var copyButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
[box,copyButton].forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
view.addSubview([=10=])
}
box.backgroundColor = .red
box.setTitle("Add", for: .normal)
copyButton.setTitle("Copy", for: .normal)
NSLayoutConstraint.activate([
box.leadingAnchor.constraint(equalTo: copyButton.trailingAnchor),
box.bottomAnchor.constraint(equalTo: view.bottomAnchor),
box.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
box.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
copyButton.leadingAnchor.constraint(equalTo: view.leadingAnchor),
copyButton.bottomAnchor.constraint(equalTo: view.bottomAnchor),
copyButton.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.04),
copyButton.widthAnchor.constraint(equalTo:view.widthAnchor ,multiplier: 0.5),
])
copyButton.addTarget(self, action: #selector(copyBox), for: .touchDown)
box.addTarget(self, action: #selector(addQuarter), for: .touchDown)
copyButton.backgroundColor = .blue
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapGestured(_:))))
}
override var prefersStatusBarHidden: Bool {
return true
}
@objc func copyBox() {
if let copyObject = currentView?.copyView as? UIImageView{
copyObject.center = location
copyObject.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:))))
view.addSubview(copyObject)
}
}
@objc func handlePanGestured(_ gesture: UIPanGestureRecognizer) {
currentView = gesture.view
let draggedView = gesture.view!
view.bringSubviewToFront(draggedView)
let translation = gesture.translation(in: view)
draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
gesture.setTranslation(.zero, in: view)
}
@objc func handleTapGestured(_ gesture: UITapGestureRecognizer) {
if self.view == gesture.view{
let loc = gesture.location(in: self.view)
location = loc
}
}
@objc func addQuarter(){
let subview = UIImageView()
subview.isUserInteractionEnabled = true
addBoxes.append(subview)
view.addSubview(subview)
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGestured(_:)))
subview.addGestureRecognizer(pan)
subview.frame = CGRect(x: view.bounds.midX , y: view.bounds.midY + CGFloat(100), width: CGFloat(100), height: 100)
subview.backgroundColor = .brown
addBoxes.append(subview)
}
}
extension UIView{
var copyView : UIView?{
if let archiv = try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: false){
if let view = try? NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archiv) as? UIView{
return view
}
}
return nil
}
}