iOS:transitionFromView - 未显示“toView”中的标签

时间:2012-05-07 19:04:35

标签: objective-c ios animation subview uiviewanimationtransition

当在UIView容器中的两个子视图之间切换时,我有一个奇怪的错误。 请注意,两个子视图具有相同的视图控制器类型(相同视图),唯一的区别是标签,它由属性定义。

从box1切换(动画)时 - > box2,box2中的标签文本未显示。但是,如果我除了在box2上设置标签文本外,还要更改视图的背景颜色,标签文本会在动画后显示。

在我看来,就像绘制box2时一样,当只定义标签文本时。怎么样?

viewDidLoad中

// Define the to sub vies and the container
box1 = [[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil];
box2 = [[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil];
containerView = [[UIView alloc] initWithFrame:PercentageGroupView.frame];

[containerView addSubview:box1.view];
[self.view addSubview:containerView];

viewWillAppear中

box1.title = "Box nr. 1";
box2.title = "Box nr. 2";

segmentedControlChanged

[UIView transitionFromView:box1.view
                    toView:box2.view
                  duration:1
                   options:UIViewAnimationOptionTransitionFlipFromBottom 
                completion:nil];

我忘记了什么吗?

1 个答案:

答案 0 :(得分:0)

所以,我设置了以下场景:

ViewController.h

#import <UIKit/UIKit.h>
#import "MyBoxViewController.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) MyBoxViewController *activeBox;
@property (strong, nonatomic) MyBoxViewController *box1;
@property (strong, nonatomic) MyBoxViewController *box2;
- (IBAction)SwitchViews:(id)sender;
@end

ViewController.m

#import "ViewController.h"
@implementation ViewController
@synthesize activeBox;
@synthesize box1;
@synthesize box2;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Define the to sub views and the container
    [self setBox1:[[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]];
    [[[self box1] view] setBackgroundColor:[UIColor blueColor]];
    [[[self box1] titleLabel] setText:@"Box Number 1"];
    [[[self box1] view] setFrame:CGRectMake(20, 20, 200, 200)];
    [self setBox2:[[MyBoxViewController alloc] initWithNibName:@"MyBoxViewController" bundle:nil]];
    [[[self box2] view] setBackgroundColor:[UIColor orangeColor]];
    [[[self box2] titleLabel] setText:@"Box Number 2"];
    [[[self box2] view] setFrame:CGRectMake(40, 40, 200, 200)];
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
    [containerView addSubview:[[self box1] view]];
    [self setActiveBox:[self box1]];
    [[self view] addSubview:containerView];
}
- (IBAction)SwitchViews:(id)sender {
    if ([self activeBox] == [self box1]) {
        // switch from 1 to 2
        [UIView transitionFromView:[[self box1] view]
                            toView:[[self box2] view]
                          duration:1
                           options:UIViewAnimationOptionTransitionFlipFromBottom 
                        completion:nil];
        [self setActiveBox:[self box2]];
    } else {
        // switch from 2 to 1
        [UIView transitionFromView:[[self box2] view]
                            toView:[[self box1] view]
                          duration:1
                           options:UIViewAnimationOptionTransitionFlipFromBottom 
                        completion:nil];
        [self setActiveBox:[self box1]];
    }
}
@end

MyBoxViewController.h

#import <UIKit/UIKit.h>
@interface MyBoxViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@end

MyBoxViewController.m

#import "MyBoxViewController.h"
@implementation MyBoxViewController
@synthesize titleLabel;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

请注意,在我的ViewController.xib上,我添加了一个与UIButton绑定的-(IBAction)SwitchViews:(id)sender

唯一突出的是,当我激活[UIView transition...]时,我设置的帧被保留。我没有在您指定帧的代码中看到任何位置。

除此之外,在我的代码中,我得到一个蓝色矩形(20,20,200,200)并点击按钮翻转它以显示橙色矩形(40,40,200,200)。

这对你有帮助吗?