iOS编写一个画板,可以变化颜色,字体大小
来源:互联网 发布:中等收入陷阱国家 知乎 编辑:程序博客网 时间:2024/06/05 10:16
#import "DrawView.h"
@implementation DrawView
- (id)initWithFrame:(CGRect)frame
{
self = [superinitWithFrame:frame];
if (self) {
// Initialization code
self.lineArray = [NSMutableArrayarrayWithCapacity:1];
self.array = [NSMutableArrayarrayWithCapacity:1];
self.arraySize = [NSMutableArrayarrayWithCapacity:1];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame =CGRectMake(20,400, 60, 40);
button.backgroundColor = [UIColorgreenColor];
[selfaddSubview:button];
[button setTitle:@"撤消"forState:UIControlStateNormal];
[button addTarget:selfaction:@selector(buttonPress:)forControlEvents:UIControlEventTouchUpInside];
self.slider = [[UISlideralloc]initWithFrame:CGRectMake(100,400, 200, 40)];
[selfaddSubview:_slider];
_slider.minimumValue =1.0;
_slider.maximumValue =9.0;
[_sliderrelease];
NSMutableArray *array = [NSMutableArrayarrayWithObjects:@"红",
@"橙",
@"黄",
@"绿",
@"青",
@"蓝",
@"紫",nil];
self.segControl = [[UISegmentedControlalloc]initWithItems:array];
_segControl.frame =CGRectMake(10,460, 300, 20);
_segControl.selectedSegmentIndex =0;
[selfaddSubview:_segControl];
[_segControlrelease];
}
return self;
}
- (void)dealloc
{
self.lineArray =nil;
[superdealloc];
}
- (void)buttonPress:(UIButton *)button
{
[_lineArrayremoveLastObject];
[_arrayremoveLastObject];
[_arraySizeremoveLastObject];
[selfsetNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *pointarray = [NSMutableArrayarrayWithCapacity:1];
[_lineArrayaddObject:pointarray];
NSInteger temp =_segControl.selectedSegmentIndex;
[self.arrayaddObject:[NSNumbernumberWithInt:temp]];
[self.arraySizeaddObject:[NSNumbernumberWithFloat:_slider.value]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
NSValue *pointvalue = [NSValuevalueWithCGPoint:point];
[[_lineArraylastObject] addObject:pointvalue];
[selfsetNeedsDisplay];
}
- (CGColorRef)choice:(NSInteger)temp
{
CGColorRef color = [UIColorwhiteColor].CGColor;
switch (temp) {
case 0:
color = [UIColorredColor].CGColor;
break;
case 1:
color = [UIColororangeColor].CGColor;
break;
case 2:
color = [UIColoryellowColor].CGColor;
break;
case 3:
color = [UIColorgreenColor].CGColor;
break;
case 4:
color = [UIColorcyanColor].CGColor;
break;
case 5:
color = [UIColorblueColor].CGColor;
break;
case 6:
color = [UIColorpurpleColor].CGColor;
break;
default:
break;
}
return color;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context =UIGraphicsGetCurrentContext();
//设置画笔颜色
for (int i =0 ; i < _lineArray.count; i++) {
NSMutableArray *point = [_lineArrayobjectAtIndex:i];
NSInteger temp = [[self.arrayobjectAtIndex:i] intValue];
CGContextSetStrokeColorWithColor(context, [selfchoice:temp]);
CGContextSetLineWidth(context, [[_arraySizeobjectAtIndex:i] floatValue]);
for (int j =0; j < (int)point.count -1; j++) {
NSValue *firstValue = [point objectAtIndex:j];
NSValue *secondValue = [point objectAtIndex:j + 1];
CGPoint firstPoint = [firstValue CGPointValue];
CGPoint secondPoint = [secondValue CGPointValue];
CGContextMoveToPoint(context, firstPoint.x, firstPoint.y);
CGContextAddLineToPoint(context, secondPoint.x, secondPoint.y);
}
CGContextStrokePath(context);
}
}
@end
- iOS编写一个画板,可以变化颜色,字体大小
- IOS修改TABBAR字体大小和选中颜色
- iOS UILabel改变指定字体大小和颜色
- iOS UILabel换行同时修改字体大小颜色
- iOS 富文本~设置字体大小和颜色
- 一个文本框2种字体大小、颜色
- 【IOS】仿 AppleTree 画板中的颜色选择器 (半成品)
- 安卓中利用html实现字体颜色的变化、字体大小的变化和换行
- iOS中webView的字体大小/颜色 以及背景颜色修改
- UITableViewCell删除按钮的背景颜色,字体大小可以改变吗?
- ios 画板
- 画板 ios
- Android:一个可以随意涂鸦的画板应用
- Swing写一个滚动变化颜色
- 三颜色画板
- iOS 在一个UILabel里面字体大小不一样
- iOS Label大小不变内容可适应变化(字体大小变化)
- ios改变导航条title的字体大小和颜色
- Android onKeyDown()监听返回键无效
- JSP内置对象----response
- 学习笔记之快速排序法 --java
- 使用 Eclipse 远程调试 Java 应用程序
- UVA - 10825 Anagram and Multiplication
- iOS编写一个画板,可以变化颜色,字体大小
- 自己的Vim配置
- JSP/Servlet基础——登录成功后返回之前的页面
- 安装DirectX SDK后,编译VTK出错"__RPC__out_xcount_part"
- Android之基于XMPP协议即时通讯软件(二)
- mysql: load data infile
- 因为你是大学生,所以你什么也不是!——俞敏洪
- 什么是P问题、NP问题和NPC问题
- 回声-输入输出练习(字符串)