Iphone 用手势画线
我使用以下代码将视图添加为子视图Iphone 用手势画线,iphone,objective-c,ipad,subview,Iphone,Objective C,Ipad,Subview,我使用以下代码将视图添加为子视图 imageview = [[UIImageView alloc] initWithFrame:[holderView frame]]; [imageview setImage:cppobject->OutputImage]; imageview.contentMode = UIViewContentModeScaleAspectFit; [holderView addSubview:imageview]; hold
imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:cppobject->OutputImage];
imageview.contentMode = UIViewContentModeScaleAspectFit;
[holderView addSubview:imageview];
holderView.contentMode = UIViewContentModeScaleAspectFit ;
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
[rotationRecognizer release];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
[panRecognizer release];
-(void)scale:(id)sender {
}
-(void)rotate:(id)sender {
}
-(void)move:(id)sender {
}
-(void)tapped:(id)sender {
}
当用户使用两个手指平移和一个uilabel(绿色)时,我需要画一条线,如下图所示
我看了一下
但我无法将其应用于子视图,特别是DrawInRect方法
在装甲识别器功能(移动)中,我想使用
UIPanGestureRecognizer *gR = (UIPanGestureRecognizer *) sender ;
NSValue *value = [NSValue valueWithCGPoint: [gR locationInView:gR.view]];
[Points addObject:value];
[holderView setNeedsDisplay];
NSLog(@"End of measuring") ;
我将使用点中的点在中的所有子视图上方绘制线
-(void)drawRect:(CGRect)rect {
NSLog(@"Entered Draw In Rect");
if (Measuring) {
[[UIColor redColor] setStroke];
UIBezierPath *pathToDraw = [UIBezierPath bezierPath];
for (int n = 1; n < [Points count] - 1 ; n++) {
NSValue * value = [Points objectAtIndex:(NSInteger)n];
CGPoint point = [value CGPointValue];
[pathToDraw moveToPoint:point];
value = [Points objectAtIndex:(NSInteger)n+1];
point = [value CGPointValue];
[pathToDraw addLineToPoint:point];
}
[pathToDraw stroke];
}
}
-(void)drawRect:(CGRect)rect{
NSLog(@“已输入绘制矩形”);
如果(测量){
[[UIColor redColor]设置行程];
UIBezierPath*pathToDraw=[UIBezierPath bezierPath];
对于(int n=1;n<[点数]-1;n++){
NSValue*value=[点对象索引:(NSInteger)n];
CGPoint point=[value CGPointValue];
[路径绘制移动点:点];
值=[点对象索引:(NSInteger)n+1];
点=[value CGPointValue];
[pathToDraw addLineToPoint:point];
}
[路径绘制笔划];
}
}
问题是[holderView setNeedsDisplay];千万不要打电话或提出任何建议或帮助
任何建议作为子视图的图像视图绘制在支架视图的顶部。imageview是不透明的。这意味着在绘制视图时,支架视图中没有实际可见的部分,因此它的
drawRect
调用得到了优化
尝试以另一种方式对视图进行排序,以便holder视图是imageview的子视图。然后图像视图将绘制,支架视图将绘制在其顶部
另外,请注意,应该使用父视图的边界作为子视图的框架
UIView* subview = [[UIView alloc] initWithFrame:[parentview bounds]];
编辑(添加):
看
,特别是在“视图层次结构和子视图管理”部分下:
在视觉上,子视图的内容会模糊其父视图的全部或部分内容
因此,请尝试将imageview设置为父视图,按如下方式进行初始化:
// instance variables:
UIImageView* imageView;
MyHolderView* holderView;
imageView = [[UIImageView alloc] initWithFrame:mainRect];
holderView = [[MyHolderView alloc] initWithFrame:[imageView bounds]];
holderView.opaque = NO;
holderView.backgroundColor = [UIColor clearColor];
[imageView addSubview:holderView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:holderView action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
// etc...
现在,将绘制图像视图,并在其顶部绘制支架视图及其子视图。现在,当您在holderview上调用setNeedsDisplay时,它将收到一个drawRect:
调用
例如,像这样跟踪手势。这可能在视图控制器或MyHolderView视图子类中;这里的示例在MyHolderView类中,这样location1
和location2
实例变量就可以很容易地与drawRect:
方法共享:
-(void)scale:(id)sender {
if (sender == pinchRecognizer) { // this allows the responder to work with multiple gestures if required
// get position of touches, for example:
NSUInteger num_touches = [pinchRecognizer numberOfTouches];
// save locations to some instance variables, like `CGPoint location1, location2;`
if (num_touches >= 1) {
location1 = [pinchRecognizer locationOfTouch:0 inView:holderView];
}
if (num_touches >= 2) {
location2 = [pinchRecognizer locationOfTouch:1 inView:holderView];
}
// tell the view to redraw.
[holderView setNeedsDisplay];
}
}
然后在holder view drawRect例程中:
-(void)drawRect:(CGRect)rect {
// use instance variables location1 and location2 to draw the line.
}
看起来你的holderView可能是一个UIScrollView。如果是这样,这将更加困难。你可以发布你的
drawRect:
方法吗?当我们看不到有问题的代码时,很难提供帮助;但是它没有工作setNeedsDisplay
只是告诉框架需要呈现视图。如果视图没有正确插入到层次结构中,则不会调用drawRect
分离在另一个视图上绘制的逻辑。。!例如,使用具有类似imageView框架的透明测量视图,添加imageView的子视图,并在其中处理绘图逻辑@问题是-(void)drawRect:(CGRect)永远不能调用rect,[holderView setNeedsDisplay];千万别开枪你有什么建议吗对不起。。。再读一遍这个问题后,我完全重写了它。第二次祝你好运:)我会的,你能重新发布缩放代码吗,你刚刚删除了我需要它,请,你这么说是什么意思,它的drawRect调用得到了优化。我尝试了[imageview setNeedsDisplay];但是drawrect不被称为I used-(void)scale:(id)sender,我创建了cutome uiview并在其中修改了drawrect,但我为线条绘制添加了一个新的子视图,因为我无法对子视图进行排序,因为我使用此顺序进行缩放,非常感谢