1

我在 UIScrollView 中有一个图像,并且我有在用户拖动手指时绘制文本的代码。我希望能够控制,当用户触摸屏幕时,如果

a) 用户应该用他们的手指来画画或者

b) 用户应该用手指移动滚动视图

我有一个布尔值来跟踪用户在做什么。我有接触方法:

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    if(draw == false) {
        printf("calling super");
        [scrollView touchesBegan:touches withEvent:event];
    }
    else
        [myPath moveToPoint:[mytouch locationInView:self]];

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
    if(draw == false)
        [scrollView touchesMoved:touches withEvent:event];
    else {
        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];
    }

为什么这不起作用?基本上,如果我不画画,我会调用滚动视图的 touchesBegan 和 touchesMoved。如果我正在绘画,我会使用 myPath 进行绘画。

但是,当 draw 为 false 时,滚动视图不会四处移动或放大等。

4

2 回答 2

1

我以前遇到过这个问题。我这样解决:

你应该做一个 mainView 。它有2个属性。一个是 yourScrollView ,一个是 yourDrawImageView 。[yourScrollView addSubviews:yourDrawImageView];[mainView addSubviews:yourScrollView];

然后像这样在 mainView.m 中编写你的 touches 方法(忽略 scrollView 语句)

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
   if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]]) 
   {

        [myPath moveToPoint:[mytouch locationInView:self]];
   }

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

    UITouch *mytouch=[[touches allObjects] objectAtIndex:0];
   if ([[touches allObjects] isKindOfClass:[yourDrawImageView class]]) 
   {

        [myPath addLineToPoint:[mytouch locationInView:self]];
        [self setNeedsDisplay];
   }


}

最后一步,s you ignor is to code scrollView touches event , ityourSceollView.m 中写的是这样的:

#import "yourScrollView.h"


@implementation yourScrollView


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    if(!self.dragging)
        [[self nextResponder] touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if(!self.dragging)
        [[self nextResponder] touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    if(!self.dragging)
        [[self nextResponder] touchesEnded:touches withEvent:event];
}

- (void)dealloc {
    [super dealloc];
}


@end 

我希望它可以帮助你:-)

于 2012-07-27T23:51:09.573 回答
0

UIScrollView使用UIGestureRecognizers,系统分别调用 stouchesBegan:withEvent:和好友。一种更简单的方法是在您不希望滚动视图对触摸做出反应时简单地禁用滚动视图。

于 2012-07-27T19:15:25.400 回答