iOS编写一个画板,可以变化颜色,字体大小

#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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值