Search code examples
ioscgrectcgrectmake

making a CGRect off screen


Is it possible to create UIImageView within a CGRect offscreen?

I will then animate it onto the screen after a user pushes a UIButton.

I know this function CGRectMake(x,y,height, width) but apparently x and y cannot have negative values.

Can anyone help me?


Solution

  • Yes it is possible. There are a couple of things that you could do. Depending on where you want it to animate in from, set the x and y of the imageview to be negative or greater than <parentView>.bounds.size.width or height etc. Then you can use UIView to animate changes.

    So in viewDidAppear or whatever method triggers the animation you can use From apple docs:

    + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion

    to handle all of the animations. Here is an example:

    [UIView animateWithDuration:<timeInSecondsOfHowLongItShouldTake>
                animations:^
                {
                        imageView.frame = <CGRectFrameOfPlaceThatYouWantItToGo>
                        //you could also make it fade in with imageView.alpha = 1;
                        //assuming you started out with an alpha < 1
                }
                completion:^(BOOL finished)
                {
                        //do something after it finishes moving 
                        //the imageview into place
                }
    ];