如何在iPhone手势中绘制图形? [英] How to draw graphics in iphone gesture?

查看:73
本文介绍了如何在iPhone手势中绘制图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题要在用户在iPhone上进行平移手势(即用户触摸并拖动手指)后绘制线条或圆圈指示器.但是,UIGraphicsGetCurrentContext()始终返回nil,有人知道如何在iPhone上实现吗?

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?

谢谢,

@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
    }
}

推荐答案

您应该在应用程序的数据部分中留意.在-(void)panAction:(UIPanGestureRecognizer *)gR中调用[myCanvasView setNeedsDisplay],然后在myCanvasView -drawInRect:(CGRect)rect方法中绘制此轨道.

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.

类似这样的东西:

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

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

PanIndicator草案:

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

这篇关于如何在iPhone手势中绘制图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆