用手势画线

发布于 2024-11-16 12:04:11 字数 2550 浏览 1 评论 0原文

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

    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 函数(移动) 我想使用画线

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

,我将使用 Points 中的点在问题 是 [holderView setNeedsDisplay];切勿致电或触发任何建议或有关

任何建议的帮助

I add a view as sub view using the following code

    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 {
}

I need to draw line when the user use his two fingers to pan and a uilabel (the green one) like in the following image

I had look on How to draw graphics in iphone gesture?

But I couldn't apply it on the subview , specially the DrawInRect method

in panRecognizer function (move) I want to draw line using

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

    NSLog(@"End of measuring") ; 

and I will use the points in Points to draw line above all the subviews in

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

the problem is [holderView setNeedsDisplay]; never call or fire drawRect any suggestion or help regarding that

any suggestion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-11-23 12:04:12

作为子视图的图像视图绘制在持有者视图的顶部。图像视图是不透明的。这意味着当绘制视图时,持有者视图的任何部分实际上都不可见,因此它的 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< /a>
,特别是在“视图层次结构和子视图管理”部分下:

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

因此,尝试使 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 类中,以便可以使用 drawRect: 方法轻松共享 location1location2 实例变量。

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

:然后在持有者视图中绘制矩形例程:

-(void)drawRect:(CGRect)rect {

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

The image view being the subview draws on top of the holder view. The imageview is opaque. This means that when the views are drawn there is no part of the holder view that is actually visible, so it's drawRect call is optimised out.

Try ordering the views the other way around, so that the holder view is the subview of the imageview. Then the imageview will draw and the holder view will draw on top of it.

Also, note that you should use the bounds of the parent view as the frame of the subview.

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

Edit (add):

See http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html%23//apple_ref/doc/uid/TP40009503-CH2-SW1
, specifically under the section "View Hierarchies and Subview Management":

"Visually, the content of a subview obscures all or part of the content of its parent view"

So, try make the imageview the parent, do your initialisation like so:

// 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...

Now, the image view is drawn, and the holder view, its subview is drawn on top of it. Now when you call setNeedsDisplay on the holderview it will receive a drawRect: call.

For example, track the gesture like so. This could be in your view controller or in your MyHolderView view subclass; the example here would be in the MyHolderView class so that the location1 and location2 instance variables can be shared easily with the drawRect: method.:

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

and then in the holder view drawRect routine:

-(void)drawRect:(CGRect)rect {

  // use instance variables location1 and location2 to draw the line.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文