1

我有一个包含很多对象的 UIView。我也有一个 touchesMoved 的实现,如下所示:

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

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

有人可以帮我让touchesMoved再次工作吗?我不知道我做了什么来打破它!

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

4

1 回答 1

3

我刚刚看了您编辑中的指南,我想我可以看到问题所在。有关类似问题,请参阅此答案。

您的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:
于 2013-06-11T00:46:32.257 回答