Search code examples
iosswiftxcodeanimationuiactivityviewcontroller

How to manage UIActivityViewController completion?


to share an image, i swipe it off screen.

What's happening now is:

  • i swipe the image
  • it presents the UIActivityViewController
  • i choose an activity
  • the activity shows up modally

if i cancel the activity:

  • the image is on screen (the view did load)
  • the image move back in

what i need:

  • i cancel the activity
  • the image is not on screen
  • the image move back in

How can i do this ?

here is my code:

        let activityViewController = UIActivityViewController(activityItems: [imageToShare], applicationActivities: nil)
    self.present(activityViewController, animated: true, completion: {
        print("presented")
        //self.grids.center = self.offScreenPosition
        print("position share is \(self.grids.center)")
    })

    switch deviceOrientation {
    case "portrait":
        activityViewController.completionWithItemsHandler = {(UIActivityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
            if !completed {
                print("cancelled")
                self.moveViewVertically(.backIn, range: self.verticalRange)
            }
            if completed {
                print("completed")
                self.moveViewVertically(.backIn, range: self.verticalRange)
            }
        }
    case "landscape":
        activityViewController.completionWithItemsHandler = {(UIActivityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) in
            if !completed {
                print("cancelled")
                self.moveViewHorizontally(.backIn, range: self.horizontalRange)
            }
            if completed {
                self.moveViewHorizontally(.backIn, range: self.horizontalRange)
            }
        }
    default:
        break
    }

prints are there to see what's happening. it' my first app. thank you for your help.


Solution

  • problem is solved. in the moveView method, i wrote center.y -= 1 to move the view. ie an iteration over a desired range.

    now i do:

    UIView.animate(withDuration: 0.5) {
                self.grids.transform = CGAffineTransform(translationX: 0, y: -self.view.frame.height)}
    

    it works as expected.