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



0 0
原创粉丝点击