[英]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 {
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
}
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 {
$0.translatesAutoresizingMaskIntoConstraints = false
view.addSubview($0)
}
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.