如何在 swift 中使用滑动手势在 x 和 y 轴上翻转图像

  • 本文关键字:图像 翻转 swift ios swift
  • 更新时间 :
  • 英文 :


任何人都可以帮我,如何在 x 轴和 y 轴上使用滑动手势翻转图像,并在 swift 中使用动画。我找到了一些答案,但它们不符合我的要求。提前谢谢你。

    //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)

最新更新