开发者

How to draw a line on top of an image when finger moves on iPhone

I am trying to draw a line on top of a background image in my UIView tracing the movements of the user's finger. I can accomplish this by drawing the UIImage as my background and then doing my line drawing in drawRect but I have to load the image each time drawRect is called and it makes performance sluggish. Alternatively, if I use a UIImageView as my background image (just adding it to the view in IB or programatically) the lines disappear when it is drag开发者_如何学Pythonged over the image. Is there a way to tell the lines to draw on top of the UIImageView. Thanks for your help.

Here is my code using the UIImage method:

- (void)drawRect:(CGRect)rect {
    UIImage *imageField = [UIImage imageNamed:@"field.jpg"];
    CGRect rectForImage=CGRectMake(0,0,200,200);
    [imageField drawInRect:rectForImage];

        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextMoveToPoint(context, x1, y1);
    CGContextAddLineToPoint(context, x2, y2);
    CGContextStrokePath(context);
}


Adding the image as the background view is the correct way to do it. The problem lies in the fact that you added the image directly to the view. Subviews are displayed on top of a view, not behind it. Correctly use a container view that holds the image view as a background, and then your line-drawing view on top of that.


You can try drawing your (opaque) lines to a transparent CALayer, and placing that CALayer on top of your UIView, thus above the background UIImage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜