ios - 检查是否定义了协议(protocol)方法

标签 ios objective-c-runtime uiscrollviewdelegate

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 定义了此方法协议(protocol),我也不知道该怎么做

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

最佳答案

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

参见 How to test a protocol for a method?测试给定方法的协议(protocol)。

关于ios - 检查是否定义了协议(protocol)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8053991/

相关文章:

ios驱动sdk |我如何检查每张票,然后取消它?例如如果它有 tag = 1,我需要取消它

objective-c - 使用运行时将弱 ivar 添加到 Objective-C 类

iphone - 在运行时在 iPhone 上解释 Objective C 脚本?

objective-c - ScriptingBridge - 它是如何工作的 "Behind the Scenes"

ios - 如何根据我的方案更改 .plist 条目?

ios - 为 Alamofire 全局设置超时间隔

ios - 只有当 UIWebView 向下滚动一点时,状态栏才会检测到点击

ios - scrollView.dragging == true 在 scrollViewDidEndDragging 之后?

iOS - AVPlayer 不为某些 URL 流式传输音频

swift - 在 ScrollView 中嵌入 PageViewController - 以编程方式 Swift 5