gpt4 book ai didi

iOS swift : How to overlay two images that zoom in at same time without loosing coordinates of overlaying image

转载 作者:IT王子 更新时间:2023-10-29 05:37:32 33 4
gpt4 key购买 nike

我的问题真的很简单......

想象一张代表您家周围的街道和建筑物的背景图片,请注意这是专门制作的图片。此图像( View )是可缩放的,到目前为止一切都很好。类似于 map ,但使用图像代替。

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

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

现在开始实现...我有一个简单的 ScrollableView,其中包含一个 ImageView 。我正在尝试覆盖其他多张我想保留位置但不放大或缩小的图片(或者至少不像主图片那么多)。我会说它类似于上面有位置图钉的 map ,但它只是使用图片。我在 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
}
}

非常感谢任何提示。我用谷歌搜索但找不到示例并且仍在学习 View 和快速以及一般的移动开发。感谢您的帮助。

最佳答案

您相应地拥有引脚的更改中心。使用这个更新的代码:

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

}

关于iOS swift : How to overlay two images that zoom in at same time without loosing coordinates of overlaying image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34835367/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com