【问题标题】:Changing views in a view Controller在视图控制器中更改视图
【发布时间】:2014-03-10 06:52:01
【问题描述】:

所以我想制作一个像 7 分钟锻炼这样的应用程序,所以在锻炼期间,视图会更改为不同的锻炼,通常由图像、计时器和显示锻炼名称的标签组成。这一切都是自动发生的,所以一个例子是,

查看 1. 显示一个人做俯卧撑的图像,显​​示一个 30 秒的计时器,标题称为表格。 30 秒后,它会转换到新视图。 查看 2. 同上,但图片和标题不同 查看3.....等等。

我认为没有必要为所有练习制作不同的视图控制器,所以你们对我需要做什么来制作这样的东西有什么建议吗?

【问题讨论】:

  • 你是对的,你不需要多于 1 个视图控制器。在您当前的视图控制器中,只需在 30 秒后淡出此视图并再次淡入新输入即可获取一个视图(在此添加您的图像和标题)。如果您需要示例,请告诉我
  • @CharanGiri 你的意思是在 30 秒后更改文本和图像吗?
  • 是的,这样您将使用更少的内存,并且可以重复使用元素而无需再次分配它
  • @CharanGiri,你能给我一个代码 sn-p 你的意思吗,谢谢

标签: ios objective-c uilabel


【解决方案1】:

试试这个

#import "ViewController.h"

@interface ViewController ()
{
UIView *baseView;
UILabel *titleLabel;
UIImageView *viewforPic;
float fadeDuration;
NSMutableArray *infoArray ;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];


NSArray *imagesArray=[[NSArray alloc]initWithObjects:@"red.png",@"blue.png",@"yellow.png",  nil];
NSArray *titleArray =[[NSArray alloc]initWithObjects:@"red",@"blue",@"yellow", nil];

infoArray =[[NSMutableArray alloc]init];
for (int i=0; i<imagesArray.count; i++) {
    NSMutableDictionary *dict =[[NSMutableDictionary alloc]init];
    [dict setObject:imagesArray[i] forKey:@"images"];
    [dict setObject:titleArray[i] forKey:@"titles"];
    [infoArray addObject:dict];
  //  [dict removeObjectsForKeys:keys];
}
NSLog(@"%@",infoArray);

baseView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 380)];
[self.view addSubview:baseView];
titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
titleLabel.textAlignment=NSTextAlignmentCenter;
[baseView addSubview:titleLabel];
viewforPic =[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, 320, 320)];
[baseView addSubview:viewforPic];

[self showAnimationwithinfor:0];

}

-(void)showAnimationwithinfor :(int)tempIndex
{
titleLabel.text=[infoArray[tempIndex] objectForKey:@"titles"];
viewforPic.backgroundColor= [UIColor redColor];  //[temp[0] objectForKey:@"images"];

[UIView animateWithDuration:10.0f animations:^{
    baseView.alpha = 1.0f;
} completion:^(BOOL finished){
    [UIView animateWithDuration:10.0f animations:^{
        baseView.alpha = 0.0f;
        if (infoArray.count>tempIndex+1) {
         int tempedIndex=tempIndex+1;
            [self showAnimationwithinfor:tempedIndex];
        }
    }];
}];
}
@end

【讨论】:

    【解决方案2】:

    不必更改视图。您也可以在同一个视图上工作。假设您完成了 30 秒的工作,您也可以在同一视图中创建新活动。如果您想移动到新视图,那么您可以将数据从一个视图传递到另一个视图。并打开新视图的代码之一是

    [self.navigationController pushViewController:View animated:YES]; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多