任务是,在运行时在 ScrollView 中使用的自定义 map 上绘制路径,然后每当位置坐标(纬度、经度)更新时,我都必须在运行时绘制路径。我在这里试图解决的问题是我创建了一个“图形”类,它是 UIView 的子类,我在其中用“drawrect:”方法对绘图进行编码。因此,当我将图形添加为图像上 ScrollView 的 subview 时,会绘制线条,但我需要继续绘制线条,就好像它是路径一样。我需要在运行时画线,需要不断更新“CGContextStrokeLineSegments”方法的点(x,y)。代码:
View Controller :
- (void)loadView {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
graph = [[graphics alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,480);
UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fortuneCenter.png"]];
self.view=scrollView;
[scrollView addSubview:tempImageView2];
scrollView.userInteractionEnabled = YES;
scrollView.bounces = NO;
[scrollView addSubview:graph];
}
图形.m:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint point [2] = { CGPointMake(160, 100), CGPointMake(160,300)};
CGContextSetRGBStrokeColor(context, 255, 0, 255, 1);
CGContextStrokeLineSegments(context, point, 2);
}
那么我如何在运行时绘制线条。我现在只是模拟,所以我没有使用实时数据(坐标)。只想使用虚拟数据(x,y 坐标)进行模拟。假设有一个按钮,每当我按下它时,它都会更新坐标,以便路径延伸。
最佳答案
最简单的方法是将表示点的实例变量添加到 UIView
子类。
然后,每次路径更改时,适当更新 ivar 并调用 -setNeedsDisplay
或 setNeedsDisplayInRect
自定义 UIView
(甚至在其父 View 上) .然后运行时将重新绘制新路径。
关于iphone - 核心图形,如何在运行时画线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10043499/