在iOS中创建自定义地图功能
我想创建一个特定区域的示例地图,用户可以点击其中任何一个区域,该区域的颜色将变为橙色 为此,我在图像顶部添加了一些按钮,但它不能提供准确的结果在iOS中创建自定义地图功能,ios,uibutton,mkmapview,Ios,Uibutton,Mkmapview,我想创建一个特定区域的示例地图,用户可以点击其中任何一个区域,该区域的颜色将变为橙色 为此,我在图像顶部添加了一些按钮,但它不能提供准确的结果 请在此分享您的评论。您可以使用CAShapeLayer。在本例中,我检查触摸位置是否在特定层内,然后检查触摸位置是否在该层的路径内 import UIKit class ViewController: UIViewController { private let step = 50 private lazy var rectSize = CGS
请在此分享您的评论。您可以使用CAShapeLayer。在本例中,我检查触摸位置是否在特定层内,然后检查触摸位置是否在该层的路径内
import UIKit
class ViewController: UIViewController {
private let step = 50
private lazy var rectSize = CGSize(width: step, height: step)
private struct MapSize { let width: Int; let height: Int }
private let mapCanvas = UIView()
override func viewDidLoad() {
super.viewDidLoad()
generateMap(withSize: MapSize(width: 6, height: 6))
view.addSubview(mapCanvas)
mapCanvas.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
view.leadingAnchor.constraint(equalTo: mapCanvas.leadingAnchor),
view.trailingAnchor.constraint(equalTo: mapCanvas.trailingAnchor),
view.topAnchor.constraint(equalTo: mapCanvas.topAnchor, constant: -200),
mapCanvas.heightAnchor.constraint(equalToConstant: 300)
])
}
private func generateMap(withSize size: MapSize) {
for x in 0..<size.width {
for y in 0..<size.height {
let layer = CAShapeLayer()
layer.frame = CGRect(origin: CGPoint(x: x * step, y: y * step), size: rectSize)
layer.path = createPath().cgPath
layer.fillColor = UIColor.cyan.cgColor
layer.borderWidth = 1
layer.borderColor = UIColor.black.cgColor
mapCanvas.layer.addSublayer(layer)
}
}
}
private func createPath() -> UIBezierPath {
let rectangle = UIBezierPath()
rectangle.move(to: CGPoint(x: 0, y: 0))
rectangle.addLine(to: CGPoint(x: 0, y: step))
rectangle.addLine(to: CGPoint(x: step, y: step))
rectangle.addLine(to: CGPoint(x: step, y: 0))
rectangle.close()
return rectangle
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first, let sublayers = mapCanvas.layer.sublayers else { return }
let touchLocaltion = touch.location(in: mapCanvas)
for sublayer in sublayers {
guard let shape = sublayer as? CAShapeLayer,
let path = shape.path,
shape.hitTest(touchLocaltion) != nil else { continue }
let locationInsideRect = mapCanvas.layer.convert(touchLocaltion, to: shape)
if path.contains(locationInsideRect) {
if shape.fillColor == UIColor.red.cgColor {
shape.fillColor = UIColor.cyan.cgColor
} else {
shape.fillColor = UIColor.red.cgColor
}
}
}
}
}
导入UIKit
类ViewController:UIViewController{
私人出租阶梯=50
private lazy var rectSize=CGSize(宽度:步长,高度:步长)
私有结构映射大小{let width:Int;let height:Int}
私有let mapCanvas=UIView()
重写func viewDidLoad(){
super.viewDidLoad()
generateMap(尺寸:MapSize(宽度:6,高度:6))
view.addSubview(mapCanvas)
mapCanvas.TranslatesAutoResizezingMaskinToConstraints=false
NSLayoutConstraint.activate([
view.leadingAnchor.constraint(等式:mapCanvas.leadingAnchor),
view.trailingAnchor.constraint(equalTo:mapCanvas.trailingAnchor),
view.topAnchor.constraint(等式:mapCanvas.topAnchor,常数:-200),
mapCanvas.heightAnchor.constraint(equalToConstant:300)
])
}
专用函数生成器映射(大小:MapSize){
对于0中的x。您可以使用UITapGestureRecognitor,然后计算选择的区域。您可以从手势识别器获取触摸坐标。可能重复:检查此答案你好,rmaddy,感谢您的回复。您能告诉我我们将如何使用自定义形状创建区域,因为它不是矩形/正方形。请使用CAShapeLayer。只需绘制形状并将其分配给该对象的路径属性,然后我猜您可以通过调用方法hitTest
检查点是否在该形状内,或者使用CGPathContainsPoint并将CGPath分配给层。你好,安迪,如果您能共享一些相同的代码或链接,那将对我非常有帮助。非常感谢安迪。太棒了人。它的工作原理与上图所示完全相同。你真的做了一件很棒的工作:)现在我只需要最后一个查询,而不是矩形,我们将如何创建自定义形状。你可以使用UIBezierPath绘制任何形状。然后你可以将其分配给UIShapeLayer并在屏幕上定位。对于绘制形状,你可以使用PaintCode,然后导出swift code from Itady,我们将如何确定哪个地区被开发,因为我们有许多地区的资源?