gpt4 book ai didi

iphone - 如何在 GLPaint 示例代码中创建自己的 recordedPath

转载 作者:太空狗 更新时间:2023-10-30 04:00:48 24 4
gpt4 key购买 nike

我最近下载了 GLPaint 示例代码并查看了其中非常有趣的部分。有一个 recordedPaths NSMutableArray,其中包含点,然后由 GLPaint 读取和绘制。

这里声明:

NSMutableArray *recordedPaths;
recordedPaths = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recording" ofType:@"data"]];
if([recordedPaths count])
[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.2];

这是播放代码:

 - (void) playback:(NSMutableArray*)recordedPaths {

NSData* data = [recordedPaths objectAtIndex:0];

CGPoint* point = (CGPoint*)[data bytes];

NSUInteger count = [data length] / sizeof(CGPoint),

i;



//Render the current path

for(i = 0; i < count - 1; ++i, ++point)

[self renderLineFromPoint:*point toPoint:*(point + 1)];



//Render the next path after a short delay

[recordedPaths removeObjectAtIndex:0];

if([recordedPaths count])

[self performSelector:@selector(playback:) withObject:recordedPaths afterDelay:0.01];

}

由此我了解到 recordedPaths 是一个可变数组,他在其中构造了 CGPoint 的 c 数组,然后读取和呈现这些数组。我想放入自己的数组,但一直遇到麻烦。

我尝试将 recordedPaths 声明更改为:

      NSMutableArray *myArray = [[NSMutableArray alloc] init];

CGPoint* points;

CGPoint a = CGPointMake(50,50);

int i;

for (i=0; i<100; i++,points++) {

a = CGPointMake(i,i);

points = &a;

}

NSData *data = [NSData dataWithBytes:&points length:sizeof(*points)];

[myArray addObject:data];

虽然这没有用...有什么建议吗?

最佳答案

如果您查看 Recording.data,您会注意到每一行都是它自己的数组。要捕获墨水并播放它,您需要有一个数组数组。出于本演示的目的 - 声明一个可变数组 - writRay

    @synthesize writRay;
//init in code
writRay = [[NSMutableArray alloc]init];

捕捉墨迹

// Handles the continuation of a touch.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

CGRect bounds = [self bounds];
UITouch* touch = [[event touchesForView:self] anyObject];

// Convert touch point from UIView referential to OpenGL one (upside-down flip)
if (firstTouch) {
firstTouch = NO;
previousLocation = [touch previousLocationInView:self];
previousLocation.y = bounds.size.height - previousLocation.y;
/******************* create a new array for this stroke's points **************/
[writRay addObject:[[NSMutableArray alloc]init]];
/***** add 1st point *********/
[[writRay objectAtIndex:[writRay count] -1]addObject:[NSValue valueWithCGPoint:previousLocation]];
} else {
location = [touch locationInView:self];
location.y = bounds.size.height - location.y;
previousLocation = [touch previousLocationInView:self];
previousLocation.y = bounds.size.height - previousLocation.y;
/********* add additional points *********/
[[writRay objectAtIndex:[writRay count] -1]addObject:[NSValue valueWithCGPoint:previousLocation]];
}

// Render the stroke
[self renderLineFromPoint:previousLocation toPoint:location];

}

回放墨迹。

- (void)playRay{

if(writRay != NULL){

for(int l = 0; l < [writRay count]; l++){
//replays my writRay -1 because of location point
for(int p = 0; p < [[writRay objectAtIndex:l]count] -1; p ++){
[self renderLineFromPoint:[[[writRay objectAtIndex:l]objectAtIndex:p]CGPointValue] toPoint:[[[writRay objectAtIndex:l]objectAtIndex:p + 1]CGPointValue]];
}
}
}
}

为了获得最佳效果,摇动屏幕以清除并从 AppController 中的 changeBrushColor 调用 playRay。

关于iphone - 如何在 GLPaint 示例代码中创建自己的 recordedPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2350168/

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