我有一个按钮,它根据下面所示的 Clip 类创建其他按钮。这些新创建的按钮将添加到数组中并存储在 plist 中。
class Clip: Encodable, Decodable {
var name: String = ""
var xCoordinate: Int = 100
var yCoordinate: Int = 300
// more parameter will be added later on e.g color, scale etc..
}
每个按钮都可以在 View 中移动,新的 x 和 y 坐标存储在 plist 中。
@objc func handlePan(sender: UIPanGestureRecognizer){
let uIViewSelected = sender.view!
switch sender.state {
case .began, .changed :
moveViewWithPan(view: uIViewSelected, sender: sender)
break
case .ended:
//Finds the position when the button is no longer being dragged
let x = Int(uIViewSelected.center.x)
let y = Int(uIViewSelected.center.y)
//clipArray[0] need to be the corresponding clicked button e.g clipArray[2]
clipArray[0].xCoordinate = x
clipArray[0].yCoordinate = y
saveData()
break
default:
break
}
}
以上仅当我创建一个按钮时才有效。当添加更多按钮时,上面的行仅更改数组中的第一个剪辑。我需要一种方法将值更新为单击的正确按钮。
当我以编程方式创建所有按钮时,如何识别单击按钮的数组位置?目前我将 ClipArray 的值设置为 0。
clipArray[0].xCoordinate = x
clipArray[0].yCoordinate = y
我什至不确定使用 plist 是否是存储按钮的最佳方式。
任何帮助或文档将不胜感激。 谢谢
最佳答案
根据 dfd 响应,我向创建的每个按钮添加了标签,现在解决了问题。
let x = Int(uIViewSelected.center.x)
let y = Int(uIViewSelected.center.y)
//clipArray.append(clip)
var tagNo = uIViewSelected.tag
clipArray[tagNo].xCoordinate = x
clipArray[tagNo].yCoordinate = y
关于arrays - Swift - 更新和存储多个以编程方式创建的按钮的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52866748/