目标C:通过长按使得点更大

时间:2012-07-24 09:58:32

标签: objective-c ios ios4 core-graphics paint

通过长按可以在绘画中做出更大的贡献吗?因为当我做长按手势时我想让我的线条更大,并且使用该点来触摸移动线条。我希望这是有道理的,这是我的代码。

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//upon moving
{

            UITouch *touch = [touches anyObject];
            previousPoint2 = previousPoint1;
            previousPoint1 = currentTouch;
            currentTouch = [touch locationInView:self.view];


            CGPoint mid1 = midPoint(previousPoint2, previousPoint1); 
            CGPoint mid2 = midPoint(currentTouch, previousPoint1);


            UIGraphicsBeginImageContext(CGSizeMake(1024, 768));
            [imgDraw.image drawInRect:CGRectMake(0, 0, 1024, 768)];
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSetLineCap(context,kCGLineCapRound);
            CGContextSetLineWidth(context, slider.value);
            CGContextSetBlendMode(context, blendMode);
            CGContextSetRGBStrokeColor(context,red, green, blue, 1);
            CGContextBeginPath(context);
            CGContextMoveToPoint(context, mid1.x, mid1.y);//Computation
            CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
            CGContextStrokePath(context);

            imgDraw.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsGetCurrentContext();
}

现在我如何在这里插入长按?

2 个答案:

答案 0 :(得分:1)

您应该在视图中添加UILongPressGestureRecognizer。然后,该识别器应该有一个与之关联的方法,增加点的半径然后绘制它,当手势结束时将半径重置为某个默认的起始值。

尝试这样的事情:

-(void)viewDidLoad
{
    [super viewDidLoad];
    UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(drawAndExpandPoint:)];
    [self addGestureRecognizer:recognizer];
}

然后,在drawAndExpandPoint方法中,您可以执行类似的操作(使用名为radius的ivar具有一些默认值):

-(void)drawAndExpandPoint:(UILongPressGestureRecognizer *)recognizer
{ 
    //Reset radius, if gesture ended
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        radius = DEFAULT_RADIUS;
        return;
    }

    else if (radius <= MAX_RADIUS) {
        radius += RADIUS_INCREMENT;
        //You will have to write this method to draw the point
        [self drawAtPoint:[recognizer locationInView:self.view] withRadius:radius];
    }
}

此代码可能不是您所描述的100%,但我认为它概述了使用手势识别器的一般策略 - 它使事情变得更容易。

答案 1 :(得分:0)

一种可能的解决方案:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

  • UITouch中的touches保存在实例变量中。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

  • 检查是否有实例 变量已设置。如果是,则计算时间戳的差异 保存的触摸和新的触摸。使用此差异确定线宽然后取消设置实例变量。