作业

来源:互联网 发布:新淘宝账号可以开店吗 编辑:程序博客网 时间:2024/05/22 12:15


1.两个Label一个向下移动,一个向右移动



#import "ViewController.h"


@interface ViewController ()

@property(strong,nonatomic)UIButton *button;

@property(strong,nonatomic)UILabel *label1;

@property(strong,nonatomic)UILabel *label2;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfcreateButton];

    [selfcreateLabel];

    

    

}


- (void)createButton

{

    _button = [UIButtonbuttonWithType:UIButtonTypeSystem];

    _button.frame =CGRectMake(100,100, 100,30);

    [_buttonsetTitle:@"点击我"forState:UIControlStateNormal];

    _button.backgroundColor = [UIColorgreenColor];

    [_buttonaddTarget:selfaction:@selector(tapButton:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:_button];

}


- (void)createLabel

{

    _label1 = [[UILabelalloc]initWithFrame:CGRectMake(10,0, 100,30)];

    _label1.text =@"Label1";

    _label1.backgroundColor = [UIColorredColor];

    [self.viewaddSubview:_label1];

    

    _label2 = [[UILabelalloc]initWithFrame:CGRectMake(150,0, 100,30)];

    _label2.text =@"Label2";

    _label2.backgroundColor = [UIColorredColor];

    [self.viewaddSubview:_label2];

    

    

}

- (void)tapButton:(UIButton*)sender

{

    //获取Label1frame

    CGRect frame1 =_label1.frame;

    frame1.origin.y+=200;

    

    

    CGRect frame2 =_label2.frame;

    frame2.origin.x+=100;

    

    //做动画

    [UIViewanimateWithDuration:1animations:^{

        _label1.frame = frame1;

        //alpha透明度:0~1,0完全透明,

        _label1.alpha =0;

        _label2.frame = frame2;

    }];

    

}



@end


2.点A加1,点B加1,还有清零

#import "ViewController.h"


@interface ViewController ()

@property(strong,nonatomic)UILabel *label;

@property(assign,nonatomic)NSUInteger index1;

@property(assign,nonatomic)NSInteger  index2;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    [selfcreateButtonWith:CGRectMake(20,200, 150,30) title:@"buttonA"tag:1];

    [selfcreateButtonWith:CGRectMake(180,200, 150,30) title:@"buttonB"tag:2];

    [selfcreateButtonWith:CGRectMake(150,250, 100,30) title:@"计数器清零"tag:3];

    

    _label = [[UILabelalloc]initWithFrame:CGRectMake(120,30, 150,30)];

    _label.text =@"000";

    _label.textAlignment =NSTextAlignmentCenter;

    _label.backgroundColor = [UIColorgreenColor];

    [self.viewaddSubview:_label];

    

    //初始值

    _index1 =0;

    _index2 =0;

    

    

}


//包装一个通用的方法,创建按钮使用需要改变的参数即可,节省代码

- (void)createButtonWith:(CGRect)frame

                   title:(NSString *)title

                     tag:(NSUInteger)tag

                    

{

    UIButton *button = [[UIButtonalloc]initWithFrame:frame];

    [button setTitle:titleforState:UIControlStateNormal];

    button.tag = tag;

    button.backgroundColor = [UIColorlightGrayColor];

    [button addTarget:selfaction:@selector(tapButton:)forControlEvents:UIControlEventTouchUpInside];

    [self.viewaddSubview:button];

    

}



- (void)tapButton:(UIButton*)sender

{

    switch (sender.tag) {

        case1://按钮A

        {

            _index1++;

            NSString *str = [NSStringstringWithFormat:@"你点击了A %lu",_index1];

            _label.text = str;//显示在label

        }

            break;

        case2://按钮B

        {

            _index2++;

            NSString *str = [NSStringstringWithFormat:@"你点击了A %lu",_index2];

            _label.text = str;//显示在label

        }

            break;


        case3://按钮C

        {

            _index1 =0;

            _index2 =0;

            _label.text =@"000";

        }

            break;

   

        default:

            break;

    }

}


#pragma mark - 分别指定按钮的事件(不推荐)

- (void)tapA

{

    

}


- (void)tapB

{

    

}


- (void)tapC

{

    

}

@end


0 0
原创粉丝点击