首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速MKAnnotationView旋转

快速MKAnnotationView旋转
EN

Stack Overflow用户
提问于 2015-06-21 11:28:08
回答 3查看 2.3K关注 0票数 3

当坐标被更改时,我试图旋转我的自定义注释。我可以成功地改变坐标,但是我尝试了所有的方法来旋转它,而没有运气。基本思想是有一个平面,并设置它的方向旋转。

这是我的代码:

代码语言:javascript
复制
import UIKit
import MapKit

class CustomPointAnnotation: MKPointAnnotation {
    var imageName: String!
}

class ViewController: UIViewController, MKMapViewDelegate {

var myAnnot : MKPointAnnotation!
var myDegree = 0.0
var mylat = 41.1036

@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
    super.viewDidLoad()


    mapView.delegate = self
    mapView.mapType = MKMapType.Hybrid
    mapView.showsUserLocation = true
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewDidAppear(animated: Bool) {

    println("HOMESET")
    var homeGPSData = CLLocation(latitude: 41.1036, longitude: 29.3822)
    println(homeGPSData)
    self.myAnnot = MKPointAnnotation()

    myAnnot.coordinate = CLLocationCoordinate2DMake(homeGPSData.coordinate.latitude, homeGPSData.coordinate.longitude)
    myAnnot.title = "Base"
    var rangeCircle : MKCircle  = MKCircle(centerCoordinate: homeGPSData.coordinate, radius: 20000)
    mapView.addOverlay(rangeCircle)
    mapView.addAnnotation(myAnnot)

   var myLoop = NSTimer.scheduledTimerWithTimeInterval(0.0004, target: self, selector: "rotateAndMoveAnnotation", userInfo: nil, repeats: true)


}


func rotateAndMoveAnnotation() {


    var lon = 29.3822

    var homeGPSData = CLLocation(latitude: self.mylat, longitude: lon)

    myAnnot.coordinate = CLLocationCoordinate2DMake(homeGPSData.coordinate.latitude, homeGPSData.coordinate.longitude)

    self.mylat = self.mylat + 0.001
    self.myDegree = self.myDegree + 1




}

func degreesToRadians(degrees: Double) -> Double { return degrees * M_PI / 180.0 }
func radiansToDegrees(radians: Double) -> Double { return radians * 180.0 / M_PI }

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {



    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)

    }

    anView.image = UIImage(named: "sprite.png")


    return anView
}

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


}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-19 22:28:13

它以这种方式工作(是的,我使用了示例中的一些代码部分:)主要是获得注释视图,然后更改(旋转)图像:

代码语言:javascript
复制
for tempAnnotation in self.mapView.annotations as! [RouteAnnotation]
{
    tempAnnotation.angle = annotations[self.annotationIDs[tempAnnotation.routeId]!].angle
    let annotationView: MKAnnotationView = self.mapView.viewForAnnotation(tempAnnotation)!
    var annotationViewImage = annotationView.image
    annotationViewImage = imageResize(image: UIImage(named:"arrowPin.png")!,sizeChange: CGSizeMake(48, 48)).imageRotatedByangles(CGFloat(tempAnnotation.angle), flip: false)
    annotationView.image = annotationViewImage
}

//路由注释类

代码语言:javascript
复制
class RouteAnnotation : MKPointAnnotation {
    var angle: Int = 0
    var routeId: Int = 0
}

//UIImage扩展

代码语言:javascript
复制
extension UIImage {
public func imageRotatedByangles(angles: CGFloat, flip: Bool) -> UIImage {
    let anglesToRadians: (CGFloat) -> CGFloat = {
        return $0 / 180.0 * CGFloat(M_PI)
    }

    // calculate the size of the rotated view's containing box for our drawing space
    let rotatedViewBox = UIView(frame: CGRect(origin: CGPointZero, size: size))
    let t = CGAffineTransformMakeRotation(anglesToRadians(angles));
    rotatedViewBox.transform = t
    let rotatedSize = rotatedViewBox.frame.size

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize)
    let bitmap = UIGraphicsGetCurrentContext()

    // Move the origin to the middle of the image so we will rotate and scale around the center.
    CGContextTranslateCTM(bitmap, rotatedSize.width / 2.0, rotatedSize.height / 2.0);

    //   // Rotate the image context
    CGContextRotateCTM(bitmap, anglesToRadians(angles));

    // Now, draw the rotated/scaled image into the context
    var yFlip: CGFloat

    if(flip){
        yFlip = CGFloat(-1.0)
    } else {
        yFlip = CGFloat(1.0)
    }

    CGContextScaleCTM(bitmap, yFlip, -1.0)
    CGContextDrawImage(bitmap, CGRectMake(-size.width / 2, -size.height / 2, size.width, size.height), CGImage)

    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}
}

希望,它能帮助那些有同样问题的人

票数 1
EN

Stack Overflow用户

发布于 2018-12-08 13:20:59

我添加了两行代码,这些代码在下面的注释和旋转中都很好地在4中工作:

代码语言:javascript
复制
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    let reuseIdentifier = "pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)

    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.canShowCallout = true
    } else {

        annotationView?.annotation = annotation
    }

    //these two lines rotated the annotation ---------->>
    var rotationInRadian: Double = .pi
    annotationView?.transform = CGAffineTransform(rotationAngle: rotationInRadian)

    return annotationView
}

刚刚注意到rotationAngle是弧度中的CGFloat数。

票数 1
EN

Stack Overflow用户

发布于 2015-06-24 00:28:09

找到了一个肮脏的解决方案。刚向viewforAnnotation方法添加了以下行:

代码语言:javascript
复制
anView.boundsRotation = myRotateAngle
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30964002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档