How to draw graphics in iphone gesture?

∥☆過路亽.° 提交于 2019-12-11 04:19:57

问题


I have a question to draw a line or circle indicator after user has pan gesture (i.e. user touches and drags their finger) on iPhone. However, UIGraphicsGetCurrentContext() always returns nil, does anyone know how to implement this on iPhone?

Thanks, clu

@interface MyView : UIView <UIGestureRecognizerDelegate> {
CGPoint location;
PanIndicator *panIndicator;
}

@implementation MyView 
- (id)init {
    if (self = [super init]) {
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [panGesture setMaximumNumberOfTouches:1];
        [panGesture setDelegate:self];
        [self addGestureRecognizer:panGesture];
        [panGesture release];

        panIndicator = [[PanIndicator alloc] init];
        [self addSubview:panIndicator];
    }
    return self;
}

- (void)panAction:(UIPanGestureRecognizer *)gR {
    if ([gR state]==UIGestureRecognizerStateBegan) {
        location = [gR locationInView:self];
    } else if ([gR state]==UIGestureRecognizerStateEnded) {
    //  The following code in this block is useless due to context = nil
//      CGContextRef context = UIGraphicsGetCurrentContext();
//      CGContextAddRect(context, CGRectMake(30.0, 30.0, 60.0, 60.0));
//      CGContextStrokePath(context);
    } else if ([gR state]==UIGestureRecognizerStateChanged) {
    CGPoint location2 = [gR locationInView:self];
        panIndicator.frame = self.bounds;
        panIndicator.startPoint = location;
        panIndicator.endPoint = location2;
//      [panIndicator setNeedsDisplay];    //I don't know why PanIncicator:drawRect doesn't get called
        [panIndicator drawRect:CGRectMake(0, 0, 100, 100)]; //CGRectMake is useless
    }
}

回答1:


You should keep track of the finger in your application's data part. Call [myCanvasView setNeedsDisplay] in -(void)panAction:(UIPanGestureRecognizer *)gR and in myCanvasView -drawInRect:(CGRect)rect method draw this track.

Something like this:

- (void)panAction:(UIPanGestureRecognizer *)gR 
{
    [myData addPoint:[gR locationInView:gR.view]];
    [myCanvasView setNeedsDisplay];
}

- (void)drawInRect:(CGRect)rect
{
    [self drawLinesFromData:myData];
}

A draft for PanIndicator:

@interface PanIndicator : UIView {}
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGPoint endPoint;
@end

@implementation PanIndicator
@synthesize startPoint = startPoint_;
@synthesize endPoint = endPoint_;

- (void)drawRect:(CGRect)aRect 
{
    [[UIColor redColor] setStroke];

    UIBezierPath *pathToDraw = [UIBezierPath bezierPath];
    [pathToDraw moveToPoint:self.startPoint];
    [pathToDraw addLineToPoint:self.endPoint];
    [pathToDraw stroke]
}

@end



回答2:


I did this with a custom gesture. When the gesture sets the gesture state (from touches began, moved or ended) the gestures action callback occurs back in the view, and the view calls "setNeedsDisplayInRect" and then the drawing occurs from drawRect.

The catch with your implementation is that you do not get to set the graphics context from within the gesture's tracking methods. When a view is flagged as needing redraw (via 'setNeedsDisplay') this is done for you.

The reason for this is so that the view's contents can be cached in a layer, which is really important for optimising animations and compositing. So if you need to draw in a view, keep the rest of the interface in sync with your changes by calling setNeedsDisplay and doing the drawing from your drawRect method.



来源:https://stackoverflow.com/questions/6173685/how-to-draw-graphics-in-iphone-gesture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!