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
- iOS集成可手势签名的视图
- iOS的手势事件和可滚动视图(tableView、collectionView...)的触摸事件冲突的解决方法
- iOS MMDrawerController侧滑菜单与中心视图手势冲突问题的解决方案(续--点击可收起抽屉)
- iOS 手势切换视图
- iOS可重复使用的折叠式表视图
- iOS自定义弹出视图,以及简单的手势讲解
- ios软件开发 获取手势点击视图的tag
- ios 手势点击子视图的时候不响应父视图的点击事件
- iOS开发中让子视图不响应父视图的手势识别器
- Xamarin.iOS滑动视图上图片(视图)的缩放(双击手势实现缩放)
- ios 怎么让子视图不响应父视图的手势识别器呢?
- ios简单手势操作-模态视图
- <iOS>视图添加手势不会retain当前视图
- iOS 点击子视图不让其响应父视图手势
- IOS下集成支付宝RSA签名
- Java【Java开发集成 Swagger UI生成可视图的API文档(详细图解)】
- ios手势的综合
- iOS 手势的用法
- 每天一个linux命令(39):lsof命令
- Template模式详解--设计模式(13)
- const 详解(修饰变量、输入参数、返回值、成员函数)
- Fragment FragmentOne{2627db19} not attached to Activity
- lapply &sapply note
- iOS集成可手势签名的视图
- VB6.0基础 用VB获取当前的年月日
- Android插件化原理解析——Hook机制之Binder Hook
- Dungeon Game
- Qt之操作Excel
- RecyclerView的快速滑动,导致程序崩溃
- Android开发环境搭建
- Android面试(一)?(内存泄露,Activity生命周期,横竖屏切换,Activity设为窗口,安全退出Activity,)
- 外包测试为何越来越受移动APP开发企业的重视?