NO.5 ios--ui code review 实现单击一次view随机改变一个颜色

ViewController.m


//

//  ViewController.m
//  shijian
//
//  Created by admin on 15-2-25.
//  Copyright (c) 2015年 admin. All rights reserved.
//


#import "ViewController.h"
#import "TouchView.h"
@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    TouchView *view = [[TouchView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
    view.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view];
    [view release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


新建TouchView类


//
//  TouchView.m
//  shijian
//
//  Created by admin on 15-2-25.
//  Copyright (c) 2015年 admin. All rights reserved.
//


#import "TouchView.h"


@implementation TouchView


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    int random1 = arc4random() % 256;
    int random2 = arc4random() % 256;
    int random3 = arc4random() % 256;
    
    self.backgroundColor = [UIColor colorWithRed:random1/255.0 green:random2/255.0 blue:random3/255.0 alpha:1.0];
}


@end




你可能感兴趣的:(ios,UI)