UITouch、CGContextRef简单综合应用

来源:互联网 发布:车载电子狗软件哪个好 编辑:程序博客网 时间:2024/05/16 00:29
#import <UIKit/UIKit.h>

@interface KView:UIView {

//放划线数组

    NSMutableArray *array;

//放删除时的线

    NSMutableArray *array1;
    CGContextRef context;
}

@end


#import "KView.h"

@implementation KView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        //支持多触点
        //self.multipleTouchEnabled=YES;
        ;
        self.backgroundColor=[UIColor whiteColor];
        array1=[[NSMutableArray alloc]init];
        array=[[NSMutableArray alloc]init];
        UIButton* button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame=CGRectMake(0, 0, 50, 30);
        [button setTitle:@"后退" forState:0];
        [button addTarget:self
                   action:@selector(cler)
         forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];

        button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame=CGRectMake(60, 0, 50, 30);
        [button setTitle:@"前进" forState:0];
        [button addTarget:self
                   action:@selector(add)
         forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame=CGRectMake(120, 0, 50, 30);
        [button setTitle:@"清除" forState:0];
        [button addTarget:self
                   action:@selector(allclear)
         forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];

                

//        UITouch *t=[touches anyObject];
//        CGPoint p= [t locationInView:self];
//        NSLog(@"%@",NSStringFromCGPoint(p));
    }
    return self;
}

//清除所有线
-(void)allclear{
    [array removeAllObjects];
    [array1 removeAllObjects];

//重新加载此页面
    [self setNeedsDisplay];
}

//清除最后一条划线
-(void)cler{
    if (array.count==0) {
        [array1 removeAllObjects];
    }else{
        [array1 addObject:[array lastObject]];
        [array removeLastObject];
        [self setNeedsDisplay];
    }
    
}
-(void)add{
    if (array1.count==0) {
        
    }else{
        [array addObject:[array1 lastObject]];
        [array1 removeLastObject];
        [self setNeedsDisplay];
    }
    
}
-(void)drawRect:(CGRect)rect{

//    CGContextRef context = UIGraphicsGetCurrentContext();
//    
//    
//    
//    画一个正方形图形 没有边框
//    
//    CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);
//    CGContextFillRect(context, CGRectMake(2, 2, 270, 270));
//    CGContextStrokePath(context);
//    
//    
//    
//    写文字
//    
//    CGContextSetLineWidth(context, 1.0);
//    CGContextSetRGBFillColor (context,  1, 1, 1, 1.0);
//    
//    UIFont  *font = [UIFont boldSystemFontOfSize:11.0];
//    
//    [@"fangyp" drawInRect:CGRectMake(40, 40, 80, 20) withFont:font];
//    
//    
//    
//    画一条线
//    
//    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
//    CGContextMoveToPoint(context, 20, 20);
//    CGContextAddLineToPoint(context, 200,20);
//    CGContextStrokePath(context);
//    
//    
//    
//    画正方形边框
//    
//    CGContextSetRGBStrokeColor(context, 1, 1.0, 1.0, 1.0);
//    CGContextSetLineWidth(context, 2.0);
//    CGContextAddRect(context, CGRectMake(2, 2, 270, 270));
//    CGContextStrokePath(context);
//    
//    
//    
//    画方形背景颜色
//    CGContextTranslateCTM(ctx, 0.0f, self.view.bounds.size.height);
//    CGContextScaleCTM(ctx, 1.0f, -1.0f);
//    UIGraphicsPushContext(ctx);
//    CGContextSetLineWidth(ctx,320);
//    CGContextSetRGBStrokeColor(ctx, 250.0/255, 250.0/255, 210.0/255, 1.0); 
//    CGContextStrokeRect(ctx, CGRectMake(0, 0, 320, 460));
    //获取当前的画布
    CGContextRef con=UIGraphicsGetCurrentContext();
    //设置画笔颜色
    CGContextSetStrokeColorWithColor(con, [UIColor redColor].CGColor);
    //设置画笔宽度
    CGContextSetLineWidth(con, 3.0f);
    for (int i=0; i<array.count; i++) {
        NSMutableArray *ar=[array objectAtIndex:i];
        for (int j=0; j<ar.count-1; j++) {
            CGPoint point=CGPointFromString([ar objectAtIndex:j]);
            //NSLog(@"point.x=%f   point.xy=%f",point.x,point.y);
            CGPoint point1=CGPointFromString([ar objectAtIndex:j+1]);
            //NSLog(@"point1.x=%f   point1.y=%f",point1.x,point1.y);

           //初识划线起点
            CGContextMoveToPoint(con, point.x, point.y);

            //划线的终点
            CGContextAddLineToPoint(con, point1.x, point1.y);

            //将两点链接
            CGContextStrokePath(con);
        }
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //NSLog(@"%s",__func__);
    //NSLog(@"%@",touches);
    UITouch *t=[touches anyObject];
    CGPoint p= [t locationInView:self];
    NSMutableArray *aNewLine=[[NSMutableArray alloc]init];
    [aNewLine addObject:NSStringFromCGPoint(p)];
    [array addObject:aNewLine];
    [aNewLine release];
//    NSLog(@"%@",NSStringFromCGPoint(p));
    [self setNeedsDisplay];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    //NSLog(@"%s",__func__);
    UITouch *t=[touches anyObject];
    CGPoint p= [t locationInView:self];
    [[array lastObject]addObject:NSStringFromCGPoint(p)];
    //NSLog(@"%@",NSStringFromCGPoint(p));
    [self setNeedsDisplay];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
//    NSLog(@"%s",__func__);
//    for (int i=0; i<array.count; i++) {
//        NSLog(@"%d",array.count);
//        NSLog(@"%@",[array objectAtIndex:i]);
//        }
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s",__func__);
}

  *************闲杂东西**************

画一个正方形图形 没有边框

CGContextSetRGBFillColor(context, 0, 0.25, 0, 0.5);
CGContextFillRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);

 

写文字

CGContextSetLineWidth(context, 1.0);
CGContextSetRGBFillColor (context,  1, 1, 1, 1.0);

UIFont  *font = [UIFont boldSystemFontOfSize:11.0];

[@"fangyp" drawInRect:CGRectMake(40, 40, 80, 20) withFont:font];

 

画一条线

CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.5);//线条颜色
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 200,20);
CGContextStrokePath(context);

 

画正方形边框

CGContextSetRGBStrokeColor(context, 1, 1.0, 1.0, 1.0); 
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(2, 2, 270, 270));
CGContextStrokePath(context);

 

画方形背景颜色

  CGContextTranslateCTM(ctx, 0.0f, self.view.bounds.size.height);
   CGContextScaleCTM(ctx, 1.0f, -1.0f);
   UIGraphicsPushContext(ctx);
  CGContextSetLineWidth(ctx,320);
  CGContextSetRGBStrokeColor(ctx, 250.0/255, 250.0/255, 210.0/255, 1.0); 
  CGContextStrokeRect(ctx, CGRectMake(0, 0, 320, 460));
  UIGraphicsPopContext();
原创粉丝点击