gpt4 book ai didi

ios - 让用户在 View 上绘画的技术是什么?

转载 作者:行者123 更新时间:2023-11-28 17:36:08 25 4
gpt4 key购买 nike

iOS 下让用户在交互模式下绘制矩形、线段或其他简单形状的最佳实践是什么?假设我在用户第一次按下时捕捉到,那么当移动手指时我该怎么办?我应该捕捉当前坐标,设置一些内部点并为 View 调用 setNeedsDisplay 并绘制这两个点之间的线吗?

如果我想在已经有一堆形状的表面上绘画怎么办?把它们全部检查一遍再重新粉刷会不会花太多时间?

在Windows下我是用异或运算只绘制用户移动鼠标时需要的东西,iOS有什么技巧?非常感谢任何文章。

谢谢

最佳答案

试试这个

-(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;
}

希望对你有帮助

关于ios - 让用户在 View 上绘画的技术是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9781901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com