永远不会调用drawRect和setNeedsDisplay

时间:2011-06-27 08:10:18

标签: iphone ipad uigesturerecognizer

我使用

添加了一个uiview(持有uimageview的持有者视图)作为子视图和asign手势。

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

panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
holderView.tag=101;
[self.view addSubview:holderView];
[holderView sizeToFit];
[self AddInformation];
[holderView retain];
[self retain];

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

绘制线条
         UIPanGestureRecognizer *gR = (UIPanGestureRecognizer *) sender ; 


        NSValue *value = [NSValue valueWithCGPoint: [gR locationInView:gR.view]]; 





        [Points addObject:value];


        [holderView setNeedsDisplay];

        NSLog(@"End of measuring") ; 

我将使用Points中的点在

中的所有子视图上方绘制线条

- (无效)的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 :(得分:3)

您需要子类化UIView并在子类中实现drawRect。

然后在创建 holderview 时,请确保您实例化的是子类UIView,而不是默认的UIView。