zoukankan      html  css  js  c++  java
  • (IOS)签名Demo

    思路是将每一次按下屏幕的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

    效果:

  • 相关阅读:
    python定时任务:apscheduler的使用(还有一个celery~)
    Python定时任务-schedule vs. Celery vs. APScheduler
    结合Django+celery二次开发定时周期任务
    The Django Book 2.0--中文版
    第十二章: 部署Django
    Django扩展自定义manage命令
    使用django-extension扩展django的manage――runscript命令
    Django | 执行项目下指定的脚本
    C语言宏定义技巧——多次包括头文件内容不同
    《Java并发编程实战》第十章 避免活跃性危急 读书笔记
  • 原文地址:https://www.cnblogs.com/mingfung-liu/p/3229738.html
Copyright © 2011-2022 走看看