【问题标题】:How to draw graphics in iphone gesture?如何在iphone手势中绘制图形?
【发布时间】: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
    }
}

【问题讨论】:

    标签: iphone xcode gesture


    【解决方案1】:

    您应该在应用程序的数据部分中跟踪手指。在-(void)panAction:(UIPanGestureRecognizer *)gR 和myCanvasView -drawInRect:(CGRect)rect 方法中调用[myCanvasView setNeedsDisplay] 绘制这条轨迹。

    类似这样的:

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

    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
    

    【讨论】:

    • 是的,这就是我目前所做的。但是,如果我只想有一个圆形指示器来显示用户手指的位置,这不是直截了当的吗?是否可以直接在“location2”显示指示器?或者我必须在 drawRect:?
    • 当然,为指标创建一个视图(也许 UIImageView 就足够了)。并根据手势坐标移动此视图位置。手势开始时显示,结束时隐藏。
    • Zapko,感谢您的提示。这对圆形指示器很有用。但是在平移手势过程中如何配合线指示器呢?目前我有3个指标,起点圆,平移手势线,终点圆。看起来流畅地显示 UI 很复杂。一个问题是重绘问题。
    • 好的,要制作平移指示器,您可以继承 UIView。它应该有起点、终点和drawRect 方法来从一个点画一条线到另一个点。并且不要忘记禁用其用户交互。
    • 这种泛指标是否有示例代码可供参考?我尝试从头开始,但我不知道如何开始。
    【解决方案2】:

    我用自定义手势做到了这一点。当手势设置手势状态(从触摸开始、移动或结束)时,手势操作回调会返回到视图中,并且视图调用“setNeedsDisplayInRect”,然后从drawRect 进行绘图。

    您的实现的问题是您无法从手势的跟踪方法中设置图形上下文。当一个视图被标记为需要重绘(通过'setNeedsDisplay')时,这是为你完成的。

    这样做的原因是视图的内容可以缓存在一个层中,这对于优化动画和合成非常重要。因此,如果您需要在视图中绘图,请通过调用 setNeedsDisplay 并从您的 drawRect 方法进行绘图,使界面的其余部分与您的更改保持同步。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多