Search code examples
objective-cuiimageviewcgrect

How to get object position and size in UIView objective c


I put UIImageView in my Scene from Object library, and give it an image and defined OUTLET in .h file. Now I want to check its coordinates, or center point, or frame X,Y,Width,Height. I am using This

CGRect newFrameSize = CGRectMake(recycleBin.frame.origin.x, recycleBin.frame.origin.y,
recycleBin.frame.size.width, recycleBin.frame.size.height);

or

CGRect newFrameSize = recycleBin.frame;

by using this

NSLog(@"%@", NSStringFromCGRect(newFrameSize));

gives same result that is

2013-01-16 21:42:25.101 xyzapp[6474:c07] {{0, 0}, {0, 0}}

I want its actual position and size when viewcontroller loaded, so when user click on image view it will fadeout by zoom-in towards users and will disappear, and when user tap on reset button, it fadein and zoom-in back to original form (reverse to the previous animation). Also give me hint, how to perform this animation on UIImageView or any button or label. Thx


Solution

  • Unfortunately, you can't check an item's actual frame as set in IB in -viewDidLoad. The earliest you can check it (that I've found) is by overriding -viewDidAppear:. But, since -viewDidAppear: could be called multiple times throughout the life of the view, you need to make sure you're not saving the frame it's in the modified state.

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        if(savedFrame == CGRectZero) {
            savedFrame = self.recycleBin.frame;
            NSLog(@"Frame: %@", NSStringFromCGRect(savedFrame));
        }
    }
    

    Where savedFrame is a member variable (or you could make it a property).

    From the description of the animation you're wanting, it sounds like adjusting the frame isn't the way to go about it. It sounds like you're wanting to get the effect of the view stretching and fading out (and the reverse when being reset)? If so, some code like this might be more so what you're looking for...

    Fade out:

    float animationDuration = 2.0f; // Duration of animation in seconds
    float zoomScale = 3.0f; // How much to zoom in duration the animation
    [UIView animateWithDuration:animationDuration animations:^{
        CGAffineTransform transform = CGAffineTransformMakeScale(zoomScale, zoomScale);
        self.recycleBin.transform = transform;
        self.recycleBin.alpha = 0; // Make fully transparent
    }];
    

    And then, to reset the view:

    float animationDuration = 2.0f; // Duration of animation in seconds
    [UIView animateWithDuration:animationDuration animations:^{
        CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 1.0f);
        self.recycleBin.transform = transform;
        self.recycleBin.alpha = 1.0; // Make fully opaque
    }];
    

    You can play around with the numbers to see if you get the effects you desire. Most animations in iOS are actually extremely simple to do. This code would work for any UIView subclass.