我正在以编程方式创建一个新按钮,可以将其拖动到视图中的所需位置。我想将新坐标保存到我的核心数据中,但是在重载时,clip.xCoordinate和clip.yCoordinate的值返回0。
下面是我要保存新位置的坐标并且要保存X和Y轴位置的位置。是否需要确定要编辑哪个按钮?还是uIViewSelected.frame.midX?
@objc func handlePan(sender: UIPanGestureRecognizer)
{
let uIViewSelected = sender.view!
let clip = Clip(context: self.context) //Core Data
switch sender.state {
case .began, .changed :
moveViewWithPan(view: uIViewSelected, sender: sender)
break
case .ended:
clip.xCoordinate = uIViewSelected.frame.midX
clip.yCoordinate = uIViewSelected.frame.midY
self.saveClip()
break
default:
break
}
}
在核心数据中,如果有帮助,我将分别保存x和y坐标。 - 名称 -x坐标 -yCoordinate
如果任何人都有解决方案,或者可以向我指出正确的方向,那就太好了!谢谢
编辑
func saveClip() {
do {
try context.save()
} catch {
print ("Error Saving Context")
print(error)
}
}
func loadClip(with request: NSFetchRequest<Clip> = Clip.fetchRequest()) {
do {
clipArray = try context.fetch(request)
} catch {
print("Error Fetching data from context")
}
}
func moveViewWithPan(view: UIView, sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: self.view)
if let view = sender.view {
view.center = CGPoint(x: view.center.x + translation.x, y: view.center.y + translation.y)
}
sender.setTranslation(CGPoint.zero, in: view)
}