签名Demo (IOS)_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 签名Demo (IOS)

签名Demo (IOS)

 2013/8/1 15:09:10  MingFung_Liu  博客园  我要评论(0)
  • 摘要:思路是将每一次按下屏幕的touchmove时的点存到一个数组里,即一个数组相当于一个笔画;再将该代表笔画的数组保存到一个大数组中,每组每次touch的移动都历遍大数组和笔画数组,将点于点之间连接起来。#import<UIKit/UIKit.h>@interfaceBIDDrawView:UIView{NSMutableArray*allPoints;}@end#import"BIDDrawView.h"@implementationBIDDrawView-(id
  • 标签:iOS
 

思路是将每一次按下屏幕的touch move时的点存到一个数组里,即一个数组相当于一个笔画;再将该代表笔画的数组保存到一个大数组中,每组每次touch的移动都历遍大数组和笔画数组,将点于点之间连接起来。

#import <UIKit/UIKit.h>
@interface BIDDrawView : UIView
{
    NSMutableArray *allPoints;
}

@end

 

#import "BIDDrawView.h"

@implementation BIDDrawView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        allPoints=[[NSMutableArray alloc] init];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.frame=CGRectMake(10, 30, 100, 30);
        [btn addTarget:self action:@selector(undo:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    return self;
}

-(void)undo:(UIButton *)sender
{
    if (allPoints.count>0) {
        [allPoints removeLastObject];
        [self setNeedsDisplay];
    }
}

- (void)drawRect:(CGRect)rect
{
    if (allPoints.count == 0) {
        return;
    }
    
    CGContextRef ctx=UIGraphicsGetCurrentContext();  //获取画板,或者说获取画图上下文。
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor); //设置画笔颜色。
    CGContextSetLineWidth(ctx, 1.5); //设置线条宽度。
    
    for (NSMutableArray *points in allPoints) {
        for (int i=0;i<points.count-1;i++) {
            if (points.count==0) {
                break;
            }
            NSValue *sValue = [points objectAtIndex:i];
            CGPoint sPoint=[sValue CGPointValue];
            
            NSValue *eValue = [points 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
{
    NSMutableArray *points = [NSMutableArray array];
    [allPoints addObject:points];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self];
    NSValue *v = [NSValue valueWithCGPoint:p]; //CGPoint 转为 对象
    NSMutableArray *points = [allPoints lastObject];
    [points addObject:v];
    //驱动画笔(drawRect方法)
    [self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}
@end

 

效果:

发表评论
用户名: 匿名