ios - Swift - StackView, panGesture, CoreData -
my question / issue relates saving new position of uistackview, coredata, after moving pangesture.
i’ve created uistackview via function createpersonstack() called viewdidload(), based on objects retrieved coredata.
at stage, below code works in respect creating , moving objects.
each uistackview has coredata person.firstname attribute text label (hopefully can used identify object save to)
i pull initial xpos , ypos (starting position) uistackview (person) coredata.
i new coordinates listed in print statement of guesture.state == .ended block of code.
if possible, saving new x & y coordinates (after gesture state .ended) coredata against correct person. i.e. when view launches again, updated new coordinates starting position of each member family. thanks
func createpersonstack() { let family = fetchedresultscontroller.fetchedobjects person in family! { _ = createpersonwithposition(name: person.firstname!, age: person.age!, xpos: cgfloat(person.xpos), ypos: cgfloat(person.ypos)) } } func createpersonwithposition(name: string, age: string, xpos: cgfloat, ypos: cgfloat) -> uistackview { //image view let imageview = uiimageview() imageview.widthanchor.constraint(equaltoconstant: 50.0).isactive = true imageview.heightanchor.constraint(equaltoconstant: 50.0).isactive = true imageview.image = uiimage(named: “person image”) //text label let textlabel = uilabel() textlabel.backgroundcolor = uicolor.clear textlabel.widthanchor.constraint(equaltoconstant: 50.0).isactive = true textlabel.heightanchor.constraint(equaltoconstant: 20.0).isactive = true textlabel.font = textlabel.font.withsize(10) textlabel.text = name textlabel.textalignment = .center //stack view let stackview = uistackview() stackview.axis = uilayoutconstraintaxis.vertical stackview.distribution = uistackviewdistribution.equalspacing stackview.alignment = uistackviewalignment.center stackview.spacing = 1.0 stackview.addarrangedsubview(imageview) stackview.addarrangedsubview(textlabel) stackview.translatesautoresizingmaskintoconstraints = false self.view.addsubview(stackview) //constraints stackview.centerxanchor.constraint(equalto: self.view.centerxanchor, constant: cgpoint(x: xpos, y: ypos).x).isactive = true stackview.centeryanchor.constraint(equalto: self.view.centeryanchor, constant: cgpoint(x: xpos, y: ypos).y).isactive = true let panguesture = uipangesturerecognizer(target: self, action: #selector(pan(guesture:))) stackview.addgesturerecognizer(panguesture) return stackview } // mark: - move people func pan(guesture: uipangesturerecognizer) { let translation = guesture.translation(in: self.view) if let guestureview = guesture.view { guestureview.center = cgpoint(x: guestureview.center.x + translation.x, y: guestureview.center.y + translation.y) if guesture.state == .ended { print("moved along x axis = \(guestureview.center.x - 512)") // equals x position in view center (ipad landscape orientation) print("moved along y axis = \(guestureview.center.y - 384)”) // equals y position in view center (ipad landscape orientation) } } guesture.settranslation(cgpoint.zero, in: self.view) }
Comments
Post a Comment