用手势绘制直线

4
我使用以下代码将一个视图添加为子视图:

我使用以下代码将一个视图添加为子视图:

    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(绿色的那个),如下图所示:

我查看了如何在iPhone手势中绘制图形?但我无法将其应用于子视图,特别是DrawInRect方法。

在panRecognizer函数(move)中,我想要使用以下方法绘制线条:

    UIPanGestureRecognizer *gR = (UIPanGestureRecognizer *) sender ; 
    NSValue *value = [NSValue valueWithCGPoint: [gR locationInView:gR.view]]; 
    [Points addObject:value];
    [holderView setNeedsDisplay];

    NSLog(@"End of measuring") ; 

我将使用Points中的点来绘制所有子视图上方的线条。

-(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];
  }
}

问题是[holderView setNeedsDisplay];从未调用或触发drawRect。有关此问题的任何建议或帮助。

有什么建议吗?


看起来你的 holderView 可能是一个 UIScrollView。如果是这种情况,那么这会更加困难。你能同时发布你的 drawRect: 方法吗?当我们无法看到相关代码时,帮助你变得非常困难。 - RyanR
我尝试了[imageView setNeedsDisplay]; 但它没有起作用。 - AMH
setNeedsDisplay 只是告诉框架视图需要被渲染。如果视图没有正确插入到层次结构中,drawRect 将不会被调用。 - RyanR
将在另一个视图上绘制的逻辑分离出来!例如,使用类似于imageView的透明MeasurementView,添加imageview的子视图并在那里处理绘制逻辑。@AMH - Mahesh
1个回答

4

图片视图作为子视图,绘制在容器视图之上。图片视图是不透明的。这意味着当视图被绘制时,实际上没有任何部分是可见的,因此它的drawRect调用被优化掉了。

尝试改变视图的顺序,使得容器视图成为图片视图的子视图。然后图片视图将被绘制,而容器视图将绘制在其上方。

另外,请注意应该使用父视图的边界作为子视图的框架。

UIView* subview = [[UIView alloc] initWithFrame:[parentview bounds]];

编辑(添加):

请参见http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW1,特别是在“视图层次结构和子视图管理”一节下面:

“从视觉上看,子视图的内容会遮盖其父视图的全部或部分内容。”

因此,请将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];
  }
}

然后在持有者视图的drawRect例程中:

-(void)drawRect:(CGRect)rect {

  // use instance variables location1 and location2 to draw the line.
}

问题是-(void)drawRect:(CGRect)rect从未被调用,[holderView setNeedsDisplay];从未触发它,您有什么建议吗? - AMH
抱歉...在重新阅读问题后,我完全重写了它。第二次好运 :) - Matt Connolly
我可以,请你重新发布缩放代码,你刚刚删除了它,我需要它,请问你所说的"so it's drawRect call is optimized out"是什么意思。 - AMH
我尝试使用 [imageview setNeedsDisplay]; 但是 drawrect 方法没有被调用。 - AMH
我使用了-(void)scale:(id)sender,并创建了自定义的UIview并修改了其中的drawRect,但是我添加了一个新的子视图用于线条绘制,因为我不能对子视图进行排序,因为我使用这个顺序进行缩放,非常感谢。 - AMH

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接