我创建了一个绘图应用程序。由于我在大图像上使用绘图,我也需要滚动,但是当我实现滚动时,绘图不再起作用。
我认为绘图和滚动之间的触摸事件存在冲突。
我想用两根手指滚动并用一根手指画画,但我找不到如何实现它。如果有人能帮助我解决这个问题,我感激不尽。
-(void)viewDidLayoutSubviews
{
scroller.scrollEnabled=YES;
scroller.contentSize=temp.frame.size;
toggledrawandscroll=false;
}
- (IBAction)changescrollstatues:(id)sender {
if(toggledrawandscroll){
//if draw is enable,disable it
toggledrawandscroll =false;
scroller.scrollEnabled=YES;
scroller.canCancelContentTouches=YES;
scroller.contentSize=temp.frame.size;
}
else{
toggledrawandscroll =true;
scroller.scrollEnabled=NO;
scroller.canCancelContentTouches=NO;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//initial setting for touch point
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
//set the concext
UIGraphicsBeginImageContext(self.view.frame.size);
[temp.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
//drawing setting
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),red, green, blue, 1);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
temp.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped) {
//initial seting
UIGraphicsBeginImageContext(self.view.frame.size);
[temp.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
temp.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
答案 0 :(得分:0)
所以看起来你有一个切换来启用滚动?如果是这种情况,您的 changesscrollstatues 方法永远不会启用滚动,并且永远不会将您的布尔值设置为 YES 。您的滚动条始终处于启用状态,因此触摸事件不会被捕获。