您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐