一,程序布局整理

前言://1,程序启动//2,程序流程框架//3,程序界面一致//4,程序界面功能,

//这里只做页面的固定功能, //在首次创建界面时,我们会指定好固定事件触发前的固定方法 //至于另外程序启动后,运行过程中会添加和减少的事件触发的方法,我们会在另外一个"刷新"方法内实现

在上面7大视图中添加按钮事件,这些事件写在所有外观布局代码的最下边,例如在editScene视图中,我们把所有的事件都写在如下内容处,其余视图中的事件按此方式来写

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

//返回按钮功能

[leftButton addTarget:self action:@selector(editSceneBackMainScene:) forControlEvents:UIControlEventTouchUpInside];

//保存按钮功能

[rightButton addTarget:self action:@selector(editSceneBackMainScene:) forControlEvents:UIControlEventTouchUpInside];

//背景颜色按钮的功能

[backgroundButton addTarget:self action:@selector(editSceneToBgColorScene:) forControlEvents:UIControlEventTouchUpInside];

//画笔颜色按钮的功能

[midButton addTarget:self action:@selector(editSceneToLineColorScene:) forControlEvents:UIControlEventTouchUpInside];

//画笔大小按钮的功能

[lineWidthButton addTarget:self action:@selector(editSceneToLineWidthScene:) forControlEvents:UIControlEventTouchUpInside];

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

二,画图

布局和跳转写好之后,就要完成主要功能,最主要功能是画图,界面三

建立一个画图类,.h文件如下:

@interface TuyaPad : UIView

@property (nonatomic,strong)UIColor * lineColor;

@property (nonatomic,assign)CGFloat lineWidth;

-(UIImage *)getImageFromCurrentContext;

@end

实现内容如下:

#import "TuyaPad.h"

@interface TuyaPath:NSObject

@property (nonatomic,assign)CGMutablePathRef path;

@property (nonatomic,assign)CGColorRef color;

@property (nonatomic,assign)CGFloat width;

@end

@implementation TuyaPath

@end

@interfaceTuyaPad()

//存储路径信息

@property (nonatomic,strong)NSMutableArray* pathlist;

@end

@implementation TuyaPad

{

//触摸路径

CGMutablePathRef touchPath;

//中间变量路径

CGMutablePathRef tempPath;

//三个触摸点

CGPoint startPoint,controlPoint,endPoint;

}

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.lineColor=[UIColor blackColor];

self.lineWidth=10.f;

self.pathlist=[[NSMutableArray alloc]init];

}

returnself;

}

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

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

[super drawRect:rect];

//1,获取当前画布

CGContextRef context=UIGraphicsGetCurrentContext();

//2,划线

for (TuyaPath *path in self.pathlist) {

//设置描绘颜色

CGContextSetStrokeColorWithColor(context, path.color);

//设置线条宽度

CGContextSetLineWidth(context, path.width);

//将路径添加到画布上

CGContextAddPath(context, path.path);

//设置线条形状

CGContextSetLineCap(context, kCGLineCapRound);

//开始描绘

CGContextStrokePath(context);

}

}

//触摸开始

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

{

//实例化tuyaPath对象

TuyaPath *itemPath=[[TuyaPath alloc]init];

itemPath.color=self.lineColor.CGColor;

itemPath.width=self.lineWidth;

//实例化触摸路径对象

touchPath=CGPathCreateMutable();

itemPath.path=touchPath;

//将TuyaPath对象添加到路径数组内

[self.pathlist addObject:itemPath];

//获取触摸对象

UITouch *touch=[touches anyObject];

startPoint =[touch previousLocationInView:self];

controlPoint=[touch previousLocationInView:self];

endPoint=[touch locationInView:self];

[self touchesMoved:touches withEvent:event];

}

//触摸移动

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

{

//获取触摸对象

UITouch *touch=[touches anyObject];

startPoint=controlPoint;

controlPoint=[touch previousLocationInView:self];

endPoint=[touch locationInView:self];

CGFloat mid1x=(startPoint.x+controlPoint.x)/2.0;

CGFloat mid1y=(startPoint.y+controlPoint.y)/2.0;

CGFloat mid2x=(controlPoint.x+endPoint.x)/2.0;

CGFloat mid2y=(controlPoint.y+endPoint.y)/2.0;

tempPath=CGPathCreateMutable();

CGPathMoveToPoint(tempPath, NULL, mid1x, mid1y);

CGPathAddQuadCurveToPoint(tempPath, NULL, controlPoint.x, controlPoint.y, mid2x, mid2y);

//将中间变量路径添加到触摸路径内

CGPathAddPath(touchPath, NULL, tempPath);

//获取路径数组内的最后一个元素

TuyaPath *laseItem=[self.pathlist lastObject];

laseItem.path=touchPath;

//根据中间变量路径,计算最小范围

CGRect boundingBox=CGPathGetBoundingBox(tempPath);

boundingBox.origin.x-=self.lineWidth;

boundingBox.origin.y-=self.lineWidth;

boundingBox.size.width+=2*self.lineWidth;

boundingBox.size.height+=2*self.lineWidth;

//[self setNeedsDisplay];

[selfsetNeedsDisplayInRect:boundingBox];//在原画板上画bounding区域的内容

}

//触摸结束

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

{

//获取触摸路径对象

TuyaPath *lastPath=[self.pathlist lastObject];

//指定触摸路径对象结束时,触摸路径是touchPath

lastPath.path=touchPath;

}

-(UIImage *)getImageFromCurrentContext

{

UIGraphicsBeginImageContext(self.bounds.size);//创建画布用UIGraphicsGetCurrentContext()可以获得

[self.layerrenderInContext:UIGraphicsGetCurrentContext()];//self.layer当前画布上的内容  渲染到图片画布

UIImage *result=  UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[self.pathlistremoveAllObjects];

[selfsetNeedsDisplay];

return result;

}

@end

主要内容如上,剩下的是细节和部分功能,在下节上主要代码.

IOS 作业项目(4)步步完成 画图 程序(中续)的更多相关文章

  1. IOS 作业项目(4)步步完成 画图 程序(中)

    一,承接上文,继续本文  [UIButton buttonWithType:UIButtonTypeRoundedRect]; 如此声明的按钮才会有点击闪动的效果!如果直接frame方式声明就不会有. ...

  2. IOS 作业项目(4)步步完成 画图 程序(问题处理)终结

    一,解决换色程序崩溃问题 程序崩溃是因为颜色的内存被释放,添加如下类的内容即可 @implementation TuyaPath - (id)init { self = [super init]; i ...

  3. IOS 作业项目(4)步步完成 画图 程序(剧终)

    // //  CHViewController.m //  SuperDrawingSample // //  Created by JaikenLI on 13-11-21. //  Copyrig ...

  4. IOS 作业项目(4)步步完成 画图 程序(上)

    先上流程图

  5. IOS 作业项目(2) 画图(保存,撤销,笔粗细设定功能)

    先上效果图

  6. IOS 作业项目 TableView两个section中cell置顶功能实现

    点击cell会置顶,其他的下移

  7. IOS 作业项目(3) 霓虹灯效果

    先上效果图 #import "CHViewController.h"@interface CHViewController (){    int i;    int j;}@pro ...

  8. IOS 作业项目(1) 关灯游戏 (百行代码搞定)

    1,准备工作,既然要开关灯,就需要确定灯的灯的颜色状态 首先想到的是扩展UIColor

  9. iOS:项目中疑难Crash问题集锦

    项目中疑难Crash问题集锦 iOS App运行中遇到Crash的情况相信大家都遇到过,开发和者测试中遇到了可能很方便的办法就是直接拿着设备连接一下,然后使用Xcode自带的工具就可以解析出Crash ...

随机推荐

  1. asp.net 奇淫技巧

    VS2013自动注释插件 分享一个Visual Studio的背景插件,让堆码更富情趣 工欲善其事必先利其器系列之:更换Visual Studio代码风格. 为Visual Studio更换皮肤和背景 ...

  2. 去除DataTable重复数据的三种方法

    业务需求 最近做一个把源数据库的数据批次导出到目标数据库.源数据库是采集程序采集而来的原始数据库,所以需要对其进行一些处理(过滤一些为空,长度太短或太长,非法字符,重复数据)然后在进行入库. 其中要避 ...

  3. 意外的节点类型 Element。只能在简单内容或空内容上调用 ReadElementString 方法

    问题出现的情景: 在调用携程团购接口时,需要把获取的xml字符串反序列化实体对象,出现了这个错误. 详情: 在对xml文档有这样一条语句“ <Description Category=" ...

  4. 客户信贷管理&临时授信

    信贷额度的组成:假如某客户信用限额1万:开出销售订单时锁定1万:一旦发货1万,销售订单1万限额释放,变成发货锁定限额1万.一旦开票,发货1万限额释放,应收锁定1万限额.清帐成功,应收释放1万.信用限额 ...

  5. Extjs 视频教程

    ---恢复内容开始--- 网易云课堂 <尚学堂_Ext视频教程> login.html <html> <head> <meta http-equiv=&quo ...

  6. [转]开发者需要了解的WebKit(mark)

    以下内容转自:http://www.infoq.com/cn/articles/webkit-for-developers -------------------------------------- ...

  7. ANGULAR JS PROMISE使用

    Promise是一种模式,以同步操作的流程形式来操作异步事件,避免了层层嵌套,可以链式操作异步事件. 我们知道,在编写javascript异步代码时,callback是最最简单的机制,可是用这种机制的 ...

  8. cmd命令行中的errorlevel和延迟赋值

    最近用到了命令行,一点心得: 1.errorlevel返回的确实是上一条命令的返回值,但不同命令的表现完全不同.比如: dir echo %errorlevel% //显示0 dir aldkalf ...

  9. virtualbox安装提示出现严重错误解决办法

    解决办法: 在服务里面启动1. Device Install Service2. Device Setup Manager 这两个服务就好了.也有可能只需要启动第一个.

  10. 可以创建专业的客户端/服务器视频会议应用程序的音频和视频控件LEADTOOLS Video Conferencing SDK

    LEADTOOLS Video Streaming Module控件为您创建一个自定义的视频会议应用程序和工具提供所有需要的功能.软件开发人员可以使用Video Streaming Module SD ...