0

Good day.

This is my last option — to ask here.

The problem is: I am building multiply view app. On the first screen I have UIImageView that I can move with UIPanGestureRecognizer function:

@IBAction func movement(recognizer: UIPanGestureRecognizer) {

    let translation = recognizer.translationInView(self.view)

    if let view = recognizer.view {

        view.center = CGPoint(x:view.center.x + translation.x, y:view.center.y + translation.y)

    }

    recognizer.setTranslation(CGPointZero, inView: self.view)
}

It's a usual code to move things around, I took at raywenderlich.com

So what I want is to save position of the UIImageView element after interaction with it and after segue.

I believe that I need to return CGPoint from this function and segue it like so:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showPlaylistDetail" {
            let playlistDetailController = segue.destinationViewController as! PlaylistDetailViewController

playlistDetailController.image2.center = image1.center

        }
    }

So my question(s) is(are):

  1. Do I use right code to move an object with Pan Gesture Recognizer?

  2. How would you return CGPoint from this function and how would you read it?

  3. How would I save UIImageView's position in process of segue and after?

huh... This little bug is killing me. I bet, it is easy to solve, but as far as I am beginner I have no idea how :(

1 Answers1

0

From your description there are several solutions, my would be.

maintain a property currentCenter in the firstViewController

var currentCenter:CGPoint

In method movement add the following line after setting view.center

currentCenter.x = view.center.x
currentCenter.y = view.center.y

and then prepareForSegue as you said.

Hope this approach helps!

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
  • Thank you for the answer. But it does not seems right. As I get from this answer ( http://stackoverflow.com/questions/24014800/property-synthesize-equivalent-in-swift ) there is no @property in Swift. So do you suggest to use global variable? – Konstantin Bashenko May 06 '15 at 10:51
  • yes, and If you have a single `imageView` that you are moving, you can directly pass `thatImageView.center`. – Vivek Molkar May 06 '15 at 10:53
  • It does not work. Image comes right to it's initial position after segue starts. – Konstantin Bashenko May 06 '15 at 10:56
  • you need to use same approach. declare a variable in second view controller. in your `prepareForSegue` assign `currentCenter` to that variable. and In `viewDidLoad` in second view controller assign the variable to `imageview.center`. – Vivek Molkar May 06 '15 at 11:02