iOS集成可手势签名的视图

来源:互联网 发布:php 遍历json数组 编辑:程序博客网 时间:2024/05/02 09:28


1.h文件设置可供外界调用的方法

#import <UIKit/UIKit.h>

@interface MyView : UIView

@property(copy,nonatomic)NSString *tagStr;

// get point  in view

-(void)addPA:(CGPoint)nPoint;

-(void)addLA;

-(void)revocation;

-(void)refrom;

-(void)clear;

-(void)setLineColor:(NSInteger)color;

-(void)setlineWidth:(NSInteger)width;

@end



2.m文件内部实现

#import "MyView.h"

@implementation MyView

//保存线条颜色

static NSMutableArray *colorArray;

//保存被移除的线条颜色

static NSMutableArray *deleColorArray;

//每次触摸结束前经过的点,形成线的点数组

static NSMutableArray *pointArray;

//每次触摸结束后的线数组

static NSMutableArray *lineArray;

//删除的线的数组,方便重做时取出来

static NSMutableArray *deleArray;

//线条宽度的数组

static float lineWidthArray[10]={5.0,10.0,12.0,14.0,16.0,20.0,22.0,24.0,26.0,28.0};

//删除线条时删除的线条宽度储存的数组

static NSMutableArray *deleWidthArray;

//正常存储的线条宽度的数组

static NSMutableArray *WidthArray;

//确定颜色的值,将颜色计数的值存到数组里默认为0,即为绿色

static int colorCount;

//确定宽度的值,将宽度计数的值存到数组里默认为0,即为10

static int widthCount;

//保存颜色的数组

static NSMutableArray *colors;

- (id)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        //初始化颜色数组,将用到的颜色存储到数组里

        colors=[NSMutableArrayarrayWithObjects:[UIColorblackColor],[UIColorgreenColor],[UIColorblueColor],[UIColorredColor],[UIColorblackColor],[UIColorwhiteColor], nil];

        WidthArray=[NSMutableArrayarray];

        deleWidthArray=[NSMutableArrayarray];

        pointArray=[NSMutableArrayarray];

        lineArray=[NSMutableArrayarray];

        deleArray=[NSMutableArrayarray];

        colorArray=[NSMutableArrayarray];

        deleColorArray=[NSMutableArrayarray];

        //颜色和宽度默认都取当前数组第0位为默认值

        colorCount=0;

        widthCount=0;

        // Initialization code

    }

    returnself;

}


//给界面按钮操作时获取tag值作为width的计数。来确定宽度,颜色同理

-(void)setlineWidth:(NSInteger)width{

    widthCount=width;

}

-(void)setLineColor:(NSInteger)color{

    colorCount=color;

}

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

// An empty implementation adversely affects performance during animation.

//uiview默认的drawRect方法,覆盖重写,可在界面上重绘,并将AViewController.xib的文件设置为自定义的MyView

- (void)drawRect:(CGRect)rect

{

    //获取当前上下文,

    CGContextRef context=UIGraphicsGetCurrentContext();

    CGContextBeginPath(context);

    CGContextSetLineWidth(context,10.0f);

    //线条拐角样式,设置为平滑

    CGContextSetLineJoin(context,kCGLineJoinRound);

    //线条开始样式,设置为平滑

    CGContextSetLineCap(context,kCGLineJoinRound);

    //查看lineArray数组里是否有线条,有就将之前画的重绘,没有只画当前线条

    if ([lineArraycount]>0) {

        for (int i=0; i<[lineArraycount]; i++) {

            NSArray * array=[NSArray

                             arrayWithArray:[lineArrayobjectAtIndex:i]];

           

        if ([arraycount]>0)

        {

            CGContextBeginPath(context);

            CGPoint myStartPoint=CGPointFromString([arrayobjectAtIndex:0]);

            CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);

            

            for (int j=0; j<[arraycount]-1; j++)

            {

                CGPoint myEndPoint=CGPointFromString([arrayobjectAtIndex:j+1]);

                //--------------------------------------------------------

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

            }

            //获取colorArray数组里的要绘制线条的颜色

            NSNumber *num=[colorArrayobjectAtIndex:i];

            int count=[numintValue];

            UIColor *lineColor=[colorsobjectAtIndex:count];

            //获取WidthArray数组里的要绘制线条的宽度

            NSNumber *wid=[WidthArrayobjectAtIndex:i];

            int widthc=[widintValue];

            float width=lineWidthArray[widthc];

            //设置线条的颜色,要取uicolor的CGColor

            CGContextSetStrokeColorWithColor(context,[lineColorCGColor]);

            //-------------------------------------------------------

            //设置线条宽度

            CGContextSetLineWidth(context, width);

            //保存自己画的

            CGContextStrokePath(context);

        }

     }

    }

    //画当前的线

    if ([pointArraycount]>0)

    {

        CGContextBeginPath(context);

        CGPoint myStartPoint=CGPointFromString([pointArrayobjectAtIndex:0]);

        CGContextMoveToPoint(context, myStartPoint.x, myStartPoint.y);

        

        for (int j=0; j<[pointArraycount]-1; j++)

        {

            CGPoint myEndPoint=CGPointFromString([pointArrayobjectAtIndex:j+1]);

            //--------------------------------------------------------

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

        }

        UIColor *lineColor=[colorsobjectAtIndex:colorCount];

        float width=lineWidthArray[widthCount];

        CGContextSetStrokeColorWithColor(context,[lineColorCGColor]);

        //-------------------------------------------------------

        CGContextSetLineWidth(context, width);

        CGContextStrokePath(context);

    }


     

}

//在touch结束前将获取到的点,放到pointArray里

-(void)addPA:(CGPoint)nPoint{

    NSString *sPoint=NSStringFromCGPoint(nPoint);

    [pointArrayaddObject:sPoint];

}

//在touchend时,将已经绘制的线条的颜色,宽度,线条线路保存到数组里

-(void)addLA{

    NSNumber *wid=[[NSNumberalloc]initWithInt:widthCount];

    NSNumber *num=[[NSNumberalloc]initWithInt:colorCount];

    [colorArrayaddObject:num];

    [WidthArrayaddObject:wid];

    NSArray *array=[NSArrayarrayWithArray:pointArray];

    [lineArrayaddObject:array];

    pointArray=[[NSMutableArrayalloc]init];

}


#pragma mark -

//手指开始触屏开始

static CGPoint MyBeganpoint;

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

{

    

    UILabel * label = (UILabel *)self.superview;

    label.text =@"";

    

}

//手指移动时候发出

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

{

    self.tagStr =@"kk";

    UITouch* touch=[touchesanyObject];

MyBeganpoint=[touchlocationInView:self];

    NSString *sPoint=NSStringFromCGPoint(MyBeganpoint);

    [pointArrayaddObject:sPoint];

    [selfsetNeedsDisplay];

}

//当手指离开屏幕时候

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

{

     [selfaddLA];

    NSLog(@"touches end");

}

//电话呼入等事件取消时候发出

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

{

NSLog(@"touches Canelled");

}

//撤销,将当前最后一条信息移动到删除数组里,方便恢复时调用

-(void)revocation{

    if ([lineArraycount]) {

        [deleArrayaddObject:[lineArraylastObject]];

        [lineArrayremoveLastObject];

    }

    if ([colorArraycount]) {

        [deleColorArrayaddObject:[colorArraylastObject]];

        [colorArrayremoveLastObject];

    }

    if ([WidthArraycount]) {

        [deleWidthArrayaddObject:[WidthArraylastObject]];

        [WidthArrayremoveLastObject];

    }

    //界面重绘方法

    [selfsetNeedsDisplay];

}

//将删除线条数组里的信息,移动到当前数组,在主界面重绘

-(void)refrom{

    if ([deleArraycount]) {

        [lineArrayaddObject:[deleArraylastObject]];

        [deleArrayremoveLastObject];

    }

    if ([deleColorArraycount]) {

        [colorArrayaddObject:[deleColorArraylastObject]];

        [deleColorArrayremoveLastObject];

    }

    if ([deleWidthArraycount]) {

        [WidthArrayaddObject:[deleWidthArraylastObject]];

        [deleWidthArrayremoveLastObject];

    }

    [selfsetNeedsDisplay];

     

}

-(void)clear{

    UILabel * label = (UILabel *)self.superview;

   label.text =@"请使用正楷签名";  

    self.tagStr =nil;

    //移除所有信息并重绘

   [deleArrayremoveAllObjects];

    [deleColorArrayremoveAllObjects];

    colorCount=0;

    [colorArrayremoveAllObjects];

    [lineArrayremoveAllObjects];

    [pointArrayremoveAllObjects];

    [deleWidthArrayremoveAllObjects];

    widthCount=0;

    [WidthArrayremoveAllObjects];

    [selfsetNeedsDisplay];

  

}

@end






0 0
原创粉丝点击