UI01绘图

来源:互联网 发布:西门子plc编程手册 编辑:程序博客网 时间:2024/06/04 22:24

DrawView.h


#import <UIKit/UIKit.h>


@interface DrawView : UIView


@property (retain,nonatomic)NSMutableArray *lineArray;//线数组。存放触摸点的数组,可以形成线



@end



DrawView.m


#import "DrawView.h"


@implementation DrawView



//初始化方法

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        //初始化属性

        self.lineArray = [[NSMutableArrayalloc]init];

        

        //创建Button

        UIButton *btn = [UIButtonbuttonWithType:1];

        btn.frame = CGRectMake(100, 350, 100, 50);

        [btnsetTitle:@"撤销"forState:UIControlStateNormal];

        [btnaddTarget:selfaction:@selector(chexiao:)forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];

    }

    returnself;

}


//创建撤销方法

- (void)chexiao:(id)sender

{

   //移除线数组中的每一画

    [self.lineArrayremoveLastObject];

    //调用重绘

    [selfsetNeedsDisplay];

}


//——————————————————————提供触摸开始、移动的方法


//触摸开始触发该函数

//集合对象touches代表触摸时候的触点

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //点数组

    NSMutableArray *pointArray = [[NSMutableArrayalloc]init];

   //将点数组放入线数组

    [self.lineArray addObject:pointArray];

   //获取容器中任意一个值(点)

    UITouch *touch = [touchesanyObject];

   //获得触摸对应视图的位置

    CGPoint pt = [touch locationInView:self];

   

    //触点放入数组前,使用 NSValue valueWithCGPoint将结构体的CGPoint对象转化为OC语言的NSValue对象,补充:一个NSValue对象是用来存储一个C或者Objective-C数据的简单容器。它可以保存任意类型的数据,比如int,float,char,当然也可以是指pointers, structures, and object ids。NSValue类的目标就是允许以上数据类型的数据结构能够被添加到集合里,例如那些需要其元素是对象的数据结构,如NSArray或者NSSet的实例。需要注意的是NSValue对象一直是不可枚举的。

    [pointArrayaddObject:[NSValuevalueWithCGPoint:pt]];

   //在控制台打印出点击的坐标

    NSLog(@"%@",[[self.lineArraylastObject]lastObject]);

}

//触摸移动触发该函数

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

    UITouch *touch = [touchesanyObject];

   //获得触摸对应视图的位置

    CGPoint pt = [touch locationInView:self];

    

    //触点放入数组前,使用 NSValue valueWithCGPoint将结构体的CGPoint对象转化为OC语言的NSValue对象

    [[self.lineArraylastObject]addObject:[NSValuevalueWithCGPoint:pt]];

    

    NSLog(@"%@",[[self.lineArraylastObject]lastObject]);

    

   //强制重绘界面,由系统调用 drawRect:绘图方法。只有这样运行后才能有绘图效果

    [selfsetNeedsDisplay];

    

}


//绘图的方法

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    // Drawing code

    

    //CGContextRefC中的函数库默认情况下,该库CoreGraphics.framework已经是导入的

    

   //检索当前的上下文

    CGContextRef context = UIGraphicsGetCurrentContext();

   //设置笔画的颜色

    CGContextSetStrokeColorWithColor(context, [UIColorredColor].CGColor);

   //设置笔画的粗细

    CGContextSetLineWidth(context,2);

   //循环线数组拿到内不的点数组

    for (int i=0; i<self.lineArray.count; i++) {

        //取到线中的点数组

        NSMutableArray *ptArray = [self.lineArrayobjectAtIndex:i];

       //循环遍历数组取出其中的点

        for (int j=0; j<ptArray.count-1; j++) {//为了防止循环的溢出-1

           //获得点数组中的所有点元素

            NSValue *p1 = [ptArrayobjectAtIndex:j];

           //将之前转换为NSValue对象的值,转换回CGPoint类型的值

            CGPoint pt1 = [p1CGPointValue];

            

            NSValue *p2 = [ptArrayobjectAtIndex:j+1];

            CGPoint pt2 = [p2CGPointValue];

            

            //指定绘制的起点

            CGContextMoveToPoint(context, pt1.x, pt1.y);//这个是c中的数据结构,它用来写入参数可以看出。

            //指定绘制的起点

            CGContextAddLineToPoint(context, pt2.x, pt2.y);

            //绘制图片

        }

        CGContextStrokePath(context);

    }

}



ViewController.m

#import "ViewController.h"

#import "DrawView.h"



@interfaceViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

    //实例一个DrawView的对象dv,获得窗口的大小

    DrawView *dv = [[DrawViewalloc]initWithFrame:self.view.frame];

    

    dv.backgroundColor = [UIColorwhiteColor];

    

    [self.viewaddSubview:dv];

    

    

    

}

原创粉丝点击