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
}
}
You should keep track of the finger in your application’s data part. Call
[myCanvasView setNeedsDisplay]in-(void)panAction:(UIPanGestureRecognizer *)gRand in myCanvasView-drawInRect:(CGRect)rectmethod draw this track.Something like this:
A draft for PanIndicator: