我有一个 UIViewController
。其中有一个 UITableView
,它在原点 0,0
处绘制。我还有一个 UIScrollView
绘制在 0,-80
处,因此它在屏幕外并且不可见。
按下菜单按钮时,我将 UIViewController
的框架向下移动 80 像素以显示 UIScrollView
。
这里的问题是 UIScrollView
根本没有响应。
如果我在 0,0
处绘制 UIScrollView
,它在加载时可见,它工作正常。我什至可以在屏幕外对其进行动画处理,然后毫无问题地返回屏幕。
这是我的 View 在动画之前的样子:
_____________________________
| | Frame -> (0,-80, 320, 80)
| ScrollView | **Offscreen**
|_____________________________|
| | <- Original view (0,0,320,480)
| |
| |
| |
| |
| |
| Original View |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|_____________________________|
当我制作动画时,我会执行以下操作:
[UIView animateWithDuration:0.3
delay:0.0
options:(UIViewAnimationOptionAllowUserInteraction |
UIViewAnimationOptionCurveEaseIn)
animations:^{
self.view.frame = CGRectMake(0.0, (self.view.frame.origin.y + container.frame.size.height), self.view.frame.size.width, self.view.frame.size.height);
scroll.userInteractionEnabled = YES;
}
completion:^(BOOL finished) {
if (scrollLoaded == NO) {
[self loadFavorites];
scrollLoaded = YES;
}
}];
现在我的 View 在动画后看起来像这样:
_____________________________
| | Frame -> (0, -80, 320, 80)
| ScrollView |
|_____________________________|
| | <- Original view (0, 80, 320, 480)
| |
| |
| |
| |
| |
| Original View |
| |
| |
| |
| |
| |
| |
| |
|_____________________________|
| |
| **Offscreen** |
|_____________________________|
我已经将我的 scrollView
子类化以更仔细地监听事件:
.h
#import <UIKit/UIKit.h>
@interface UIScrollView_ExtraTouch : UIScrollView
@end
.m
#import "UIScrollView+ExtraTouch.h"
@implementation UIScrollView_ExtraTouch
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"scrollview touchesBegan");
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"scrollview touchesMoved");
if(!self.dragging){
[self.nextResponder touchesMoved:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"scrollview touchesEnded");
[self.nextResponder touchesEnded:touches withEvent:event];
}
@end
动画之后没有任何内容进入日志。我认为 意味着由于框架位于 -80
,它认为它不在屏幕上,因此不会收到任何类型的操作。
这是正确的吗?如果是这样,有什么办法可以解决吗?
最佳答案
ScrollView 在原始 View 之外,因此您无法对其使用react。要解决此问题,请创建一个足够大以包含整个内容区域的包装 View (将 scrollView 和 tableView 都作为 subview ),将其添加到原始 View ,并为该包装 View 而不是原始 View 设置动画。
因此包装 View 将为 320x560,原始 View 中为 0,-80。然后根据需要上下移动 80。
关于objective-c - 从屏幕外响应丢失动画对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8232207/