我想像这样在 UIView 动画期间获得滚动contentOffset
视图contentInset
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(100, 100, 120, 100)];
scrollView.backgroundColor = [UIColor grayColor];
scrollView.contentSize = scrollView.frame.size;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"hello world";
[scrollView addSubview:label];
UIViewController *vc = [[UIViewController alloc] init];
[vc.view addSubview:scrollView];
[scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:NULL];
[scrollView addObserver:self forKeyPath:@"contentInset" options:NSKeyValueObservingOptionNew context:NULL];
[UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
scrollView.contentInset = UIEdgeInsetsMake(50, 0, 0, 0);
scrollView.contentOffset = CGPointMake(0, -50);
} completion:nil];
self.window.rootViewController = vc;
return YES;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentOffset"]) {
NSLog(@"offset:%@", [change valueForKey:NSKeyValueChangeNewKey]);
}
if ([keyPath isEqualToString:@"contentInset"]) {
NSLog(@"inset:%@", [change valueForKey:NSKeyValueChangeNewKey]);
}
}
不幸的是,动画时没有输出,是我错过了什么还是 KVO 在做 UIView 动画时不起作用?