iOS Swift:如何叠加同时放大的两个图像,而不会丢失重叠图像的坐标

前端之家收集整理的这篇文章主要介绍了iOS Swift:如何叠加同时放大的两个图像,而不会丢失重叠图像的坐标前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的问题真的很简单

想象一下,代表您房子周围的街道和建筑物的背景图像,请注意,这是一个专用图像.这个图像(视图)是可缩放的,到目前为止这么好.类似于地图,但与图像相反.

现在在图像上绘制的街道上方的图像上有代表汽车的标记.这些将随着时间推移,因此将动态地移动.我已经成功地把汽车放在正确的地方,但是当我放大/缩小时,汽车就不在位置.请注意,我不希望汽车的尺寸发生变化,它们将始终保持不变.

本质上,与Google地图顶部的地图标记非常相似,但不是地图,我有一个背景图片,而不是标记,我有其他图像(汽车).

现在为了执行…
我有一个简单的ScrollableView包含一个图像视图.我正在尝试覆盖我想保持放置但不放大或缩小的其他多张照片(或至少不如主图片).我会说,它类似于一个地图上有位置针,但它只是使用图片.
我在Swift和最新的iOS版本(9x)

import UIKit

class ViewController: UIViewController,UIScrollViewDelegate
{
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var pin1View: UIImageView!
    @IBOutlet weak var pin2View: UIImageView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.scrollView.minimumZoomScale = 1.0;
        self.scrollView.maximumZoomScale = 10.0;



    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
    {
        let pin1_X = self.pin1View.frame.origin.x
        let pin2_Y = self.pin2View.frame.origin.y
        //I guess I need to do something here but not sure
        return self.imageView
    }   
}

任何提示非常感激.我google了,但找不到例子,仍然学习意见和迅速以及一般的移动设备.
谢谢你的帮助.

解决方法

相应地,您有针的更换中心.使用此更新代码
class ViewController: UIViewController,UIScrollViewDelegate
{
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var pin1View: UIImageView!
    @IBOutlet weak var pin2View: UIImageView!
    var pin1ViewCenter : CGPoint = CGPointZero
    var pin2ViewCenter : CGPoint = CGPointZero

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.scrollView.scrollEnabled = true
        self.scrollView.minimumZoomScale = 1.0
        self.scrollView.maximumZoomScale = 10.0

        pin1ViewCenter = pin1View.center
        pin2ViewCenter = pin2View.center

        self.scrollView.contentSize = self.imageView.frame.size
    }


    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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

    func scrollViewDidZoom(scrollView: UIScrollView) {

        let scaleAffineTransform = CGAffineTransformScale(CGAffineTransformIdentity,scrollView.zoomScale,scrollView.zoomScale)
        var translatedPoint = CGPointApplyAffineTransform(pin1ViewCenter,scaleAffineTransform)
        pin1View.transform = CGAffineTransformTranslate(CGAffineTransformIdentity,translatedPoint.x - pin1ViewCenter.x,translatedPoint.y - pin1ViewCenter.y)
        translatedPoint = CGPointApplyAffineTransform(pin2ViewCenter,scaleAffineTransform)
        pin2View.transform = CGAffineTransformTranslate(CGAffineTransformIdentity,translatedPoint.x - pin2ViewCenter.x,translatedPoint.y - pin2ViewCenter.y)
    }

    func scrollViewDidEndZooming(scrollView: UIScrollView,withView view: UIView?,atScale scale: CGFloat) {
        let scaleAffineTransform = CGAffineTransformScale(CGAffineTransformIdentity,scale,scale)
        scrollView.contentSize = CGSizeApplyAffineTransform(self.imageView.bounds.size,scaleAffineTransform)
    }

}
原文链接:https://www.f2er.com/iOS/329275.html

猜你在找的iOS相关文章