关闭

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

标签: UI界面
129人阅读 评论(0) 收藏 举报
分类:

//计算器

- (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;

    }];

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:67915次
    • 积分:1659
    • 等级:
    • 排名:千里之外
    • 原创:97篇
    • 转载:35篇
    • 译文:0篇
    • 评论:2条
    文章分类