OC 实现5升6升桶装3L水,通过定时器分步实现
来源:互联网 发布:win8.1安装java 编辑:程序博客网 时间:2024/04/27 21:04
核心思想水只往5L桶里加注
代码如下:
#import "AddWaterViewController.h"
@interface AddWaterViewController ()
{
CGFloat a;
CGFloat b;
CGFloat z;
CGFloat ta;
CGFloat tb;
CGFloat totalWater;
NSTimer *timer;
UIView *aZhu;
UIView *bZhu;
UIView *zZhu;
CGFloat bottomPointY;
NSInteger countTime;
UILabel *aLabel;
UILabel *bLabel;
UILabel *pLabel;
UIView *pView;
}
@end
@implementation AddWaterViewController
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
[self.navigationControllersetNavigationBarHidden:NO];
}
- (void)viewDidLoad {
[superviewDidLoad];
self.title =@"5L6L桶量出3L水";
self.view.backgroundColor =[UIColorwhiteColor];
countTime =0;
bottomPointY =200;
a = 0;
b = 0;
z = 30;
ta = 50;
tb = 60;
aZhu =[[UIViewalloc]initWithFrame:CGRectMake(10,bottomPointY-a,20, a)];
aZhu.backgroundColor =[UIColorredColor];
[self.viewaddSubview:aZhu];
aLabel =[[UILabelalloc]initWithFrame:CGRectMake(0,bottomPointY,40, 20)];
aLabel.textAlignment =NSTextAlignmentCenter;
[self.viewaddSubview:aLabel];
bZhu =[[UIViewalloc]initWithFrame:CGRectMake((self.view.frame.size.width-20)*0.5,bottomPointY-b,20, b)];
bZhu.backgroundColor =[UIColorredColor];
[self.viewaddSubview:bZhu];
bLabel =[[UILabelalloc]initWithFrame:CGRectMake((self.view.frame.size.width-40)*0.5,bottomPointY, 40,20)];
bLabel.textAlignment =NSTextAlignmentCenter;
[self.viewaddSubview:bLabel];
pView =[[UIViewalloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)*0.5,bottomPointY+200-totalWater,50, totalWater)];
pView.backgroundColor =[UIColorblueColor];
[self.viewaddSubview:pView];
pLabel =[[UILabelalloc]initWithFrame:CGRectMake((self.view.frame.size.width-40)*0.5,bottomPointY+200,40, 20)];
pLabel.textAlignment =NSTextAlignmentCenter;
[self.viewaddSubview:pLabel];
pLabel.text = [NSStringstringWithFormat:@"%.0f",totalWater];
UIView *cZhu =[[UIViewalloc]initWithFrame:CGRectMake((self.view.frame.size.width-30),bottomPointY-z,20, z)];
cZhu.backgroundColor =[UIColoryellowColor];
[self.viewaddSubview:cZhu];
UILabel *cLabel =[[UILabelalloc]initWithFrame:CGRectMake((self.view.frame.size.width-40),bottomPointY, 40,20)];
cLabel.textAlignment =NSTextAlignmentCenter;
[self.viewaddSubview:cLabel];
cLabel.text = [NSStringstringWithFormat:@"%.0f",z];
timer = [NSTimerscheduledTimerWithTimeInterval:2target:selfselector:@selector(daoshui)userInfo:nilrepeats:YES];
// Do any additional setup after loading the view.
}
-(void)daoshui{
if (a !=z) {
if(a ==0) {
a =ta;
countTime++;
[aZhusetFrame:CGRectMake(10,bottomPointY-a,20, a)];
}
if(a +b > tb) {
int temp =tb - b;
if (temp!=0) {
b =tb;
}else{
totalWater+=b;
b =0;
[pViewsetFrame:CGRectMake((self.view.frame.size.width-50)*0.5,bottomPointY+200-totalWater,50, totalWater)];
pLabel.text = [NSStringstringWithFormat:@"%.0f",totalWater];
}
a =a - temp;
[aZhusetFrame:CGRectMake(10,bottomPointY-a,20, a)];
[bZhusetFrame:CGRectMake((self.view.frame.size.width-20)*0.5,bottomPointY-b,20, b)];
bLabel.text = [NSStringstringWithFormat:@"%.0f",b];
aLabel.text = [NSStringstringWithFormat:@"%.0f",a];
} else {
b =b + a;
a =0;
[aZhusetFrame:CGRectMake(10,bottomPointY-a,20, a)];
[bZhusetFrame:CGRectMake((self.view.frame.size.width-20)*0.5,bottomPointY-b,20, b)];
bLabel.text = [NSStringstringWithFormat:@"%.0f",b];
aLabel.text = [NSStringstringWithFormat:@"%.0f",a];
}
}else{
NSLog(@"%ld",countTime);
[timerinvalidate];
}
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
- OC 实现5升6升桶装3L水,通过定时器分步实现
- 桶装水
- 中国企业实现SOA “分步走”
- JAVA定时器实现之一(通过继承TimerTask)
- 通过WebViewJavascriptBridge实现OC与JS交互
- OC中通过协议实现多继承
- Spring MVC实现分步用户注册
- 分步加载流驱动实现启动提速
- German 实现异步分步式任务
- 分步式文件存储系统技术及实现
- leetcode桶装水问题
- 定时器 实现
- 定时器实现
- 单片机通过定时器来实现多任务,充分利用定时器资源来实现异步处理
- 二进制流分步写入数据库实现(SPS中实现)
- python通过线程实现定时器timer的代码
- ls -l 实现
- ls -l 命令实现
- 虚拟机安装linux的Vmwaretools
- 《Algorithm》读书笔记-1
- JS不间断横/纵向滚动
- Linux常用命令
- 关于安卓微信支付提示未知错误,而IOS正常的处理问题
- OC 实现5升6升桶装3L水,通过定时器分步实现
- 函数是一等公民与纯函数
- 四轴飞行diy全套入门教程(从最基础的开始)
- ios developer tiny share-20160706
- POJ 3069
- 新手菜鸟潇关于winform数据库连接问题
- JavaMail发送邮件AuthenticationFailedException异常解决
- java学习路程
- maven 系统找不到指定的路径