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


0 0
原创粉丝点击