移动 superView 中的 containerView 作为子视图

Moving containerView in superView as a subview

下图描述了场景。 http://i.stack.imgur.com/PPm4e.png

按下动画按钮时,蓝屏(或 containerView)应该从视图中移开,同样在按下时返回到初始位置。

代码有效。但是 containerView 的行为并不像其他视图的子视图。它不应该从浅灰色视图中出来。这是它的超级视图。它应该在其范围内上下移动。

模拟器截图如下:

http://i.stack.imgur.com/SJjA5.png

http://i.stack.imgur.com/I98W8.png

非常感谢任何建议。

ViewController.m :-

@interface ViewController ()
{
    BOOL scrollUp;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.containerBackgroundView addSubview:self.containerview];

    scrollUp = NO;
    [self animate:self];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}


- (IBAction)animate:(id)sender {

    if (scrollUp) {
        [self rollUpMenuPage];
        scrollUp = NO;
    }
    else
    {
        scrollUp = YES;
        [self unRollTheMenuPage];
    }

}

-(void)unRollTheMenuPage
{

    NSLog(@"containerview frame before unRoll : %@",[self.containerview description]);

    CGRect rect = CGRectMake(self.containerview.frame.origin.x, 0.0f, self.containerview.frame.size.width, self.containerview.frame.size.height);


    [UIView animateWithDuration:1.0f
                     animations:^{

                         self.containerview.frame = rect;
                     }
                     completion:^(BOOL finished){
                         // do something here if you wish.

                         NSLog(@"containerview frame after unRoll : %@",[self.containerview description]);

                     }];

}

-(void)rollUpMenuPage
{

    NSLog(@"containerview frame before roll up : %@",[self.containerview description]);

    CGRect rect = CGRectMake(self.containerview.frame.origin.x, -self.containerBackgroundView.frame.size.height, self.containerview.frame.size.width, self.containerview.frame.size.height);


    [UIView animateWithDuration:1.0f
                     animations:^{
                         self.containerview.frame = rect;
                     }
                     completion:^(BOOL finished){

                         NSLog(@"containerview frame after roll up : %@",[self.containerview description]);

                     }];


}

ViewController.h

@property (weak, nonatomic) IBOutlet UIView *containerBackgroundView;
@property (weak, nonatomic) IBOutlet UIView *containerview;

当前输出:- http://screencast.com/t/bfLmJFYmym4

问题是,当向上移动时,它不应该超过浅灰色视图。

谢谢

在我看来,您似乎在顶部添加了一个 UINavigationBar(或其他一些工具栏)作为 UIViewController 视图的子视图,并且 containerBackroundView(透明 bg 颜色)位于该栏的顶部。 建议您更改 containerBackgroundView 的 frame.origin.y,使其不会覆盖那里的顶部栏或位于状态栏下方。

好的,在重新创建您的项目并对其进行测试后,您是正确的视图 si 在它的 superView 边界之外绘制。

要撤消此操作,您可以单击后视图中的 clip subviews 属性,这样当 containerView 超出后视图边界时,它将被剪裁并且不会被绘制