iOS复习main.storybord制作 计算器与运动色块

//计算器

- (IBAction)doSum:(id)sender {

    //数值转换后计算

    int sum = [self.number1.text intValue]+[self.number2.text intValue];

    NSLog(@"计算结果是:%d",sum);

    

    //显示结果

    self.sum.text = [NSString stringWithFormat:@"%d",sum];

    

    //收起键盘

//    [self.number1 resignFirstResponder]; //取消第一响应

//    [self.number2 resignFirstResponder];

    

    [self.view endEditing:NO]; //view不要编辑

    

}


//运动色块

@interface ViewController ()


@property (weak, nonatomic) IBOutlet UIView *fathersView;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}


//修改你视图的颜色

- (IBAction)doClick:(UIButton *)sender {

    UIView *fatherView = sender.superview; //获取父视图

    

    //设置父视图颜色随机

    float hue =( arc4random() % 255 / 255.0 );//色调 0~254的随机数

    float saturation = arc4random_uniform(255)/255.0;//饱和度

    float brightness = (arc4random() %255/255.0);//亮度

    

    UIColor *randomColor = [UIColor colorWithRed:hue green:saturation blue:brightness alpha:0.7];

    fatherView.backgroundColor = randomColor;

}


- (IBAction)changeColor:(UIButton *)sender {

    //获取随机色

    float colorR = arc4random_uniform(255)/255.0;

    float colorG = arc4random_uniform(255)/255.0;

    float colorB = arc4random_uniform(255)/255.0;

    UIColor *randomCol = [UIColor colorWithRed:colorR green:colorG blue:colorB alpha:1];

    

    //获取0-306的随机数

    int x = arc4random_uniform(306);

    int y = arc4random_uniform(306);

    UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];

    v1.backgroundColor = randomCol;

    [self.fathersView addSubview:v1];

}


- (IBAction)moveColor:(UIButton *)sender {

    //获取随机色

    float colorR = arc4random_uniform(255)/255.0;

    float colorG = arc4random_uniform(255)/255.0;

    float colorB = arc4random_uniform(255)/255.0;

    UIColor *randomCol = [UIColor colorWithRed:colorR green:colorG blue:colorB alpha:1];

    

    //获取0-306的随机数

    int x = arc4random_uniform(306);

    int y = arc4random_uniform(306);

    UIView *v1 = [[UIView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];

    v1.backgroundColor = randomCol;

    [self.fathersView addSubview:v1];

    

    //移动,

    //获取0-306的随机数

    int xx = arc4random_uniform(50);

    int yy = arc4random_uniform(50);

    

    //v1frame值不能直接修改,需重新存于临时变量中

    CGRect v1Frame = v1.frame;

    v1Frame.origin.x += xx;

    v1Frame.origin.y += yy;

    

    //开始动画

    [UIView beginAnimations:nil context:nil];

    

    v1.frame = v1Frame;

    

    //结束动画

    [UIView commitAnimations];

    

    [UIView animateWithDuration:8 animations:^{

        CGRect v2Frame = v1.frame;

        v2Frame.origin.x -= xx;

        v2Frame.origin.y -= yy;

        

        v1.frame = v2Frame;

    }];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值