2012-03-20 76 views
1

iOS下的最佳做法是讓用戶在交互模式下繪製一個矩形,一個線段或其他簡單的形狀?假設用戶第一次按下時抓住,然後在移動手指時我該怎麼辦?我應該抓住當前的座標,設置一些內部點併爲視圖調用setNeedsDisplay,並在這兩點之間畫線?讓用戶在視圖上繪畫的技術是什麼?

如果我想在已經有一堆形狀的表面上繪畫,該怎麼辦?是否花費太多時間來重新研究它們並重新繪製?

在Windows下,我使用XOR操作僅繪製用戶移動鼠標時需要的內容,iOS中有哪些技巧? 任何文章都非常感謝。

THX

回答

0

試試這個

-(void) drawSquareFrom:(CGPoint)from to:(CGPoint)to { 
    CGContextRef context = [self offscreenContext]; 
    CGRect draw , full = [self offscreenContextBounds]; 

    draw.origin = from; 
    draw.size.width = to.x - from.x; 
    draw.size.height = to.y - from.y; 
    draw = CGRectStandardize(draw); 

    [[UIColor redColor] setStroke]; 
    [[UIColor clearColor] setFill]; 
    CGContextClearRect(context , full); 
    CGContextFillRect(context , draw); 
    CGContextStrokeRectWithWidth(context , draw , 10); 
    [_imageView setNeedsDisplay]; 
} 

-(CGContextRef) offscreenContext { 
    if (nil == myContext) { 
     size_t width = 400; 
     size_t height = 300; 
     CFMutableDataRef data = CFDataCreateMutable(NULL , width * height * 4); // 4 is bytes per pixel 
     CGDataProviderRef provider = CGDataProviderCreateWithCFData(data); 
     CGImageRef image = CGImageCreate(width , height , ... , provider , ...); 
     CGBitmapContextRef context = CGBitmapContextCreate(CFDataGetMutableBytePtr(data) , width , height , ...); 
     CFRelease(data); // retained by provider I think 
     CGDataProviderRelease(provider); // retained by image 

     myImage = image; 
     myContext = context; 
     myContextFrame.origin = CGPointZero; 
     myContextFrame.size.width = width; 
     myContextFrame.size.height = height; 
     _imageView.image = [UIImage imageWithCGImage:image]; 
    } 

    return myContext; 
} 
-(CGRect) offscreenContextBounds { 
    return myContextFrame; 
} 

希望對大家有所幫助

+0

我知道如何使用Quartz漆,問題是如何通過他的手指,讓用戶的油漆。像一個小的Visio類型的應用:) – Eugen 2012-03-20 07:36:40

+0

也許這個鏈接將幫助你: http://www.techotopia.com/index.php/An_iOS_4_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D – 2012-03-20 07:49:11

+0

亞歷克斯,請仔細閱讀我的問題,並檢查您的評論,在哪裏做您可以看到與使用觸摸事件的用戶的交互。正如我所說的,我非常清楚如何使用Quartz進行繪製,問題在於通常的技巧是讓用戶繪製某種形狀,就像一些圖形編輯器一樣。 – Eugen 2012-03-20 22:18:11