【问题标题】:Keep UIView in same location on image with AspectFit after rotation旋转后使用 AspectFit 将 UIView 保持在图像上的相同位置
【发布时间】:2018-08-18 05:47:15
【问题描述】:

我有一个应用程序,用户可以在其中将形状放置到图像上。图像位于设置为 AspectFit 的 UIImageView 中。 UIImageView 位于 UIScrollView 中以允许缩放和平移。形状是添加到 UIImageView 的 UIView。除非设备方向发生变化,否则一切正常。由于图像设置为 AspectFit 并且方向发生变化,因此纵横比也会发生变化。因此,我的形状不会在图像上保持相同的位置。

轮换前:

轮换后:

ViewController 代码:

override func viewDidLoad() {
    super.viewDidLoad()

    imageView.image = markupUIImage;

    scrollView.maximumZoomScale=5;
    scrollView.minimumZoomScale=0.5;
    scrollView.bounces=true;
    scrollView.bouncesZoom=true;
    scrollView.contentSize=CGSize(width: imageView.frame.size.width, height: imageView.frame.size.height);
    scrollView.showsHorizontalScrollIndicator=true;
    scrollView.showsVerticalScrollIndicator=true;
    scrollView.delegate=self;
}

func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
    return self.imageView
}

形状类:

class ShapeUIView: UIView {

    override public class var layerClass: Swift.AnyClass {
        get {
            return CAShapeLayer.self;
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        initializeView()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initializeView()
    }

    private func initializeView(){

        let shapeLayer = (layer as! CAShapeLayer);
        shapeLayer.opacity = 1.0
        shapeLayer.lineJoin = kCALineJoinRound
        shapeLayer.lineWidth = 2.0
        shapeLayer.strokeColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor
        shapeLayer.fillColor = UIColor(red: 0, green: 128, blue: 0, alpha: 0.3).cgColor

        self.isUserInteractionEnabled = true;
        self.alpha = 1.0;
        self.isHidden = false;
    }

    public func setPath(_ path: UIBezierPath) {
        (layer as! CAShapeLayer).path = path.cgPath;
    }
}

如何让形状在旋转后保持在图像上的相同位置?

【问题讨论】:

  • 您是否需要将形状保留为 UIView,以防您不在乎。您可以在 UIView 内添加图像视图,并且每次向图像添加形状时,您都会从 UIView 内部的内容创建图像 - 在这种情况下,将是图像视图和正确位置的形状 - 并用新的替换图像视图的图像图片。
  • 我需要将形状保留为 UIView。我尝试过这样的事情,但是我正在处理的图像相当大,并且在创建带有形状的图像时存在延迟,并且在创建如此大的图像时遇到了内存问题

标签: ios swift xcode cocoa-touch


【解决方案1】:

我能够弄清楚我自己的问题。在这里发帖以防其他人遇到此问题。

我所做的是设置 UIImageView.contentMode = .center 并将 UIImageView 大小设置为图像的相同高度和宽度。然后我让 UIScrollView 做它的事情并缩放图像。因此,我没有 UIImageView 缩放图像,因为 AspectFit 和 UIScrollView 缩放图像。 我在 UIViewController 中添加了以下内容:

override func viewDidAppear(_ animated: Bool) {

    //Initial Scale
    imageView.bounds.size.width = (imageView.image?.size.width)!;
    imageView.bounds.size.height = (imageView.image?.size.height)!;
    imageView.frame = CGRect(x: 0, y: 0, width: imageView.bounds.size.width, height: imageView.bounds.size.height)

    let widthScale: CGFloat = scrollView.width / imageView.image!.size.width;
    let heightScale: CGFloat = scrollView.height / imageView.image!.size.height;
    let initialZoom = min(widthScale, heightScale);

    scrollView.setZoomScale(initialZoom, animated: false)
}

现在,当我在图像上绘制形状并旋转设备时,它们会停留在正确的位置。

【讨论】:

    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    相关资源
    最近更新 更多