【问题标题】:Draw Line with gesture用手势画线
【发布时间】:2011-06-22 08:27:00
【问题描述】:

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

    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 任何关于此的建议或帮助

任何建议

【问题讨论】:

  • 看起来您的 holderView 可能是 UIScrollView。如果是这样的话,这将更加困难。您能否也发布您的 drawRect: 方法 - 当我们看不到相关代码时,很难提供帮助。
  • 我试过 [imageView setNeedsDisplay];但它没有用
  • setNeedsDisplay 只是告诉框架需要渲染视图。如果视图没有正确插入到层次结构中,drawRect 将不会被调用
  • 分离出在另一个视图上绘制的逻辑..!例如,使用具有类似于 imageView 的框架的透明 MeasurementView,添加 imageview 的子视图并在那里处理绘图逻辑。 @AMH

标签: iphone objective-c ipad subview


【解决方案1】:

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

尝试以相反的方式对视图进行排序,以使持有者视图成为图像视图的子视图。然后 imageview 会绘制,holder view 会在它上面绘制。

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

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 类中,以便 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];
  }
}

然后在holder视图中drawRect例程:

-(void)drawRect:(CGRect)rect {

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

【讨论】:

  • 问题是 -(void)drawRect:(CGRect)rect 永远不会被调用,[holderView setNeedsDisplay];永远不要开火,你有什么建议吗
  • 对不起...再次阅读问题后,我完全重写了它。祝你第二次好运:)
  • 我会的,你能重新发布缩放的代码吗,你刚刚删除了我需要它,你是什么意思所以它的 drawRect 调用被优化了。
  • 我试过 [imageview setNeedsDisplay];但不会调用drawrect
  • 我使用了 -(void)scale:(id)sender ,我创建了 cutome uiview 并修改了其中的 drawRect ,但是我为线条绘制添加了新的 subveiw 因为我无法订购子视图,因为我使用这个缩放顺序,非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多