Sunday, May 1, 2011

Why does the visibleRect method of CALayer give me always the same width for my UIImageView even if I move it half out of the screen?

I get the visibleRect from an UIImageView inside a method, that is called several times to move that view to the right. As soon as it goes partly out of the screen, I expect that the visibleRect becomes smaller. But it does not.

-(IBAction)moveToRight:(id)sender {
    CGRect frameRect = myUIImageView.frame; //that's an image I use in background of other views
    CALayer *myLayer = [myUIImageView layer];
    CGRect visRect = [myLayer visibleRect];

    NSLog(@"%f, %f", visRect.origin.x, visRect.size.width); //width is always 300.0

    CGPoint rectPoint = frameRect.origin;
    CGFloat newXPos = rectPoint.x + 10.5f;
    myUIImageView.frame = CGRectMake(newXPos, 0.0f, myUIImageView.frame.size.width, myUIImageView.frame.size.height);
}

is there something special I must think about, when I need the true visible Rectangle of an view? Actually I thought that it would be always clipped by the superview, so that method returns the true visible rectangle dimensions.

But as I move the UIImageView step by step to the right, it remains at 300.0 width. Even if a part of it goes out of the window.

From stackoverflow
  • The only clipping that changes the visible rect is from a containing scroll layer.

    Thanks : Thanks. I wonder why Apple didn't mention it right away where they say that this cool Method is so useful ;)
    Alex : I have that same thought about every day with apple ;)

0 comments:

Post a Comment