0

So I am using CATiled layer to create my own floor plan. My floor plan works just fine on older devices like the 4, 4s, 5, 5s and 6. However, when I run my program on the 6 plus, my arithmetic for zoom level is off. Here's why: When I call my draw rect method, I get different tile width's based on the device. When I call my images, I call them in the sequence of ZoomLevel_COL_ROW.image . My zoom level is multiples of 2. So every time you pinch to zoom, my ZoomLevel is called in order: 2, 4, 8, 16 and 32. My problem I am having is I can't figure out how to divide the tiles width by 512 to get me the appropriate zoom level. So on a 6plus, my initial tile width when the view loads is 85.53. So 512/85.3 gets me 6 - which is not apart of my sequence. However, on a 5s I get a tile width of 128. So 512/128 gets me 4 - which is apart of my sequence, however skips level 2. So I am having a tough time determining what would be a consistent arithmetic to get all zoom level sequences every time you pinch to zoom. Recall that my sequence is 2, 4, 8, 16,32. Hopefully I am clear, and everyone understands. Thanks!

- (void)drawRect:(CGRect)rect
{



//    NSLog(@"drawrect %@", NSStringFromCGRect(rect));
    zoomLevel = (512 / (CGRectGetWidth(rect)));

    int size = 512;
    int zoom = 1;

    while (size > rect.size.width) {
        size /= 2;
        zoom ++;
    }



    NSLog(@"Zoomlevel %d", zoomLevel);

    CGSize tileSize = rect.size;

    int firstCol = floorf(CGRectGetMinX(rect) / tileSize.width);
    int lastCol = floorf((CGRectGetMaxX(rect)-1) / tileSize.width);
    int firstRow = floorf(CGRectGetMinY(rect) / tileSize.height);
    int lastRow = floorf((CGRectGetMaxY(rect)-1) / tileSize.height);

    for (int row = firstRow; row <= lastRow; row++) {
        for (int col = firstCol; col <= lastCol; col++) {

            UIImage *tile = [self tileAtCol:col row:row];

            float width = tileSize.width;
            float height = tileSize.height;

            if (tile.size.width != tile.size.height) {
                // make sure that smaller tiles aren't stretched
                float widthRatio = tile.size.width / 512;
                float heightRatio = tile.size.height / 512;

                width *= widthRatio;
                height *= heightRatio;
            }

            CGRect tileRect = CGRectMake(tileSize.width * col,
                                         tileSize.height * row,
                                         width, height);

            tileRect = CGRectIntersection(self.bounds, tileRect);

            [tile drawInRect:tileRect];


        }
    }
}
imnilly
  • 317
  • 2
  • 4
  • 10

1 Answers1

0

Figured out why. It had to do with scale Factor! Because iPhone 6 plus has different resolution. Check out the code below:

scaleFactor = [UIScreen mainScreen].scale;


float width = rect.size.width;



width *= scaleFactor;
zoomLevel = (512/ width);
imnilly
  • 317
  • 2
  • 4
  • 10