视图的拖拽
 
文件)
 
 //
 //  KView.h
 //  UI_3
 //
 //  Created by 0101 on 13-7-29.
年
 //
 
#import <UIKit/UIKit.h>
 
@interface KView :UIView//注意:它是继承子UIView类的
 {
CGPoint
 }
 @end
 
文件)
 //
 //  KView.m
 //  UI_3
 //
 //  Created by 0101 on 13-7-29.
年
 //
 
#import "KView.h"
 
 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
 {
    //拖拽功能
UITouch * t = [touches  anyObject];
    startPoint = [tlocationInView:self];//假设当前开始位置为(x0,y0)
     
 }
 
  
 
 
 
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
 
 
 {

 
   //当前手指的位置
UITouch *t = [touches  anyObject];
 
移动后停止的位置(x1,y1)
CGPoint currentPoint = [t  locationInView:self];
 
    //移动的增量
float
float
 
 
移动后的中心位置(x2,y2)
    CGPoint p =  self.center;
x
y
 
    //视图就会移动到(拖拽到)以p为中心的位置处
self.center = p;
 }
 
 
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent
 {
 
 }
 
 
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent
 {
      
 }
 @end
 
 获取两个手指
 
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
 {
 
    //两个手指的获取,注意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
文件
 
 //
 //  IndexViewController.m
 //  UI_3
 //
 //  Created by 0101 on 13-7-29.
年
 //
 
#import "IndexViewController.h"
#import "KView.h"
#import "Klabel.h"
#import "DrawView.h"
 
@implementation
 
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;
text =  @"¥9999";
self.viewaddSubview:kLabel];
release];
 }
 @end
文件
 
 //
 //  Klabel.m
 //  UI_3
 //
 //  Created by 0101 on 13-7-29.
年
 //
 
#import "Klabel.h"
 
 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

运行效果


涂鸦 android demo 涂鸦画_数组

涂鸦

QQ的涂鸦效果

思路:将每一个笔画存在在一个小数组中,在把整个涂鸦的全部小数组存储在一个大数组中,撤销时就是将包含在大数组中的小数组一除掉。
 
 注意:
 
 //
 //  DrawView.h
 //  UI_3
 //
 //  Created by 0101 on 13-7-29.
年
 //
 
#import <UIKit/UIKit.h>
 
@interface DrawView :  UIView
 {
NSMutableArray
 }
 @end
 
 
 //
 //  DrawView.m
 //  UI_3
 //
 //  Created by 0101 on 13-7-29.
年
 //
 
#import "DrawView.h"
 
 DrawView
 
id)initWithFrame:(CGRect)frame
 {
self = [superinitWithFrame:frame];
if (self)
     {
        //初始化一个大数组
allPoints = [[NSMutableArrayalloc]init];
         
        //创建一个撤销按钮
        UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
frame =  CGRectMake(10,  30, 100, 30);
setTitle:@"Cancle"forState:0];
         
        //添加一个撤销事件
addTarget:self
action:@selector(undo:)
      forControlEvents:UIControlEventTouchUpInside];
self addSubview:btn];
         
     }
    return  self;
 }
 
void)undo:(UIButton
 {
    //若大数组中存在小数组
if (allPoints.count >0
     {
        [allPointsremoveLastObject];//移除数组中的元素,也就是将涂鸦撤销
        [selfsetNeedsDisplay];//UI同时也发生变化,保持Model和View的同步
     }
     
 }
 
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
 {
在每次Touch时就创建了一个空的points,
    //为每个笔画创建一个小数组
    NSMutableArray *points = [NSMutableArrayarray];
    //将小数组添加到大数组中
allPoints addObject:points];
 }
 
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent
 {
UITouch  *touch = [touches  anyObject];
    //将当前坐标点赋给CGPoint
CGPoint p = [touch  locationInView:self];
     
NSValue *v = [NSValuevalueWithCGPoint:p];
     
    //每次Move的时候,都要从大数组中取出空的小数组,然后将这一笔画的所有点添加到这个小数组中
    //获取小数组
    NSMutableArray *points = [allPointslastObject];//将大数组中的最后一个对象(也就是最后一个小数组)添加到小数组中
addObject:v];
     
    //驱动画笔,他能使drawRect方法多次调用
    [selfsetNeedsDisplay];
     
 }
 
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent
 {
     
 }
 @end


涂鸦 android demo 涂鸦画_#import_02


涂鸦 android demo 涂鸦画_UI_03