ios 動畫與2D、3D繪圖


   主要是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 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM