让用户在视图上绘画的技巧是什么?

时间:2012-03-20 05:46:40

标签: ios quartz-2d

iOS下的最佳做法是让用户在交互模式下绘制矩形,段或其他简单形状?假设我在第一次按下时抓住,然后在移动手指时我该怎么办?我应该抓住当前坐标,设置一些内部点并为视图调用setNeedsDisplay并在这两个点之间绘制线条?

如果我想在那里已经有一堆形状的表面上画画怎么办?是不是需要花太多时间来重温它们并重新粉刷?

在Windows下,我使用XOR操作仅绘制用户移动鼠标时所需的内容,iOS中的技术是什么? 任何文章都非常感激。

THX

1 个答案:

答案 0 :(得分: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;
}

希望它会有所帮助