用手势画线

时间:2011-06-22 08:27:00

标签: iphone objective-c ipad subview

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

    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(绿色的手指),如下图所示

我看过How to draw graphics in iphone gesture?

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

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

绘制线条
    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关于那个的任何建议或帮助

任何建议

1 个答案:

答案 0 :(得分:4)

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

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

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

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

现在,绘制图像视图,并在其上绘制持有者视图,其子视图。现在,当您在持有人视图上调用setNeedsDisplay时,它将收到drawRect:来电。

例如,跟踪手势就像这样。这可以在您的视图控制器或MyHolderView视图子类中;这里的示例将在MyHolderView类中,以便location1location2实例变量可以使用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.
}