我在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时,滚动视图不会像应有的那样移动或放大。

最佳答案

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

您应该制作一个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];
   }


}


最后一步,在yourSceollView.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


希望它可以对您有所帮助:-)

关于iphone - UIScrollView触摸和监听事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11694208/

10-13 03:59