Swift “如何绘制”;旋转;箭头作为注释?
好的,我的新任务是:在一个跟踪器应用程序中,我有一个地图,我正试图用自定义的方向箭头来注释它。我需要把这个箭头旋转到某个角度 我有所有的数据(即坐标和位置)和该方法:Swift “如何绘制”;旋转;箭头作为注释?,swift,mapkit,Swift,Mapkit,好的,我的新任务是:在一个跟踪器应用程序中,我有一个地图,我正试图用自定义的方向箭头来注释它。我需要把这个箭头旋转到某个角度 我有所有的数据(即坐标和位置)和该方法: headingImageView?.transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle)) 所以我试着: 使用创建注释 let annotation = UserAnnotation(coordinate: locations.last!.coor
headingImageView?.transform = CGAffineTransform(rotationAngle: CGFloat(rotationAngle))
所以我试着:
let annotation = UserAnnotation(coordinate: locations.last!.coordinate)
mapView.addAnnotation(annotation)
MKMapViewDelegate
协议
extension TrackViewController: MKMapViewDelegate
:viewFor:
和:renderfor:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if !overlay.isKind(of: MKPolyline.classForCoder()) {
return MKPolylineRenderer(overlay: overlay)
}
let polyline = overlay as! MKPolyline
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.strokeColor = .blue
renderer.lineWidth = 3
return renderer
}
所以这是可行的,但我需要在每次位置更新时旋转图像。我该怎么做?任何建议都将不胜感激好吧,现在我做到了:
let arrow = #imageLiteral(resourceName: "arrow")
var rotatedArrow: UIImage?
var oldAnnotation: MKPointAnnotation?
rotatedArrow = arrow.imageRotated(by: CGFloat(locations.last!.course))
annotationView.image = rotatedArrow
return annotationView
let arrow = #imageLiteral(resourceName: "arrow")
var rotatedArrow: UIImage?
var oldAnnotation: MKPointAnnotation?
但这一切看起来太丑陋了您可以使用以下代码: 但这一点很重要,要知道你必须为
rotationAngle
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is UserAnnotation {
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "Online")
annotationView.image = UIImage(named: "arrow")
annotationView.transform = CGAffineTransform(rotationAngle: CGFloat(//what you want with type of radian))
return annotationView
}
}
什么是
headingImageView
?我看你的代码样本中没有用到它。它的视图是否与注释视图。图像相同?嗯,基本相同。我还试图将这个箭头图片声明为全局变量,但它看起来太难看了。还有其他方法吗?抱歉,如果没有看到完整的来源,很难完全掌握它。为提高性能,您可以做的另一件事是使用Welcome to Stack Overflow(欢迎使用堆栈溢出)将注释视图从mapview本身中出列!为了更好地帮助那些后来发现此问题的人,您应该单击edit并添加此代码如何解决此问题的解释/摘要。谢谢