iOS 3.1.3 中的 UIGestureRecognizer?

发布于 2024-10-19 08:22:59 字数 1280 浏览 5 评论 0原文

我正在努力使现有的 iPhone/iPad 项目向后兼容到 iPhoneOS 3.0。

我当前的测试设备是 iPod Touch,其版本为 3.1.3。

以下代码引发了问题:

Class gestureRecognizer = NSClassFromString(@"UISwipeGestureRecognizer");

if (gestureRecognizer != nil)
{

    UISwipeGestureRecognizer * leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                                                               action:@selector(didSwipeLeft:)];
    leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    leftSwipeRecognizer.delegate = self;
    [self.view addGestureRecognizer:leftSwipeRecognizer];
    _leftSwipeRecognizer = leftSwipeRecognizer;
}

根据 Apple 文档,UIGestureRecognizer 是从 iOS 3.2 开始定义的。因此,我希望在以前的操作系统版本上,ClassgestureReconizernil,并且跳过以下 if 。但它不会跳过。 gestureRecognizer 不是 nilif 内的代码开始执行并在 leftSwipeRecognizer.direction 处崩溃,因为:

-[UISwipeGestureRecognizer setDirection:]: unrecognized selector sent to instance 0x1e5720

这种情况相当混乱。我想我所做的一切都是照章办事。我尝试在使用某个类之前检查它是否存在,但是不应该存在的类却存在,欺骗了我的测试,不符合其预期规范,并且崩溃。

当然,我可以在各处进行一些 respondsToSelector 检查来解决此崩溃问题,但这不是一种优雅的方法。

还有其他建议吗?

I am working on making an existing iPhone/iPad project backwards compatible down to iPhoneOS 3.0.

My current test device is an iPod Touch with 3.1.3 on it.

The following bit of code is causing problems:

Class gestureRecognizer = NSClassFromString(@"UISwipeGestureRecognizer");

if (gestureRecognizer != nil)
{

    UISwipeGestureRecognizer * leftSwipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self 
                                                                                               action:@selector(didSwipeLeft:)];
    leftSwipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    leftSwipeRecognizer.delegate = self;
    [self.view addGestureRecognizer:leftSwipeRecognizer];
    _leftSwipeRecognizer = leftSwipeRecognizer;
}

According to Apple documentation UIGestureRecognizer is defined starting from iOS 3.2. So I expect Class gestureReconizer to be nil on previous OS version and that the following if to be skipped. However it does not skip. gestureRecognizer is not nil, the code inside the if starts executing and crashes at leftSwipeRecognizer.direction because:

-[UISwipeGestureRecognizer setDirection:]: unrecognized selector sent to instance 0x1e5720

This situation is quite confusing. I guess I am doing everything by the book. I try to check if a class exists before I use it, however the class which shouldn't be there, is there, fools my test, does not comply with its expected specs, and crashes.

I could, of course put a few respondsToSelector checks here and there to work around this crash, but it wouldn't be an elegant way to do it.

Any other suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天 2024-10-26 08:22:59

根据 UIGestureRecognizer 类参考,在 "使用特殊注意事项",在检查该类是否存在后,您实际上确实需要进行额外的respondsToSelector检查。

这直接来自文档:

判断一个类是否是
在给定的 iOS 中运行时可用
发布时,您通常会检查是否
班级为零。不幸的是,这
测试并不完全准确
UIGestureRecognizer。虽然这
课程开始公开
iOS 3.2 正在开发中
在此之前的一小段时间。虽然
该类存在于较早的版本中
释放、使用和其他
手势识别器类不是
在早期版本中受支持。你
不应尝试使用以下实例
这些课程。

在运行时确定您是否
可以在您的中使用手势识别器
应用程序,测试是否该类
存在,如果存在,则分配一个
实例并检查它是否响应
到选择器 locationInView:。这
方法未添加到类中
直到iOS 3.2。代码可能看起来
像下面这样:

UIGestureRecognizer *gestureRecognizer = [[UIGestureRecognizer alloc] 
    initWithTarget:self action:@selector(myAction:)];

if (![gestureRecognizer respondsToSelector:@selector(locationInView:)]) {
    [gestureRecognizer release];
    gestureRecognizer = nil;
}
// do something else if gestureRecognizer is nil

According to the UIGestureRecognizer class reference, under "Usage Special Considerations", you actually do need to do an additional respondsToSelector check after checking if the class exists.

This is directly from the documentation:

To determine whether a class is
available at runtime in a given iOS
release, you typically check whether
the class is nil. Unfortunately, this
test is not cleanly accurate for
UIGestureRecognizer. Although this
class was publicly available starting
with iOS 3.2, it was in development a
short period prior to that. Although
the class exists in an earlier
release, use of it and other
gesture-recognizer classes are not
supported in that earlier release. You
should not attempt to use instances of
those classes.

To determine at runtime whether you
can use gesture recognizers in your
application, test whether the class
exists and, if it does, allocate an
instance and see check if it responds
to the selector locationInView:. This
method was not added to the class
until iOS 3.2. The code might look
like the following:

UIGestureRecognizer *gestureRecognizer = [[UIGestureRecognizer alloc] 
    initWithTarget:self action:@selector(myAction:)];

if (![gestureRecognizer respondsToSelector:@selector(locationInView:)]) {
    [gestureRecognizer release];
    gestureRecognizer = nil;
}
// do something else if gestureRecognizer is nil
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文