自适应 UICollectionViewCell 背景图像 - locked/unlocked 个游戏关卡

Adaptable UICollectionViewCell Background image - locked/unlocked game levels

我使用 UICollectionView 的经验很少,但我设法创建了一个可滚动界面,显示 100 个级别的 100 个按钮,4 个横向,25 个向下(好吧,它们是在 -如你所知,苍蝇)。级别的类型称为 Tortoise 只是为了让你想知道后面到底代表什么。对于Tortoise,只有20个级别。

现在,我用来显示数字的单元格数据字符串被放置在单元格的常规背景上(表示该级别已解锁但未完成)。

我还有另外 2 张图片我想用作背景图片(一张是没有数字字符串出现的锁定图片,另一张是与上面相同的背景,只是带有一个小的完成标记(以及顶部的数字字符串)).

简而言之,我正在使用 Core Data 来跟踪一组对象,以及关卡是锁定还是解锁。我调用了一个名为 figureOutLocks 的方法(就在我进入 Tortoise UICollectionView 时),它在此数组 self.lockArray 中存储 0、1 或 2 NSNumber 对象。这是该方法:

    - (void)figureOutLocks {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSManagedObjectContext* managedObjectContext = [(AppDelegate*)[[UIApplication sharedApplication]
                                                                    delegate] managedObjectContext];
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Score" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entityDescription];
        for (int i = 0; i < 20; i++) {
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %d AND %K == %d",
                                       @"level", i, @"typeOfLevel", 0];
            [fetchRequest setPredicate:predicate];
            NSError *error = nil;
            Score* lockInfo = [[managedObjectContext executeFetchRequest:fetchRequest
                                                                     error:&error] lastObject];

            lockInfo.levelCompleted = [lockInfo valueForKey:@"levelCompleted"];
            lockInfo.lockedLevel = [lockInfo valueForKey:@"lockedLevel"];
            NSInteger complete = [lockInfo.levelCompleted integerValue];
            NSInteger locked = [lockInfo.lockedLevel integerValue];

            if ((locked == 0) && (complete == 0)) {
                // level is unlocked but not complete (does not have any saved scores)
                // lockArray gets a 0
                [self.lockArray addObject:[NSNumber numberWithInteger:0]];
            } else if ((locked == 1) && (complete == 0)) {
                // level is locked which implies it is not complete
                // lockArray gets a 1
                [self.lockArray addObject:[NSNumber numberWithInteger:1]];
            } else if ((locked == 0) && (complete == 1)) {
                // level is complete thus it is unlocked
                // lockArray gets a 2
                [self.lockArray addObject:[NSNumber numberWithInteger:2]];
            }
        }
    }

再提示一下,第一关解锁未通关,其他关卡锁定(未通关)

此外,我创建了一个包含字符串对象 1-20 的 NSArray *dataArray 和一个包含 NSNumber 对象 1-20 的 NSArray *compareArray。我的 lockArrayNSMutableArray

此外,我决定制作 2 个单独的 UICollectionViewCell 子类,以便同时使用常规背景和锁定背景。我没有添加完成背景子类,因为我想确保锁定的背景有效。

这里是主要方法:

    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
             cellForItemAtIndexPath:(NSIndexPath *)indexPath {

        UICollectionViewCell *regular;

        static NSString *cellIdentifier = @"tortoiseCell";
        static NSString *tortIdentifier = @"tortoiseLocked";
        TortoiseCell *cell = (TortoiseCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        TortoiseLocked *lockCell = (TortoiseLocked *)[collectionView dequeueReusableCellWithReuseIdentifier:tortIdentifier forIndexPath:indexPath];

        NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
        NSString *cellData = [data objectAtIndex:indexPath.row];
        NSMutableArray *locks = [self.compareArray objectAtIndex:indexPath.section];
        NSNumber *locksData = [locks objectAtIndex:indexPath.row];
        NSInteger locked = [locksData integerValue];
        NSInteger lock = [[self.lockArray objectAtIndex:locked] integerValue];

        if (lock == 0) {
           [cell.buttonClick setTag:indexPath.row];
           [cell.buttonClick setTitle:cellData forState:UIControlStateNormal];
           [cell.buttonClick setBackgroundImage:[UIImage imageNamed:@"TortoiseLevels.png"]
                                forState:UIControlStateNormal];
           [cell.buttonClick setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
           [cell.buttonClick.titleLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:25]];
           [cell.buttonClick addTarget:self action:@selector(buttonPressedSoWhatNumber:)
               forControlEvents:UIControlEventTouchUpInside];
           cell.buttonClick.layer.cornerRadius = 8;
           cell.buttonClick.layer.masksToBounds = YES;
           [cell addSubview:cell.buttonClick];
           cell.layer.shouldRasterize = YES;
           cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
           regular = cell;
        } else if (lock == 1) {
           [lockCell.tortoiseLock setTag:indexPath.row];
           [lockCell.tortoiseLock setBackgroundImage:[UIImage imageNamed:@"TortoiseLock.png"]
                                forState:UIControlStateNormal];
           lockCell.tortoiseLock.layer.cornerRadius = 8;
           lockCell.tortoiseLock.layer.masksToBounds = YES;
           [lockCell addSubview:lockCell.tortoiseLock];
           lockCell.layer.shouldRasterize = YES;
           lockCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
           regular = lockCell;
        }

        return regular;
    }

我做的事情有可能吗?如果可以,我该如何使它工作?我尝试使用一个 UICollectionViewCell 子类并以编程方式更改背景,但这没有用,这就是为什么您会看到较大的 if 语句所看到的内容。有什么想法吗?

您绝对不需要从 table 中取出 两个 个单元格 - 您只会使用其中一个。您的代码结构应该更像这样:

static NSString *cellIdentifier = @"tortoiseCell";
static NSString *tortIdentifier = @"tortoiseLocked";

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    UICollectionViewCell *regular;

    NSMutableArray *locks = [self.compareArray objectAtIndex:indexPath.section];
    NSNumber *locksData = [locks objectAtIndex:indexPath.row];
    NSInteger locked = [locksData integerValue];

    NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];
    NSString *cellData = [data objectAtIndex:indexPath.row];
    NSInteger lock = [[self.lockArray objectAtIndex:locked] integerValue];

    if (lock == 0) {

        TortoiseCell *cell = (TortoiseCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
        [cell.buttonClick setTag:indexPath.row];
        [cell.buttonClick setTitle:cellData forState:UIControlStateNormal];
        [cell.buttonClick setBackgroundImage:[UIImage imageNamed:@"TortoiseLevels.png"]
                                    forState:UIControlStateNormal];
        [cell.buttonClick setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [cell.buttonClick.titleLabel setFont:[UIFont fontWithName:@"Arial Rounded MT Bold" size:25]];
        [cell.buttonClick addTarget:self action:@selector(buttonPressedSoWhatNumber:)
                   forControlEvents:UIControlEventTouchUpInside];
        cell.buttonClick.layer.cornerRadius = 8;
        cell.buttonClick.layer.masksToBounds = YES;
        [cell addSubview:cell.buttonClick];
        cell.layer.shouldRasterize = YES;
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
        regular = cell;

    } else if (lock == 1) {

        TortoiseLocked *lockCell = (TortoiseLocked *)[collectionView dequeueReusableCellWithReuseIdentifier:tortIdentifier forIndexPath:indexPath];
        [lockCell.tortoiseLock setTag:indexPath.row];
        [lockCell.tortoiseLock setBackgroundImage:[UIImage imageNamed:@"TortoiseLock.png"]
                                         forState:UIControlStateNormal];
        lockCell.tortoiseLock.layer.cornerRadius = 8;
        lockCell.tortoiseLock.layer.masksToBounds = YES;
        [lockCell addSubview:lockCell.tortoiseLock];
        lockCell.layer.shouldRasterize = YES;
        lockCell.layer.rasterizationScale = [UIScreen mainScreen].scale;
        regular = lockCell;
    }

    return regular;
}

此外,既然您已经在故事板上设置了两种类型的单元格原型,为什么还要以编程方式设置每个单元格呢?只需让它们在故事板上看起来像您想要的那样,然后在代码中添加关卡编号即可。