多个容器视图VS链接到单个容器视图的多个视图控制器

时间:2019-01-09 07:52:54

标签: ios objective-c iphone

enter image description here

我有一个具有以下配置的视图。通过单击按钮,相应的容器视图应移动到带有动画的主屏幕。如果我处于纵向或横向模式(仅其中一种),则效果很好,但如果尝试旋转iphone,则无法正常工作。

- (IBAction)button2Pressed:(UIButton *)button {
if(previousButton != button){
    if([previousButton.titleLabel.text isEqualToString:@"1"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width];
    else if([previousButton.titleLabel.text isEqualToString:@"3"])
        [self viewLoadedIntoContainer:self.container1.frame.size.width];
    else
        [self viewLoadedIntoContainer:self.container1.frame.size.width*2];
    previousButton = button;
    [self animateLabel:self.button2Outlet.frame.origin.x];
    indexCount=1;
}}

- (IBAction)button1Pressed:(UIButton *)button {
if(previousButton != button){
    if([previousButton.titleLabel.text isEqualToString:@"2"])
        [self viewLoadedIntoContainer:self.container1.frame.size.width];
    else if([previousButton.titleLabel.text isEqualToString:@"3"])
        [self viewLoadedIntoContainer:self.container1.frame.size.width*2];
    else
        [self viewLoadedIntoContainer:self.container1.frame.size.width*3];

    previousButton = button;

    [self animateLabel:self.button1Outlet.frame.origin.x];
    indexCount=0;
}}

- (IBAction)button3Pressed:(UIButton *)button {
if(previousButton != button){
    if([previousButton.titleLabel.text isEqualToString:@"1"])
       [self viewLoadedIntoContainer:-self.container1.frame.size.width*2];
    else if([previousButton.titleLabel.text isEqualToString:@"2"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width];
    else
       [self viewLoadedIntoContainer:self.container1.frame.size.width];
    previousButton = button;

    [self animateLabel:self.button3Outlet.frame.origin.x];
    indexCount=2;
}}

- (IBAction)button4Pressed:(UIButton *)button {
if(previousButton != button){
    if([previousButton.titleLabel.text isEqualToString:@"3"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width];
    else if([previousButton.titleLabel.text isEqualToString:@"2"])
        [self viewLoadedIntoContainer:-self.container1.frame.size.width*2];
    else
        [self viewLoadedIntoContainer:-self.container1.frame.size.width*3];
    previousButton = button;
    [self animateLabel:self.button4Outlet.frame.origin.x];
    indexCount=3;
}}

-(void)viewLoadedIntoContainer:(CGFloat)x{
 __weak __typeof(self) weakSelf = self;
[UIView animateWithDuration:0.3f animations:^{

    weakSelf.container1.layer.position = CGPointMake(weakSelf.container1.layer.position.x+(x), weakSelf.container1.layer.position.y);
    weakSelf.container2.layer.position = CGPointMake(weakSelf.container2.layer.position.x+(x), weakSelf.container2.layer.position.y);
    weakSelf.container3.layer.position = CGPointMake(weakSelf.container3.layer.position.x+(x), weakSelf.container3.layer.position.y);
    weakSelf.container4.layer.position = CGPointMake(weakSelf.container4.layer.position.x+(x), weakSelf.container4.layer.position.y);
    //[weakSelf.container1 setFrame:CGRectMake(weakSelf.container1.frame.origin.x+(x), weakSelf.container1.frame.origin.y, weakSelf.container1.frame.size.width, weakSelf.container1.frame.size.height)];

    //[weakSelf.container2 setFrame:CGRectMake(weakSelf.container2.frame.origin.x+(x), weakSelf.container2.frame.origin.y, weakSelf.container2.frame.size.width, weakSelf.container2.frame.size.height)];

    //[weakSelf.container3 setFrame:CGRectMake(weakSelf.container3.frame.origin.x+(x), weakSelf.container3.frame.origin.y, weakSelf.container3.frame.size.width, weakSelf.container3.frame.size.height)];

    //[weakSelf.container4 setFrame:CGRectMake(weakSelf.container4.frame.origin.x+(x), weakSelf.container4.frame.origin.y, weakSelf.container4.frame.size.width, weakSelf.container4.frame.size.height)];

}completion:^(BOOL finished) {
    //[weakSelf.container1 layoutIfNeeded];
}];

}

我正在使用4个容器视图。我应该使用4个高度和宽度相同且彼此重叠的容器视图。还是应该使用自定义segue链接到单个容器视图的4视图控制器?

1 个答案:

答案 0 :(得分:0)

这确实取决于每个视图/视图控制器的具体功能。

但是一般来说;我通常将每个“屏幕”(全屏视图)都设置为自己的UIViewController。我通常对屏幕UIViews做子视图。