AppDelegate.h
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//先建一个窗口,用来盛放其他视图
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor=[UIColor whiteColor];
[self.window makeKeyAndVisible];
//导航控制器,控制页面的跳转
PaintViewController *VC=[PaintViewController new];
VC.title=@”画图“;
UINavigationController *NC=[[UINavigationController alloc]initWithRootViewController:VC];
self.window.rootViewController=NC;
return YES;
}
PaintViewController.h
– (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
//加载视图
-(void)loadView
{
self.rootView=[[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_rootView.backgroundColor=[UIColor whiteColor];
self.view=self.rootView;
}
– (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
RootView.h
@interface RootView ()
@property(nonatomic)UIButton *button;
@property(nonatomic)UIView *view;
@property(nonatomic)UILabel *point2;
@property(nonatomic)UIColor *color;
@property(nonatomic)NSUInteger flag;
@end
@implementation RootView
//初始化
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
if (self) {
[self addAllViews];
}
return self;
}
//添加所有的视图
-(void)addAllViews
{
self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.frame=CGRectMake(300, 610, 50, 20);
[_button setTitle:@”清空” forState:UIControlStateNormal];
[_button addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_button];
self.view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 500, 600)];
_view.backgroundColor=[UIColor whiteColor];
[self addSubview:_view];
UILabel *random=[[UILabel alloc]initWithFrame:CGRectMake(50, 640, 200, 10)];
random.text=@”注意:天蓝色为随机色哦^_^”;
random.font=[UIFont fontWithName:@”Helvetica” size:15];
[self addSubview:random];
UIButton *cyan=[UIButton buttonWithType:UIButtonTypeRoundedRect];
cyan.frame=CGRectMake(20, 610, 20, 20);
cyan.backgroundColor=[UIColor cyanColor];
[self addSubview:cyan];
[cyan addTarget:self action:@selector(beCyan) forControlEvents:UIControlEventTouchUpInside];
UIButton *black=[UIButton buttonWithType:UIButtonTypeRoundedRect];
black.frame=CGRectMake(50, 610, 20, 20);
black.backgroundColor=[UIColor blackColor];
[self addSubview:black];
[black addTarget:self action:@selector(beBlack) forControlEvents:UIControlEventTouchUpInside];
UIButton *blue=[UIButton buttonWithType:UIButtonTypeRoundedRect];
blue.frame=CGRectMake(80, 610, 20, 20);
blue.backgroundColor=[UIColor blueColor];
[self addSubview:blue];
[blue addTarget:self action:@selector(beBlue) forControlEvents:UIControlEventTouchUpInside];
UIButton *red=[UIButton buttonWithType:UIButtonTypeRoundedRect];
red.frame=CGRectMake(110, 610, 20, 20);
red.backgroundColor=[UIColor redColor];
[self addSubview:red];
[red addTarget:self action:@selector(beRed) forControlEvents:UIControlEventTouchUpInside];
UIButton *green=[UIButton buttonWithType:UIButtonTypeRoundedRect];
green.frame=CGRectMake(140, 610, 20, 20);
green.backgroundColor=[UIColor greenColor];
[self addSubview:green];
[green addTarget:self action:@selector(beGreen) forControlEvents:UIControlEventTouchUpInside];
UIButton *orange=[UIButton buttonWithType:UIButtonTypeRoundedRect];
orange.frame=CGRectMake(170, 610, 20, 20);
orange.backgroundColor=[UIColor orangeColor];
[self addSubview:orange];
[orange addTarget:self action:@selector(beOrange) forControlEvents:UIControlEventTouchUpInside];
UIButton *purple=[UIButton buttonWithType:UIButtonTypeRoundedRect];
purple.frame=CGRectMake(200, 610, 20, 20);
purple.backgroundColor=[UIColor purpleColor];
[self addSubview:purple];
[purple addTarget:self action:@selector(bePurple) forControlEvents:UIControlEventTouchUpInside];
UIButton *gray=[UIButton buttonWithType:UIButtonTypeRoundedRect];
gray.frame=CGRectMake(230, 610, 20, 20);
gray.backgroundColor=[UIColor grayColor];
[self addSubview:gray];
[gray addTarget:self action:@selector(beGray) forControlEvents:UIControlEventTouchUpInside];
UIButton *yellow=[UIButton buttonWithType:UIButtonTypeRoundedRect];
yellow.frame=CGRectMake(260, 610, 20, 20);
yellow.backgroundColor=[UIColor yellowColor];
[self addSubview:yellow];
[yellow addTarget:self action:@selector(beYellow) forControlEvents:UIControlEventTouchUpInside];
}
//选色操作
-(void)beCyan
{
_flag=9;
}
-(void)beBlack
{
_flag=1;
}
-(void)beBlue
{
_flag=2;
}
-(void)beRed
{
_flag=3;
}
-(void)beGreen
{
_flag=4;
}
-(void)beOrange
{
_flag=5;
}
-(void)bePurple
{
_flag=6;
}
-(void)beGray
{
_flag=7;
}
-(void)beYellow
{
_flag=8;
}
//清屏操作
-(void)clear
{
NSArray *array=[_view subviews];
for (id i in array) {
[i removeFromSuperview];
}
// [_view removeFromSuperview];
// RootView *newView=[[RootView alloc]initWithFrame:CGRectMake(0, 0, 500, 610)];
// newView.backgroundColor=[UIColor whiteColor];
// [self addSubview:newView];
}
//通过拖拽实现画线操作
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint currentPoint=[touch locationInView:self];
self.point2=[[UILabel alloc]initWithFrame:CGRectMake(currentPoint.x, currentPoint.y, 4, 4)];
switch (_flag) {
case 1:
{
_point2.backgroundColor=[UIColor blackColor];
break;
}
case 2:
{
_point2.backgroundColor=[UIColor blueColor];
break;
}
case 3:
{
_point2.backgroundColor=[UIColor redColor];
break;
}
case 4:
{
_point2.backgroundColor=[UIColor greenColor];
break;
}
case 5:
{
_point2.backgroundColor=[UIColor orangeColor];
break;
}
case 6:
{
_point2.backgroundColor=[UIColor purpleColor];
break;
}
case 7:
{
_point2.backgroundColor=[UIColor grayColor];
break;
}
case 8:
{
_point2.backgroundColor=[UIColor yellowColor];
break;
}
case 9:
{
CGFloat R=(arc4random()%255+1)/255.0;
CGFloat G=(arc4random()%255+1)/255.0;
CGFloat B=(arc4random()%255+1)/255.0;
UIColor *randomColor=[UIColor colorWithRed:R green:G blue:B alpha:1];
_point2.backgroundColor=randomColor;
break;
}
default:
{
_point2.backgroundColor=[UIColor blackColor];
break;
}
}
if (currentPoint.x
[_view addSubview:_point2];
}
}
@end
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!