【问题标题】:touchesMoved no longer triggers after changing UIView to UIScrollView?将 UIView 更改为 UIScrollView 后 touchesMoved 不再触发?
【发布时间】:2013-06-10 01:28:31
【问题描述】:

我有一个包含很多对象的 UIView。我也有一个这样的 touchesMoved 实现:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"move");
}

我最近想让视图滚动,所以我只是打开了 UIView,并在 Interface Builder 中将其对象类更改为 UIScrollView。但是,现在即使我触摸屏幕也不会调用“touchesMoved”方法。

有人可以帮我让 touchesMoved 重新工作吗?我不确定我做了什么来破坏它!

编辑:我尝试关注this guide,但我可能做错了什么。从阅读其他帖子看来,UIScrollView 本身不能接受触摸事件,并且需要将它们发送到响应者链上?我将非常感谢任何可以帮助指导我解决这个问题的人......当我意识到 UIScrollView 杀死了我的触摸检测时,我的应用程序即将提交! (我刚刚将我的应用程序 UIView 更改为 UIScrollView 以允许与 iPhone 4 兼容)。

【问题讨论】:

  • 您的问题类似于this one。也许其中一项建议可能对您有用?
  • 感谢您的链接。他们建议设置 _mainScroller.delaysContentTouches=NO;和 _mainScroller.canCancelContentTouches=NO;这两个我都做了,我的触摸仍然没有被检测到。

标签: xcode uiview uiscrollview touchesmoved


【解决方案1】:

我刚刚在您的编辑中查看了the guide,我想我可以看到问题所在。有关类似问题,请参阅 this answer

您的 UIScrollView 子类将如下所示:

#import "AppScrollView.h"

@implementation AppScrollView

- (id)initWithFrame:(CGRect)frame
{
    return [super initWithFrame:frame];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"AppScrollView touchesEnded:withEvent:");

    // If not dragging, send event to next responder
    if (!self.dragging)
        [[self.nextResponder nextResponder] touchesEnded:touches withEvent:event];
    else
        [super touchesEnded: touches withEvent: event];

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"AppScrollView touchesMoved:withEvent:");

    [[self.nextResponder nextResponder] touchesMoved:touches withEvent:event];
}

@end

您的包含AppScrollView 对象的类应该采用UIScrollViewDelegate 协议并实现这些方法:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"SomeClass touchesMoved:withEvent:");
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"SomeClass touchesEnded:withEvent:");
}

那么记录的输出将如下所示:

2013-06-11 10:45:21.625 Test[54090:c07] AppScrollView touchesMoved:withEvent:
2013-06-11 10:45:21.625 Test[54090:c07] SomeClass touchesMoved:withEvent:
2013-06-11 10:45:21.642 Test[54090:c07] AppScrollView touchesMoved:withEvent:
2013-06-11 10:45:21.642 Test[54090:c07] SomeClass touchesMoved:withEvent:
2013-06-11 10:45:21.655 Test[54090:c07] AppScrollView touchesEnded:withEvent:
2013-06-11 10:45:21.656 Test[54090:c07] SomeClass touchesEnded:withEvent:

【讨论】:

  • 非常感谢您的回复...我今天下班后试一试并报告!
  • 嗯。有用。尽管使用单个 nextResponder 调用它不起作用。知道这是为什么吗?
猜你喜欢
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
相关资源
最近更新 更多