简体   繁体   English

使用UIPanGestureRecognizer移动后,保存并隔离UIImageView。 迅速

[英]Save and segue UIImageView after moving with UIPanGestureRecognizer. Swift

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: 在第一个屏幕上,我具有UIImageView ,可以使用UIPanGestureRecognizer函数移动UIPanGestureRecognizer

@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 这是移动代码的常用代码,我在raywenderlich.com拍摄

So what I want is to save position of the UIImageView element after interaction with it and after segue. 所以我想要的是在与UIImageView元素交互之后以及在Segue之后保存UIImageView元素的位置。

I believe that I need to return CGPoint from this function and segue it like so: 我相信我需要从此函数返回CGPoint ,然后像这样进行隔离:

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? 我是否使用正确的代码通过Pan Gesture Recognizer移动对象?

  2. How would you return CGPoint from this function and how would you read it? 您将如何从该函数返回CGPoint以及如何阅读?

  3. How would I save UIImageView's position in process of segue and after? 在后续过程中和以后如何保存UIImageView的位置?

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 :( 我敢打赌,这很容易解决,但就我是初学者而言,我还不知道如何:(

From your description there are several solutions, my would be. 根据您的描述,有几种解决方案,我会的。

maintain a property currentCenter in the firstViewController firstViewController维护属性currentCenter

var currentCenter:CGPoint

In method movement add the following line after setting view.center 在方法movement ,在设置view.center之后添加以下行

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

and then prepareForSegue as you said. 然后像你说的那样准备prepareForSegue

Hope this approach helps! 希望这种方法有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM