是否可以通过长按来画出更大的点?因为我想让我的线条在长按手势时变得更大,并使用该点使触摸中的线条移动。我希望这是有道理的,这是我的代码。
-(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();
}
现在如何在此处插入长按?
最佳答案
您应该将 UILongPressGestureRecognizer 添加到您的 View 中。然后,该识别器应该有一个与之关联的方法,该方法会增加点的半径,然后绘制它,并在手势结束时将半径重置为某个默认起始值。
尝试这样的事情:
-(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% 你所描述的,但我认为它概述了一般策略,即使用手势识别器 - 它使事情变得更容易。
关于objective-c - objective-c : making point Larger through long press,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11628427/