gpt4 book ai didi

objective-c - 加速ios功能

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:20:18 26 4
gpt4 key购买 nike

我真的需要帮助!我正在构建一个包含大量图 block 的 map ……比如 200x200 的 50 像素小图 block 。这些图 block 位于 UIScrollView 中,因此我可以在 map 上拖动自己!真的很好用吗?嗯,它 super 慢,因为 map 中有太多 subview 。

好的,所以我在滚动时加载 subview !问题是,这也需要一些时间,使滚动有点滞后/缓慢。你能帮我改进这个功能吗?我没主意了。找到了 Grand Central Dispatch,但我不知道如何使用它。

这是我的代码

-(void)loadMapTiles {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(1, queue,
^(size_t row) {
int radiusX = 11;
int radiusY = 7;
int y = currentCoord.y-radiusY;
for(int x = currentCoord.x-radiusX; x < currentCoord.x+radiusX+1; x++) {
int currTag = mapSize.width*(y-1)+x;
if(![mapScroller viewWithTag:currTag]) {
UIImageView *mapTile = [[UIImageView alloc] initWithFrame:CGRectMake(50*x, 50*y, 50, 50)];
int mapNo = (rand() % 20) + 1;
if(mapNo > 11) {
mapNo = 1;
}

mapTile.image = [UIImage imageNamed:[NSString stringWithFormat:@"desert_map%d_50.gif", mapNo]];

//Tag the mapTile so that we can find it again!
[mapTile setTag:currTag];

[mapContentView addSubview:mapTile];
[mapTile release];
}
if(x == currentCoord.x+radiusX && y < currentCoord.y+radiusY) {
x = currentCoord.x-radiusX-1;
y++;
}
}
//Remove the other tiles outside the radius!
for (UIView *mapTile in [mapContentView subviews]) {
if(mapTile.frame.origin.x / 50 < currentCoord.x-radiusX || mapTile.frame.origin.x / 50 > currentCoord.x+radiusX || mapTile.frame.origin.y / 50 < currentCoord.y-radiusY || mapTile.frame.origin.y / 50 > currentCoord.y+radiusY) {
[mapTile removeFromSuperview];
}
}
});
}

以及调用上面建图函数的函数:

-(void)scrollViewDidScroll:(UIScrollView *)myScrollView {  
CGPoint newCoord = CGPointMake((int)(myScrollView.contentOffset.x + myScrollView.frame.size.width/2) /50, (int)(myScrollView.contentOffset.y + myScrollView.frame.size.height/2) /50);
if(newCoord.x != currentCoord.x || newCoord.y != currentCoord.y) {
currentCoord = newCoord;
[self loadMapTiles];
}
}

最佳答案

你会想看看CATiledLayer .无论您的 map 有多大,它都可以让您以一种很好的方式做您想做的事。它还支持针对不同的缩放级别使用不同的图像。

关于objective-c - 加速ios功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8509205/

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