iOS编写一个画板,可以变化颜色,字体大小
2013-12-18 22:17
477 查看
#import "DrawView.h"
@implementation DrawView
- (id)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
// Initialization code
self.lineArray = [NSMutableArray
arrayWithCapacity:1];
self.array = [NSMutableArray
arrayWithCapacity:1];
self.arraySize = [NSMutableArray
arrayWithCapacity:1];
UIButton *button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.frame =
CGRectMake(20,
400, 60,
40);
button.backgroundColor = [UIColor
greenColor];
[self
addSubview:button];
[button setTitle:@"撤消"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonPress:)
forControlEvents:UIControlEventTouchUpInside];
self.slider = [[UISlider
alloc]initWithFrame:CGRectMake(100,
400,
200, 40)];
[self
addSubview:_slider];
_slider.minimumValue =
1.0;
_slider.maximumValue =
9.0;
[_slider
release];
NSMutableArray *array = [NSMutableArray
arrayWithObjects:@"红",
@"橙",
@"黄",
@"绿",
@"青",
@"蓝",
@"紫",
nil];
self.segControl = [[UISegmentedControl
alloc]initWithItems:array];
_segControl.frame =
CGRectMake(10,
460, 300,
20);
_segControl.selectedSegmentIndex =
0;
[self
addSubview:_segControl];
[_segControl
release];
}
return
self;
}
- (void)dealloc
{
self.lineArray =
nil;
[super
dealloc];
}
- (void)buttonPress:(UIButton *)button
{
[_lineArray
removeLastObject];
[_array
removeLastObject];
[_arraySize
removeLastObject];
[self
setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *pointarray = [NSMutableArray
arrayWithCapacity:1];
[_lineArray
addObject:pointarray];
NSInteger temp =
_segControl.selectedSegmentIndex;
[self.array
addObject:[NSNumber
numberWithInt:temp]];
[self.arraySize
addObject:[NSNumber
numberWithFloat:_slider.value]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches
anyObject];
CGPoint point = [touch
locationInView:self];
NSValue *pointvalue = [NSValue
valueWithCGPoint:point];
[[_lineArray
lastObject] addObject:pointvalue];
[self
setNeedsDisplay];
}
- (CGColorRef)choice:(NSInteger)temp
{
CGColorRef color = [UIColor
whiteColor].CGColor;
switch (temp) {
case 0:
color = [UIColor
redColor].CGColor;
break;
case 1:
color = [UIColor
orangeColor].CGColor;
break;
case 2:
color = [UIColor
yellowColor].CGColor;
break;
case 3:
color = [UIColor
greenColor].CGColor;
break;
case 4:
color = [UIColor
cyanColor].CGColor;
break;
case 5:
color = [UIColor
blueColor].CGColor;
break;
case 6:
color = [UIColor
purpleColor].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 = [_lineArray
objectAtIndex:i];
NSInteger temp = [[self.array
objectAtIndex:i] intValue];
CGContextSetStrokeColorWithColor(context, [self
choice:temp]);
CGContextSetLineWidth(context, [[_arraySize
objectAtIndex: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
@implementation DrawView
- (id)initWithFrame:(CGRect)frame
{
self = [super
initWithFrame:frame];
if (self) {
// Initialization code
self.lineArray = [NSMutableArray
arrayWithCapacity:1];
self.array = [NSMutableArray
arrayWithCapacity:1];
self.arraySize = [NSMutableArray
arrayWithCapacity:1];
UIButton *button = [UIButton
buttonWithType:UIButtonTypeSystem];
button.frame =
CGRectMake(20,
400, 60,
40);
button.backgroundColor = [UIColor
greenColor];
[self
addSubview:button];
[button setTitle:@"撤消"
forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonPress:)
forControlEvents:UIControlEventTouchUpInside];
self.slider = [[UISlider
alloc]initWithFrame:CGRectMake(100,
400,
200, 40)];
[self
addSubview:_slider];
_slider.minimumValue =
1.0;
_slider.maximumValue =
9.0;
[_slider
release];
NSMutableArray *array = [NSMutableArray
arrayWithObjects:@"红",
@"橙",
@"黄",
@"绿",
@"青",
@"蓝",
@"紫",
nil];
self.segControl = [[UISegmentedControl
alloc]initWithItems:array];
_segControl.frame =
CGRectMake(10,
460, 300,
20);
_segControl.selectedSegmentIndex =
0;
[self
addSubview:_segControl];
[_segControl
release];
}
return
self;
}
- (void)dealloc
{
self.lineArray =
nil;
[super
dealloc];
}
- (void)buttonPress:(UIButton *)button
{
[_lineArray
removeLastObject];
[_array
removeLastObject];
[_arraySize
removeLastObject];
[self
setNeedsDisplay];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSMutableArray *pointarray = [NSMutableArray
arrayWithCapacity:1];
[_lineArray
addObject:pointarray];
NSInteger temp =
_segControl.selectedSegmentIndex;
[self.array
addObject:[NSNumber
numberWithInt:temp]];
[self.arraySize
addObject:[NSNumber
numberWithFloat:_slider.value]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches
anyObject];
CGPoint point = [touch
locationInView:self];
NSValue *pointvalue = [NSValue
valueWithCGPoint:point];
[[_lineArray
lastObject] addObject:pointvalue];
[self
setNeedsDisplay];
}
- (CGColorRef)choice:(NSInteger)temp
{
CGColorRef color = [UIColor
whiteColor].CGColor;
switch (temp) {
case 0:
color = [UIColor
redColor].CGColor;
break;
case 1:
color = [UIColor
orangeColor].CGColor;
break;
case 2:
color = [UIColor
yellowColor].CGColor;
break;
case 3:
color = [UIColor
greenColor].CGColor;
break;
case 4:
color = [UIColor
cyanColor].CGColor;
break;
case 5:
color = [UIColor
blueColor].CGColor;
break;
case 6:
color = [UIColor
purpleColor].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 = [_lineArray
objectAtIndex:i];
NSInteger temp = [[self.array
objectAtIndex:i] intValue];
CGContextSetStrokeColorWithColor(context, [self
choice:temp]);
CGContextSetLineWidth(context, [[_arraySize
objectAtIndex: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一种简单的改变一个label上范围内字体颜色和大小的方法
- iOS 一个控件内字符显示不同的字体,大小和颜色
- 4.一个字体的随机颜色及有规律的大小变化
- ios - 可以改变系统弹框字体大小以及颜色的方法
- ios修改textField的placeholder的字体颜色大小
- 一个字体,大小,颜色可定义的自绘静态框控件-XColorStatic 类(比较好看,一共19篇自绘文章)
- 修改iOS导航栏标题的字体颜色和大小以及导航栏的背景图片
- android 利用一个TextView实现两行并且每行的字体大小和颜色各异
- iOS开发~修改UITextField的placeholder的字体大小、颜色
- iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色
- ios修改textField的placeholder的字体颜色、大小
- iOS在一个label中显示不同颜色的字体
- iOS中设置导航栏标题的字体颜色和大小
- iOS使用NSMutableAttributedString改变字符串中部分文字的字体颜色或大小
- iOS小问题---设置TabBarItem的字体大小、颜色,各种状态下的图片
- IOS设置导航栏字体大小及颜色
- UISegmentedControl字体大小,颜色,选中颜色,左边椭圆,右边直线的Button 解决之iOS开发之分段控制器UISegmentedControl
- Java实训——编写一个窗体程序,能够对文本区中的文字设置字体和大小。
- iOS开发 -- 更改placeholder的字体颜色、大小