I developed a Kuku Kube game for iOS. Each time I choose a correct different color tile, it will increase a number of tile quantity 2x2 3x3...10x10. How to optimise the code each time the tile increase that auto fit on ip4 to ip6 Plus? I think it can not use auto layout to set for the size.
- (void)setSizeforTile {
tiles = (NSInteger)pow(level, 2);
randomTile = arc4random() % tiles;
sizeForCell = 0.0;
switch (level) {
case 2:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:125 withItemSpacing:5];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:150 withItemSpacing:10];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:180 withItemSpacing:5];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:200 withItemSpacing:4];
break;
case 3:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:82 withItemSpacing:4.5];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:100 withItemSpacing:5];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:120 withItemSpacing:2.5];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:133 withItemSpacing:2.5];
break;
case 4:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:61.5 withItemSpacing:3];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:76 withItemSpacing:2];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:89 withItemSpacing:3];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:99 withItemSpacing:2.5];
break;
case 5:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:49 withItemSpacing:2.5];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:60 withItemSpacing:2];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:70 withItemSpacing:3];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:78 withItemSpacing:3.5];
break;
case 6:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:41 withItemSpacing:1.8];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:49 withItemSpacing:3];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:58 withItemSpacing:3.2];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:65 withItemSpacing:2.8];
break;
case 7:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:35.5 withItemSpacing:1];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:41.5 withItemSpacing:3];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:50 withItemSpacing:2.5];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:56 withItemSpacing:2];
break;
case 8:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:31 withItemSpacing:1];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:36 withItemSpacing:3];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:42.5 withItemSpacing:3];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:48 withItemSpacing:2.2];
break;
case 9:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:27.5 withItemSpacing:0.9];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:32 withItemSpacing:2.75];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:38 withItemSpacing:2.8];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:43 withItemSpacing:2.1];
break;
case 10:
if (IS_IPHONE_4_OR_LESS) [self setCollectionViewCellDetail:24.5 withItemSpacing:0.9];
if (IS_IPHONE_5) [self setCollectionViewCellDetail:28.5 withItemSpacing:2.75];
if (IS_IPHONE_6) [self setCollectionViewCellDetail:35 withItemSpacing:1.6];
if (IS_IPHONE_6P) [self setCollectionViewCellDetail:39 withItemSpacing:1.5];
break;
default:
break;
}
cellSize = CGSizeMake(sizeForCell, sizeForCell);
// NSLog(@"%d-%.1f-%.1f",level, sizeForCell, cellLineSpacing);
}
- (void)setCollectionViewCellDetail:(CGFloat)itemCellSize withItemSpacing:(CGFloat)itemCellSpacing {
sizeForCell = itemCellSize;
cellLineSpacing=itemCellSpacing;
itemSpacing = itemCellSpacing;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return cellSize;
}
- (void)randomColor {
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
[self setSizeforTile];
}
tiles
,randomTile
, orsizeForCell
? How aboutcellSize
? And thensizeForCell
,cellLineSpacing
, anditemSpacing
? AndcellSize
? How aboutcolor
? The only variables that are actually declared anywhere are yourhue
,saturation
, andbrightness
. Are all of the variables I've mentioned instance variables? If so, why aren't you using properties (nevermind object-oriented techniques)? \$\endgroup\$