How to fix the final position of a texrtField after a Pangesture

Asked

Viewed 12 times

0

I have a little problem with my code. I have a view with two textFields, one at the top and the other at the bottom, both on a View image. I added a Pangesture function to allow the movement of these textFields after loading an image. Allowing text to be positioned on the image in the location the user chooses. The problem is that after using Pangesture and repositioning the textFields, the user can share the new image through an Activityview, however when activityView appears the textFields go back to the original position, as if the view were rebooted. How do I fix the textfields in the new final positions? What am I doing wrong or not doing? Do I need to touch the constraints? Follow the parts of my code to facilitate understanding...

//MARK: - PAN GESTURE RECOGNIZER

//Permiti a movimentação do textField superior por gesto.
@IBAction func didPanTfTop(_ sender: UIPanGestureRecognizer) {

    let translation = sender.translation(in: self.view)

    if sender.state == .began {
        print("Gesture began")
        textFieldOriginalCenter = tfTop.center
    } else if sender.state == .changed {
        print("Gesture is changed")
        tfTop.center = CGPoint(x: textFieldOriginalCenter.x + translation.x, y: textFieldOriginalCenter.y + translation.y)
    } else if sender.state == .ended {
        print("Gesture ended")

    }
}

//Permiti a movimentação do textField inferior por gesto.
@IBAction func didPanTfBottom(_ sender: UIPanGestureRecognizer) {

    let translation = sender.translation(in: self.view)

    if sender.state == .began {
        print("Gesture began")
        textFieldOriginalCenter = tfBottom.center
    } else if sender.state == .changed {
        print("Gesture is changed")
        tfBottom.center = CGPoint(x: textFieldOriginalCenter.x + translation.x, y: textFieldOriginalCenter.y + translation.y)
    } else if sender.state == .ended {
        print("Gesture ended")

    }
}

}

Here follows the activityView

    //Aciona uma activeView para compartilhar o Meme
@IBAction func share(_ sender: UIBarButtonItem) {
    Feedback.share.hapticFeedback()
    let memeImage = generateMemedImage()
    let activityView = UIActivityViewController(activityItems: [memeImage], applicationActivities: nil)
    activityView.completionWithItemsHandler = { (activityType: UIActivity.ActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) -> Void in

        if completed {
            self.save()
            print("Image has saved")
        }
    }
    present(activityView, animated: true, completion: nil)
}

1 answer

0


In the English version I received the reply of my friend Rob Ryan, which I transcribe here...

1- first we need to create the Constraints outlets

//TOP TextField Constraints outlets
@IBOutlet weak var topUpConstraint: NSLayoutConstraint!
@IBOutlet weak var topRightConstraint: NSLayoutConstraint!
@IBOutlet weak var topLeftConstraint: NSLayoutConstraint!

//BOTTOM TextField Constraints outlets
@IBOutlet weak var bottomDownConstraint: NSLayoutConstraint!
@IBOutlet weak var bottomRightConstraint: NSLayoutConstraint!
@IBOutlet weak var bottomLeftConstraint: NSLayoutConstraint!

2- For the upper textField we will present a simple solution, although it has some fragility

    //Solução simplificada, mas um pouco frágil, que permiti a movimentação do textField superior por gesto.

@IBAction func didPanTfTop(_ gesture: UIPanGestureRecognizer) {
    switch gesture.state {
        case .began:
            originalTransform = gesture.view!.transform
        case .changed, .ended:
            let translation = gesture.translation(in: gesture.view)
            gesture.view?.transform = originalTransform.translatedBy(x: translation.x, y: translation.y)
        default:
            break
    }
}

3- For the lower textField we will present a more complex and robust solution.

  //Solução mais robusta e segura que permiti a movimentação do textField inferior por gesto.
@IBAction func didPanTfBottom(_ gesture: UIPanGestureRecognizer) {
    switch gesture.state {
        case .began:
            originalBottomDownConstant = bottomDownConstraint.constant
            originalBottomRightConstant = bottomRightConstraint.constant
            originalBottomLeftConstant = bottomLeftConstraint.constant
        case .changed, .ended:
            let translation = gesture.translation(in: gesture.view)
            bottomDownConstraint.constant = originalBottomDownConstant + translation.y
            bottomRightConstraint.constant = originalBottomRightConstant + translation.x
            bottomLeftConstraint.constant = originalBottomLeftConstant + translation.x
        default:
            break
    }
}

That’s it... That way we fix the textFields after the Uipangesturereconizer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.