检查协议方法是否已定义

发布于 2024-12-14 03:01:46 字数 1161 浏览 0 评论 0 原文

UIScrollViewDelegate 有一个新的很棒的方法:

// called on finger up if the user dragged. velocity is in points/second. targetContentOffset may be changed to adjust where the scroll view comes to rest. not called when pagingEnabled is YES
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
                     withVelocity:(CGPoint)velocity 
              targetContentOffset:(inout CGPoint *)targetContentOffset __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0)

但是,这仅在 iOS 5 中可用。对于没有这种方法的 iOS,我想使用分页作为替代方法。所以我有两个选择:

  1. 检查 iOS 版本,我不知道该怎么做,或者
  2. 检查是否为 UIScrollViewDelegate 协议,我也不知道该怎么做

我更愿意以某种方式检查该方法是否在协议中定义,而不是检查 iOS 版本。请注意,执行 respondsToSelector: 检查是不够的,因为实现该协议的类将始终定义它。

UIScrollViewDelegate has a new awesome method:

// called on finger up if the user dragged. velocity is in points/second. targetContentOffset may be changed to adjust where the scroll view comes to rest. not called when pagingEnabled is YES
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView 
                     withVelocity:(CGPoint)velocity 
              targetContentOffset:(inout CGPoint *)targetContentOffset __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0)

However, this is ONLY available in iOS 5. For iOS's without this method, I'd like to use paging as an alternative. So I'm left with two options:

  1. Check the iOS version, which I don't know how to do, or
  2. Check to see if this method is defined for the UIScrollViewDelegate protocol, which I also don't know how to do.

I would prefer to somehow check if the method is defined in the protocol rather than checking the iOS version. Note that doing a respondsToSelector: check won't be adequate since my class implementing the protocol will always define it.

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

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

发布评论

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

评论(1

凉月流沐 2024-12-21 03:01:46
BOOL isAtLeastIOS5 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0;

请参阅如何测试方法的协议? 测试给定方法的协议。

BOOL isAtLeastIOS5 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 5.0;

See How to test a protocol for a method? to test the protocol for a given method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文