0

Is it possible to make a larger point in painting through long press? Because I want to make my line much bigger when I do the long press gesture and use that point to make a line in touches move. I hope this make sense, and this is my code.

-(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();
}

Now how do I insert my long press here?

4

2 回答 2

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%,但我认为它概述了一般策略,即使用手势识别器——它使事情变得更容易。

于 2012-07-24T17:16:17.317 回答
0

一种可能的解决方案:

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

  • UITouchtouches实例变量中保存一个。

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

  • 检查是否设置了实例变量。如果是,则计算保存的触摸和新触摸的时间戳之间的差异。使用此差异确定线宽,然后取消设置实例变量。
于 2012-07-24T11:12:36.063 回答