用手势画线 [英] Draw Line with gesture

查看:141
本文介绍了用手势画线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

当用户用他的两根手指平移时,我需要绘制线条,并且如果需要用uilabel(绿色的)以下图片

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

我看过如何在iPhone手势中绘制图形?

但我无法在子视图上应用它,特别是DrawInRect方法

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

在panRecognizer函数中(移动)我想使用

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") ; 

我将使用Points中的点在

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

问题是[holderView setNeedsDisplay];永远不要打电话或解雇drawRect关于那个的任何建议或帮助

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

任何建议

推荐答案

作为子视图的图像视图绘制在持有者视图的顶部。 imageview是不透明的。这意味着当绘制视图时,持有者视图中没有任何部分实际可见,因此 drawRect 调用已被优化。

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.

尝试反过来排序视图,以便持有者视图是imageview的子视图。然后,imageview将绘制并且持有者视图将在其上绘制。

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

编辑(添加):

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

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"

因此,尝试将imageview作为父视图,进行初始化,如下所示:

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

现在,绘制图像视图和持有人视图,其子视图绘制在它上面。现在当你在holderview上调用setNeedsDisplay时,它将收到一个 drawRect:调用。

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.

例如,跟踪手势像这样。这可以在您的视图控制器或MyHolderView视图子类中;这里的示例将在MyHolderView类中,以便 location1 location2 实例变量可以与 drawRect: method。:

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

然后在持有人视图中绘制drawRect例程:

and then in the holder view drawRect routine:

-(void)drawRect:(CGRect)rect {

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

这篇关于用手势画线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆