【问题标题】:UIScrollView touching and listening for eventsUIScrollView 触摸和监听事件
【发布时间】:2012-07-27 19:11:12
【问题描述】:

我在 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 时,滚动视图不会四处移动或放大等。

【问题讨论】:

    标签: iphone ios ipad uiscrollview


    【解决方案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 , its 在 yourSceollView.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 
    

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 如果我将滚动启用设置为 false,似乎仍然可以用两根手指完成滚动。
      • 这让我很惊讶。你是否也需要禁用缩放(通过设置maximumZoomScale=minimumZoomScale?你也可以直接禁用它的panGestureRecognizer
      猜你喜欢
      • 2019-01-27
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2011-07-10
      • 2018-01-19
      • 1970-01-01
      相关资源
      最近更新 更多