开发者

Get the identity of the image loaded at the centre of the screen currently?

开发者 https://www.devze.com 2022-12-13 07:40 出处:网络
I have a uiscrollview with contentsize 1280*1280 and 25 images of equal size loaded in it. When i scroll and meet an end, I want to know the identity of the image which is currently present at the scr

I have a uiscrollview with contentsize 1280*1280 and 25 images of equal size loaded in it. When i scroll and meet an end, I want to know the identity of the image which is currently present at the screen centre. Given below is the code that i used to load the images into the scrollView. How can i figure it out?

    for (i = 0; i < 5; i++) {
    if(j == 5)
    {
        tx = tx+1;
        x=x+256;
        ty = ty+5;
        y = y-1280;
    }
    for (j = 0; j < 5; j++) {
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://stage.discoveritalia.i开发者_开发技巧t/tiles/%d/%d/%d.png",zoom,tx,ty]]]]];
        frame = [imageView frame];
        frame.origin.x = x;
        frame.origin.y = y;
        [imageView setFrame:frame];
        [myScrollView addSubview:imageView];

        ty = ty-1;
        y = y+256;

        [imageArray insertObject:imageView atIndex:i*5+j];
        [imageView release];
     }
}

If I could get the URL back, it would solve my problem right away. Is there a way to do it?


  1. Calculate the CGPoint at the center of the screen (depending on orientation)
  2. Divide the x and y values by 1280 / 5 and add ((1280 / 5) / 2) to determine the tile i-j indices that the center would overlap, with no UIScrollView offset
  3. Now add the current scroll view offset to determine which tile the center actually overlaps
0

精彩评论

暂无评论...
验证码 换一张
取 消