我正在尝试在坐标更改时旋转我的自定义注释。 我可以成功地更改坐标,但尝试了所有方法来旋转它,但没有成功。基本思想是有一个平面并通过旋转设置其方向。
这是我的代码:
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.
}
}
最佳答案
它以这种方式对我有用(是的,我使用了您示例中的一些代码部分:) 最主要的是获取注释 View 并更改(旋转)图像,然后:
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
}
//路由注解类
class RouteAnnotation : MKPointAnnotation {
var angle: Int = 0
var routeId: Int = 0
}
//UIImage扩展
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
}
}
希望对遇到同样问题的人有所帮助
关于Swift MKAnnotationView 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30964002/
点向量坐标矩阵的几何意义介绍旋转矩阵的几何含义之前,先介绍一下点向量坐标矩阵的几何含义点:在一维空间下就是一个标量,如同一条直线上,以任意某一个位置为0点,以一定的尺度间隔为1,2,3...,相反方向为-1,-2,-3...;如此就形成了一维坐标系,这时候任何一个点都可以用一个数值表示,如点p1=5,即即从原点出发沿着x轴正方向移动5个尺度;点p2=-3,负方向移动3个尺度; 在一维坐标系上过原点做垂直于一维坐标系的直线,则形成了二维坐标系,此时描述一个点需要两个数值来表示点p3=(3,2),即从原点出发沿着x轴正方向移动3个尺度,在此基础上沿着y轴正方向移动两个尺度的位置就是点p3。
Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u
欧拉角、旋转矩阵及四元数1.简介2.欧拉角2.1欧拉角定义2.2右手系和左手系2.3转换流程3.旋转矩阵4.四元数4.1四元数与欧拉角和旋转矩阵之间等效变换4.2测试Matlab代码5.总结1.简介常用姿态参数表达方式包括方向余弦矩阵、欧拉轴/角参数、欧拉角、四元数以及罗德里格参数等。高分辨率光学遥感卫星主要采用欧拉角与四元数对姿态参数进行描述。这里着重讲解欧拉角、旋转矩阵和四元数。2.欧拉角2.1欧拉角定义欧拉角是表征刚体旋转的一种方法之一,由莱昂哈德·欧拉引入的三个角度,用于描述刚体相对于固定坐标系的方向。在摄影测量、空间科学或其它技术领域,一般用一组(三个)欧拉角描述两个空间坐标之间的旋
最近更新的博客华为OD机试-卡片组成的最大数字(Python)|机试题算法思路华为OD机试-网上商城优惠活动(一)(Python)|机试题算法思路华为OD机试-统计匹配的二元组个数(Python)|机试题算法思路华为OD机试-找到它(Python)|机试题算法思路华为OD机试-九宫格按键输入(Python)|机试算法备考思路华为OD机试-身高排序(Python)|备考思路使用说明参加华为od机试,一定要注意不要完全背诵代码,需要理解之后模仿写出,通过率才会高。华为OD清单查看地址:blog.csdn.net/hihell/catego
1.变换1.1什么是变换?变换(Transform)是计算机图形学中非常重要的一部分。变换包含模型变换(Modelingtransform)以及视图变换(Viewtransform)。模型变换指的是变换模型(被拍摄物体)的位置,大小和角度;视图变换指的是变换照相机的位置和角度。从相对运动的角度来看,两种变换是可以相互转化的。1.2模型变换1.2.1二维变换缩放变换缩放变换(Scale)中,如果一个图片以原点(0,0)为中心缩放𝑠倍。那么点(𝑥,𝑦)变换后数学形式可以表示为写成矩阵形式为:当然,我们也可以给x轴和y轴不同的缩放倍数𝑠𝑥和𝑠𝑦。在非均匀情况下,缩放变换的矩阵形式为反射变换反射变换(
为Rails应用程序创建后台任务的首选方法是什么?我听说过Starling/Workling和优秀的ol'script/runner,但我很好奇哪种正在成为满足这一需求的实际方式?谢谢!澄清:我喜欢后台Rake的想法,但问题是,我需要持续运行或每10小时运行一次的东西。我不会奢侈地等待Web请求,它需要由服务器启动,与我网站上发生的事件异步。 最佳答案 RyanBates制作了三个可能真正对您有帮助的精彩截屏视频:RakeinBackgroundStarlingandWorklingCustomDaemon他谈到了使用每一种方法的各
我有一个Ruby脚本可以执行一些耗时较长的工作。它只是命令行,我想证明脚本仍在运行并且没有停止。我以前喜欢所谓的“旋转光标”,我设法在Windows下用Ruby重现了它。问题:这在其他操作系统中是否有效?如果没有,是否有独立于操作系统的方法来完成此任务?请不要使用IRB解决方案。10.times{print"/"sleep(0.1)print"\b"print"-"sleep(0.1)print"\b"print"\\"sleep(0.1)print"\b"print"|"sleep(0.1)print"\b"} 最佳答案 是的,这
我有以下使用JS和CSS旋转轮子的代码:varprefix=(function(){if(document.body.style.MozTransform!==undefined){return"MozTransform";}elseif(document.body.style.WebkitTransform!==undefined){return"WebkitTransform";}elseif(document.body.style.OTransform!==undefined){return"OTransform";}else{return"transform";}}()),ro
有谁知道如何旋转CannonJS(物理库)CANNON.RigidBody?我试图让物体随相机旋转,所以两者都朝向相同的方向。我知道我必须修改四元数,但这不能正常工作:mPlayer.objectBody.quaternion.set(0,mPlayer.yawObject.rotation.y,0,1);它还会改变对象的Y轴位置,而不仅仅是旋转。Here'sademo(WASD移动红色矩形-这是我想要旋转的)Here'sthemainscript目前它会根据物理原理自动旋转。感谢您的帮助!编辑:我现在有点让它工作了。但是它没有完全旋转(整个360度)并且它旋转的Angular不太正确
我使用Processing.js在图像上显示大约45度旋转的文本。问题是旋转后文本变得难以阅读,因为伪影变得可见,字母间距不恒定或字母没有相同的底线。这是一个demo.您可以看到第二个“HelloWorld”具有Ø符号,而不是e和o。此外,在第8个位置,字母间距问题很明显。有办法解决这个问题吗?至少对于45度旋转的文本。这是一个截图。我知道这看起来没什么大不了的,但最终图像必须是完美的,这个错误真的很突出。 最佳答案 我无法给出具体的处理解决方案,但如果您对通用答案没问题,您可以执行以下操作,然后可能将其与处理集成:Chrome的文