UIScrollView触摸和收听事件

时间:2012-07-27 19:11:13

标签: iphone ios ipad uiscrollview

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

a)用户应该使用他们的手指绘制OR

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时,滚动视图不会移动或放大等,应该是这样。

2 个答案:

答案 0 :(得分: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];
   }


}

最后一步,你在theSceollView.m中写的是s you ignor is to code scrollView touches event , it,如下所示:

#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 

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

答案 1 :(得分:0)

UIScrollView使用UIGestureRecognizer s,由系统与touchesBegan:withEvent:和朋友分开调用。更简单的方法是,当您不希望它对触摸作出反应时,只需在滚动视图上禁用滚动。