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 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章