gpt4 book ai didi

iphone - 用 quartz 绘制航向角 View

转载 作者:行者123 更新时间:2023-12-03 21:19:14 26 4
gpt4 key购买 nike

我只是想在 MKMapView 上像 map 应用程序一样绘制航向角度 View ,当然我可以使用 Photoshop 或任何必要的程序来做到这一点。但我不需要考虑视网膜显示或屏幕差异。是否可以用 quartz 或任何相关的内置库绘制这个形状以及如何绘制?

enter image description here

提前致谢。

最佳答案

你可以;)为此,您需要实现此方法:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

在这个例子中,您必须仅为注释的用户位置情况创建 MKAnnotationView 子类的实例,例如:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
{
if (userView == nil) {
userView=[[[UserLocationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"user"]
autorelease];
}

[self addObserver:userView forKeyPath:@"userHeading" options:NSKeyValueObservingOptionNew context:NULL] ;

return userView ;
}

return nil;
}

注意KVO语句,让你的自定义MKAnnotationView知道值已经改变在你的 MKAnnotationView 子类中,你需要重写drawRect;例如这样

- (void)drawRect:(CGRect)rect {

CGContextRef context = UIGraphicsGetCurrentContext();
NSAssert(context !=nil, @"Context was nil") ;

CGContextSetRGBStrokeColor (context,1.0f,0.0f,0.0f,1.0f) ;

CGContextSetLineWidth(context, 2.0f);

CGContextMoveToPoint(context, 0, 15);
CGContextAddLineToPoint(context, 30, 15);

CGContextMoveToPoint(context, 15,0);
CGContextAddLineToPoint(context, 15, 30);

CGContextStrokePath(context);

CGContextSetLineWidth(context, 4.0f);

CGContextAddArc (context,15,15,13,0,6.28,0);

CGContextStrokePath(context);

hdg = radians([self heading]);

dx = self.frame.size.width / 2 ;
dy = self.frame.size.height/ 2 ;

if (hdg < M_PI) {
y = (cos(hdg) * dy ) - dy ;
x = (sin(hdg) * dx ) + dx ;
} else {
y = (cos(hdg) * dy ) + dy ;
x = (sin(hdg) * dx ) + dx ;
}


// NSLog(@"Heading:%3f Hdg:%3f X:%3f Y:%3f", heading, hdg, x, y) ;

CGContextSetRGBStrokeColor (context,1.0f,0.0f,1.0f,1.0f) ;

CGContextSetLineWidth(context, 2.0f);
CGContextMoveToPoint(context, dx,dy);
CGContextAddLineToPoint(context, x, y);


CGContextStrokePath(context);
}

这会绘制一个带有紫色线的红色十字线,显示标题(在我的示例中,类(class)被窃听,因为我在三角学方面很弱;)但它绘制了:)

您当然需要处理 KVO 通知,例如:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
[self setHeading: [[change objectForKey:NSKeyValueChangeNewKey]doubleValue]] ;
[self setNeedsDisplay] ;
NSLog(@"Heading is %f", [self heading]);
}

-setNeedsDisplay 将触发重绘。

确保您的自定义 View 具有非零矩形框架,并且不要为其设置图像。

关于iphone - 用 quartz 绘制航向角 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6570434/

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