涂鸦 画线

 

视图的拖拽


(.h文件)


//

//  KView.h

//  UI_3

//

//  Created by 0101 on 13-7-29.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface KView :UIView//注意:它是继承子UIView类的

{

    CGPoint startPoint;

}

@end


(.m文件)

//

//  KView.m

//  UI_3

//

//  Created by 0101 on 13-7-29.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import "KView.h"


@implementation KView


-(id)initWithFrame:(CGRect)frame

{

   self =  [superinitWithFrame:frame];

    if (self)

    {

        //用户交互

        self.userInteractionEnabled =YES;

        

        //打开多手指交互,iphone最多支持五个手指,也就是多点触控

        self.multipleTouchEnabled =YES;  

    }

    return self;

}



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

{

    //拖拽功能

    UITouch * t = [touches anyObject];

    startPoint = [tlocationInView:self];//假设当前开始位置为(x0,y0)

    

}


 




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



{


   //当前手指的位置

    UITouch *t = [touches anyObject];


   //移动后停止的位置(x1,y1)

    CGPoint currentPoint = [t locationInView:self];


    //移动的增量

    float diatX = currentPoint.x - startPoint.x;

    float diatY = currentPoint.y - startPoint.y;



    //移动后的中心位置(x2,y2)

    CGPoint p = self.center;

    p.x += diatX;

    p.y += diatY;


    //视图就会移动到(拖拽到)p为中心的位置处

    self.center = p;

}



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

{


}



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

{

     

}

@end


获取两个手指


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

{


    //两个手指的获取,注意self.multipleTouchEnabled =YES;要设置成YES

    NSLog(@"%@",touches);

    UITouch *firstTouch = [[touches allObjects]objectAtIndex:0];

    UITouch *secondTouch = [[touches allObjects]objectAtIndex:1];

}


//输出结果:


2013-07-29 18:08:44.306 UI_3[759:c07] 

{(

    <UITouch: 0x884f9d0> phase: Began tap count: 1 window: 

<UIWindow: 0x8a31d10; frame = (0 0; 320 480); 

layer = <UIWindowLayer: 0x8a31b30>> 

view: <KView: 0x8a32f60; frame = (0 0; 300 400); 

layer = <CALayer: 0x8a33010>> location in window: {143, 303} 

previous location in window: {143, 303} 

location in view: {143, 283} 

previous location in view: {143, 283},

   

 <UITouch: 0x884f390> phase: Began tap count: 1 window: 

<UIWindow: 0x8a31d10; frame = (0 0; 320 480);

 layer = <UIWindowLayer: 0x8a31b30>> 

view: <KView: 0x8a32f60; frame = (0 0; 300 400); 

layer = <CALayer: 0x8a33010>> location in window: {177, 177}

 previous location in window: {177, 177} location in view: {177, 157} 

previous location in view: {177, 157}

)}


画直线



-(void)drawRect:(CGRect)rect

{

    //获取画板

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //设置画笔的颜色

    CGColorRef color = [UIColorredColor].CGColor;

    CGContextSetStrokeColorWithColor(ctx, color);

    

    //设置画笔的粗细

    CGContextSetLineWidth(ctx,2.0);

    

    //设置画图的开始点

    CGContextMoveToPoint(ctx,0, 0);

    

    //在结束点添加一条直线

    CGContextAddLineToPoint(ctx,300,400);

    

    //连接开始点和结束点

    CGContextStrokePath(ctx);

}


打折Label

.h文件


//

//  IndexViewController.m

//  UI_3

//

//  Created by 0101 on 13-7-29.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import "IndexViewController.h"

#import "KView.h"

#import "Klabel.h"

#import "DrawView.h"


@implementation IndexViewController


-(void)loadView

{

    //初始化根视图

    self.view = [[[DrawViewalloc]initWithFrame:CGRectMake(0,0, 320,480)]autorelease];

    self.view.backgroundColor = [UIColororangeColor];

     

    Klabel *kLabel = [[Klabelalloc]initWithFrame:CGRectMake(10,50, 100, 30)];

    kLabel.textAlignment =NSTextAlignmentCenter;

    kLabel.text = @"9999";

    [self.viewaddSubview:kLabel];

    [kLabel release];

}

@end

.m文件


//

//  Klabel.m

//  UI_3

//

//  Created by 0101 on 13-7-29.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import "Klabel.h"


@implementation Klabel


-(id)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self)

    {

        

    }

    return  self;

}


-(void)drawRect:(CGRect)rect

{

    //父类中原来的一些方法我们是可以直接用的,因此直接调用

    [super drawRect:rect];

    

    //一般的如果一个类不是UIView的话,是要继承父类的方法的,也就是要[super XXX];

    

    //画一条直线

    

    //获取画板

    CGContextRef ctx = UIGraphicsGetCurrentContext();

    

    //设置画笔的颜色

    CGColorRef color = [UIColorredColor].CGColor;

    CGContextSetStrokeColorWithColor(ctx, color);

    

    //设置画笔的粗细

    CGContextSetLineWidth(ctx,2.0);

    

    //设置画图的开始点

    CGContextMoveToPoint(ctx,0,self.bounds.size.height / 2);

    

    //在结束点添加一条直线

    CGContextAddLineToPoint(ctx,self.bounds.size.width ,self.bounds.size.height /2);

    

    //连接开始点和结束点

    CGContextStrokePath(ctx);

    

}

@end

运行效果



涂鸦

需求:实现类似QQ的涂鸦效果

思路:将每一个笔画存在在一个小数组中,在把整个涂鸦的全部小数组存储在一个大数组中,撤销时就是将包含在大数组中的小数组一除掉。


注意:


//

//  DrawView.h

//  UI_3

//

//  Created by 0101 on 13-7-29.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface DrawView : UIView

{

    NSMutableArray *allPoints;

}

@end



//

//  DrawView.m

//  UI_3

//

//  Created by 0101 on 13-7-29.

//  Copyright (c) 2013 PH. All rights reserved.

//


#import "DrawView.h"


@implementation DrawView


-(id)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self)

    {

        //初始化一个大数组

        allPoints = [[NSMutableArrayalloc]init];

        

        //创建一个撤销按钮

        UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

        btn.frame = CGRectMake(10, 30, 100, 30);

        [btn setTitle:@"Cancle"forState:0];

        

        //添加一个撤销事件

        [btn addTarget:self

                action:@selector(undo:)

      forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:btn];

        

    }

    return self;

}


-(void)undo:(UIButton *)sender

{

    //若大数组中存在小数组

    if (allPoints.count >0 )

    {

        [allPointsremoveLastObject];//移除数组中的元素,也就是将涂鸦撤销

        [selfsetNeedsDisplay];//UI同时也发生变化,保持ModelView的同步

    }

    

}


-(void)drawRect:(CGRect)rect

{

    //若大数组中不存在小数组,也就是没有任何笔画,则返回

    if (allPoints.count ==0)

    {

        return;

    }

    

    //获取画板

    CGContextRef ctx =UIGraphicsGetCurrentContext();

    //设置画笔颜色

    CGContextSetStrokeColorWithColor(ctx, [UIColorpurpleColor].CGColor);

    //设置线宽

    CGContextSetLineWidth(ctx,2.0);

    

    //循环大数组

    for (NSMutableArray *pointin allPoints)

    {

        //循环小数组中的所有点

        for (int i =0 ;i < point.count -1;i++)

        {

            //如果小数组中没有任何点,则跳出循环

            if (point.count ==0)

            {

                break;

            }

            

            //取出相邻的两点

            NSValue *sValue = [point objectAtIndex: i];//将对象类型转换成基本数据类型

            CGPoint sPoint = [sValue CGPointValue];

            

            NSValue *eValue = [point objectAtIndex:i+1];

            CGPoint ePoint = [eValue CGPointValue];

            

            //将两点连接起来

            CGContextMoveToPoint(ctx, sPoint.x, sPoint.y);

            CGContextAddLineToPoint(ctx, ePoint.x, ePoint.y);

            

            //将所有的点连接起来

            CGContextStrokePath(ctx);

            

        }

    }

}


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

{

   //在每次Touch时就创建了一个空的points

    //为每个笔画创建一个小数组

    NSMutableArray *points = [NSMutableArrayarray];

    //将小数组添加到大数组中

    [allPoints addObject:points];

}


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

{

    UITouch  *touch = [touches anyObject];

    //将当前坐标点赋给CGPoint

    CGPoint p = [touch locationInView:self];

    

    NSValue *v = [NSValuevalueWithCGPoint:p];

    

    //每次Move的时候,都要从大数组中取出空的小数组,然后将这一笔画的所有点添加到这个小数组中

    //获取小数组

    NSMutableArray *points = [allPointslastObject];//将大数组中的最后一个对象(也就是最后一个小数组)添加到小数组中

    [points addObject:v];

    

    //驱动画笔,他能使drawRect方法多次调用  

    [selfsetNeedsDisplay];

    

}


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

{

    

}

@end






















  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
项目:– JavaScript 中的患者数据管理系统 患者数据管理系统是为医院开发的 node JS 项目。通过使用此系统,您可以轻松访问患者数据,它具有成本效益,可改善患者护理和数据安全性。不仅如此,它还减少了错误范围。在运行项目之前,您需要下载 node.js。 这个患者数据管理项目包含 javascript、node.js 和 CSS。我们必须让服务器监听端口 3000,并使用 JSON 在客户端和服务器之间交换数据。这个项目会不断询问您有关插件更新的信息,因此请保持互联网畅通。此系统允许您执行 crud 操作。在这里,您是系统的管理员。您还可以添加所需的员工人数。此外,您还可以更新患者记录。该系统功能齐全且功能齐全。 要运行此项目,您需要在计算机上安装NodeJS并使用现代浏览器,例如 Google Chrome、  Mozilla Firefox。ReactJS项目中的此项目可免费下载源代码。有关项目演示,请查看下面的图像滑块。 对于手动安装 1.将主项目文件夹解压到任意目录 2.从 cmd 设置项目目录的路径 3. 输入命令“npm install” 4.完成后输入命令“npm start” 5.现在,您将获得一个 localhost:portnumber,并转到该 URL 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值