Swifty Codes
Swifty Codes

Reputation: 1122

How to flip the images on x and y axis with swipe gesture in swift?

Can anybody please help me, how to flip images using swipe gesture in both x and y axis with animation in swift.I found some answers but they were not upto my requirements. Thank you in advance.

Upvotes: 0

Views: 606

Answers (1)

Swifty Codes
Swifty Codes

Reputation: 1122

    //MARK: - Image flipping operations performed here.

        func flipImageOnHorizontalAxis(theImage: UIImage,imageView:UIImageView) -> UIImage {

            var orient: UIImageOrientation!
            let imgOrientation = theImage.imageOrientation

            UIView.transition(with: imageView, duration: 0.4, options: .transitionCrossDissolve, animations: {() -> Void in

                switch imgOrientation {

                case .left:
                    orient = .rightMirrored

                case .right:
                    orient = .leftMirrored

                case .up:
                    orient = .upMirrored

                case .down:
                    orient = .downMirrored

                case .upMirrored
                    :
                    orient = .up

                case .downMirrored:
                    orient = .down

                case .leftMirrored:
                    orient = .right

                case .rightMirrored:
                    orient = .left
                }

            }, completion: { _ in })

            let mirroredImage = UIImage(cgImage: theImage.cgImage!, scale: 1.0, orientation: orient)
            return mirroredImage
        }

        func flipImageOnVerticalAxis(theImage: UIImage,imageView:UIImageView) -> UIImage {

            var orient: UIImageOrientation!
            let imgOrientation = theImage.imageOrientation

            UIView.transition(with: imageView, duration: 0.4, options: .transitionCrossDissolve, animations: {() -> Void in

                switch imgOrientation {

                case .left:
                    orient = .leftMirrored

                case .right:
                    orient = .rightMirrored

                case .up:
                    orient = .downMirrored

                case .down:
                    orient = .upMirrored

                case .upMirrored:
                    orient = .down

                case .downMirrored:
                    orient = .up

                case .leftMirrored:
                    orient = .left

                case .rightMirrored:
                    orient = .right
                }

            }, completion: { _ in })
            let mirroredImage = UIImage(cgImage: theImage.cgImage!, scale: 1.0, orientation: orient)
            //let mirroredImage = UIImage(CGImage: theImage.CGImage!, scale: 1.0, orientation: orient)
            return mirroredImage
        }

//MARK: To call the above function this can be done like this:

flipImageOnHorizontalAxis(theImage: imageView.image!, imageView: imageView)

or

flipImageOnVerticalAxis(theImage: imageView.image!, imageView: imageView)

Upvotes: 2

Related Questions