动画从屏幕上丢失响应的对象

时间:2011-11-22 18:55:48

标签: objective-c ios uiview uiscrollview uiviewanimation

我有UIViewController。其中是UITableView,它是在原点0,0绘制的。我还有一个UIScrollView0,-80处绘制,因此它不在屏幕上且不可见。

按下菜单按钮后,我将UIViewController的帧设置为80px,以显示UIScrollView

这里的问题是UIScrollView根本没有回应。

如果我在UIScrollView处绘制0,0,它在加载时可见,它可以正常工作。我甚至可以在屏幕上制作动画,然后返回屏幕,没有任何问题。


以下是我的视图在动画之前的

 _____________________________
|                             |  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;
                         }
                     }];

现在我的视图在动画后的看起来像

 _____________________________
|                             |  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,它认为它在屏幕外,因此不会接收任何类型的操作。

这是对的吗?如果有,有办法解决它吗?

1 个答案:

答案 0 :(得分:1)

滚动视图位于原始视图之外,因此您无法对其进行反应。要修复创建足够大的包装器视图以包含整个内容区域(使用scrollView&amp; tableView作为子视图),请将其添加到原始视图,并为此包装器视图设置动画而不是原始视图。

因此封装视图为320x560,原始视图为0,-80。然后根据需要上下移动80。