ios 动画与2D、3D绘图

原文:http://www.cnblogs.com/hanjun/archive/2012/11/25/2787682.html by:What_If



主要是3种方式,Core Animation、Core Graphic和OpenGL ES。

   操作简易度:CA>CG>OpenGL

   性能和功能度:OpenGL>CG>CA

 

1.Core Animation

  非娱乐类的软件都会用到的动画,操作简单。

2.Quartz 2D绘图

   是一个2D绘图引擎。

  (1) 绘图Context是一个绘图的目标对象,定义了绘图的基本属性,如颜色、绘图范围、线宽及样式等。

   (2)通过UIView会创建Context,可以用类似如下的语句来得到当前的Context.

   CGContextRef currentContext = UIGraphicsGetCurrentContext();

   (3)如果在对其进行修改前想要保存当前状态,可以使用UIGraphicsPushContext;

         要恢复保存过的Context,则可用UIGraphicsPopContext。

   (4)path,路径其实就是用一系列坐标点标识出来的一条曲线或折线,创建好之后就可以沿其画线,或者对封闭的空间进行填充。 

 

通过一个简单的画板(白板)来熟悉 Quartz 2D绘图

 

添加一个Empty Application,新建一个UIView类

 在AppDelegate中修改代码:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

复制代码
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
     //  Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
     // 创建View,并指定View的大小为全屏
    WhiteBoardView *whiteView = [[WhiteBoardView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    [self.window addSubview:whiteView]; // 把创建的View作为子视图加入窗口

    [whiteView release];
    
    [self.window makeKeyAndVisible];
     return YES;
}
复制代码

 修改UIView类中代码:

 

复制代码
//   WhiteBoardView.h
//   DrawingBoard


#import <UIKit/UIKit.h>

@interface WhiteBoardView : UIView{
    
    CGContextRef whiteBoardContext;        
    CGLayerRef    whiteBoardLayer;        
    
}
复制代码

@end

 

实现类:

 

复制代码
//   WhiteBoardView.m
//   DrawingBoard


#import  " WhiteBoardView.h "

@implementation WhiteBoardView

// 对进行重写,以便在视图初始化的时候创建并设置自定义的Context
- ( id)initWithFrame:(CGRect)frame 
{        
     if (self = [super initWithFrame:frame]) {        
        
        self.backgroundColor = [UIColor whiteColor]; // 指定视图的背景色    
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();     // 指定色彩空间为RGB    
        
        
// 创建Bitmap Context,大小为View自身的大小
        whiteBoardContext = CGBitmapContextCreate(NULL, self.frame.size.width, self.frame.size.height,  8
                                                   4 *self.frame.size.width, colorSpace, kCGImageAlphaPremultipliedFirst);    
        CGColorSpaceRelease(colorSpace) ;    
        
         // 创建新的CGLayer,用于画图
        whiteBoardLayer = CGLayerCreateWithContext(whiteBoardContext, self.frame.size, NULL);    
        
         // 得到新建层的Context
        CGContextRef layerContext  = CGLayerGetContext(whiteBoardLayer);        
        
         // 指定新建层Context的线宽
        CGContextSetLineWidth(layerContext,  1.5);        
        CGContextSetLineCap(layerContext, kCGLineCapRound);     // 指定线头形状为圆形    
        CGContextSetRGBStrokeColor(layerContext,  0.0, 0.0, 0.0, 1); // 指定线的颜色,黑色
        
    }
     return self;    
}

// 对drawRect进行重写
- ( void)drawRect:(CGRect)rect 
{    
     // 先得到当前的Context
    CGContextRef currentContext = UIGraphicsGetCurrentContext();    
    
     // 根据Context的内容生成Bitmap
    CGImageRef image = CGBitmapContextCreateImage(whiteBoardContext);
     // 把Bitmap绘制到Context上
    CGContextDrawImage(currentContext, [self bounds], image);    
     // 把whiteBoardLayer也绘到当前Context中
    CGContextDrawLayerInRect(currentContext    , [self bounds], whiteBoardLayer);    
    
}

// 屏幕触摸事件  触摸开始
-( void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{    
    UITouch *theTouch = [touches anyObject]; // 先得到touch对象                    
     if ([theTouch tapCount] ==  2) // 判断是否为双击,是就清空图像
    {                                    
        CGContextClearRect(whiteBoardContext, [self bounds]); // 清空    
    
        [self setNeedsDisplay];     // 刷新屏幕显示
    }
     else // 如果不是双击,就按手指划动处理,结果是画出一个点
    {                                                            
        [self touchesMoved:touches withEvent: event];                
    }
}

// 手指划动时画线的代码
-( void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *) event
{    
    UITouch *theTouch = [touches anyObject];
    
     // 得到当前位置
    CGPoint currentTouchLocation = [theTouch locationInView:self];    
     // 得到上次位置
    CGPoint lastTouchLoacation = [theTouch previousLocationInView:self];
    
     // 取得Context
    CGContextRef layerContext = CGLayerGetContext(whiteBoardLayer);        
     // 开始定义Path
    CGContextBeginPath(layerContext);
     // 把Path的起点移动到上次位置
    CGContextMoveToPoint(layerContext, lastTouchLoacation.x, lastTouchLoacation.y);
     // 在上次位置和当前位置之间连线,并记入Path
    CGContextAddLineToPoint(layerContext, currentTouchLocation.x, currentTouchLocation.y);    
     // 沿Path画线
    CGContextStrokePath(layerContext);                                        
    
    [self setNeedsDisplay];     // 刷新屏幕                                                
    
}




- ( void)dealloc 
{
    CGContextRelease(whiteBoardContext);                                    
    CGLayerRelease(whiteBoardLayer);                                    
    [super dealloc];
}
复制代码

@end

 

 

 

3.OpenGL ES编程

  一般是三个步骤:

(1)在Context中绘图

(2)把Context中的内容送入framebuffer

(3)显示framebuffer 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值