我在开源项目上工作 - 对象文档映射器又名。 import UIKit
import PlaygroundSupport
protocol DragImageDelegate: class {
func dragimage(_ dragImage:DragImage, didDropAt position:CGPoint)
}
class DragImage: UIImageView {
weak var delegate: DragImageDelegate?
var originalPos : CGPoint!
var dropTarget: UIView?
override init (frame : CGRect) {
super.init(frame: frame)
isUserInteractionEnabled = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches : Set<UITouch>,with event: UIEvent?){
originalPos = self.center
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first{
let position = touch.location(in: self.superview)
self.center = CGPoint(x : position.x, y : position.y)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first, let target = dropTarget {
let position = touch.location(in: self.superview)
if target.frame.contains(position){
print(self.center.x, self.center.y)
guard let delegate = self.delegate else {
print("delegate not set")
return
}
print(self.center.x, self.center.y)
delegate.dragimage(self, didDropAt: position)
return
}
}
self.center = originalPos
}
}
class MyVC: UIViewController, DragImageDelegate {
let dragImage1 = DragImage(frame: CGRect(x: 0.0, y: 0.0, width: 30.0, height: 30.0))
let dragImage2 = DragImage(frame: CGRect(x: 0.0, y: 100.0, width: 30.0, height: 30.0))
override func viewDidLoad() {
let target = UIView(frame: CGRect(x: 200.0, y: 400.0, width: 30.0, height: 30.0))
target.backgroundColor = .black
view.addSubview(target)
dragImage1.backgroundColor = .white
dragImage2.backgroundColor = .white
dragImage1.dropTarget = target
dragImage2.dropTarget = target
view.addSubview(dragImage1)
view.addSubview(dragImage2)
dragImage1.delegate = self
dragImage2.delegate = self
}
private func move(_ view:UIView, to position:CGPoint) {
view.frame = CGRect(x: position.x, y: position.y, width: view.frame.size.width, height: view.frame.size.height)
}
// MARK: - DragImageDelegate
func dragimage(_ dragImage: DragImage, didDropAt position: CGPoint) {
if dragImage === dragImage1 {
move(dragImage1, to: position)
} else if dragImage === dragImage2 {
move(dragImage2, to: position)
}
}
}
var container = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 300.0, height: 600.0))
let myVc = MyVC()
myVc.view.frame = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 600.0)
myVc.view.backgroundColor = .green
container.addSubview(myVc.view)
PlaygroundPage.current.liveView = container
中的ODM
我面临着一个我努力的设计决策(而不是javascript
而不是simple
)。
在easy
中,您可以定义一个ODM
对象,该对象在数据库中描述您的数据文档。创建新的Model
实例对象时,可以通过参数传递数据值
伪代码:
Model
现在,我面临的决定是在应用程序(ODM)处理模型之前是否自动克隆模型中的var data = {
username: 'james'
email: 'james@email.com',
country: {
code: 'US',
city: ''
}
};
// In case the data object would not be cloned, it would be mutated by the ODM
var user = new UserModel(data);
对象。我倾向于选择不克隆输入数据。我觉得在data
社区中克隆数据非常受欢迎,因为它可以为最终用户制作javascript
(而不是easy
)。从我的研究中我发现一些类似和受欢迎的项目决定克隆数据,但我不确定这是一个正确的选择,因为一个模型可以有大的&amp;复杂的数据模式虽然在几十个simple
实例对象的情况下没有任何区别,但我认为在处理例如:数百个Model
个对象时可能会出现问题。
我很想听听你对这个主题的推理以及你的选择是什么? 如果您已经看到另一个答案,请不要犹豫,写下您的想法! 谢谢!
答案 0 :(得分:0)
如果继续使用输入对象来保存实体的数据,如果不克隆对象,则会遇到如下问题:
COLOR_BayerBG2BGR_VNG
这可能会导致一些令人讨厌的错误,这些错误可能并不总是很明显。
而不是克隆,我认为最干净的方法是从对象中提取相关信息并将其直接保存为成员属性:
COLOR_BayerBG2BGR_EA
更好的是,显式地将属性声明为构造函数的参数:
var UserModel = function(data) {
this.data = data;
}
UserModel.prototype.getUsername = function() {
return this.data.username;
}
var data = {username: 'james'};
var james = new UserModel(data);
data.username = 'john';
var john = new UserModel(data);
console.log(james.getUsername()); // "john"
console.log(john.getUsername()); // "john"