【发布时间】:2011-05-30 07:59:24
【问题描述】:
在用户在 iPhone 上进行平移手势(即用户触摸并拖动手指)后,我有一个问题要绘制一条线或圆形指示器。但是,UIGraphicsGetCurrentContext() 总是返回 nil,有人知道如何在 iPhone 上实现吗?
谢谢, 俱乐部
@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
}
}
【问题讨论】: