所以,现在我只使用KVO。我遵循了这里的一些建议。
在viewDidLoad
:
[self.view addObserver:self
forKeyPath:NSStringFromSelector(@selector(frame))
options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
context:nil];
然后:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([object isKindOfClass:[UIScrollView class]] && [keyPath isEqualToString:NSStringFromSelector(@selector(frame))]) {
CGRect newFrame = [change[@"new"] CGRectValue];
CGRect oldFrame = [change[@"old"] CGRectValue];
if ((newFrame.size.width == oldFrame.size.width) || (newFrame.size.height == oldFrame.size.height)) {
// If one dimension remained constant, we assume this is a displayMode change instead of a rotation
// Make whatever changes are required here, with access to new and old frame sizes.
}
}
}
我在视图的边界上尝试了这个,但它比框架上的 KVO 更频繁地触发。