0

I am having trouble with CGAffineTransformMakeScale. I am displaying a view & hiding it using below code. For the first time it shows & dismissed perfectly. But after than its not showing up again. After I printed the logs it was actually going out of screen coorediantes.

I treid applying reset transformation in dismiss, Using CGAffineTransformIdentity but still its not working properly.

 - (void) showWithAnimation
    {
        float mheight = customView.frame.size.height;

        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

        if(!keyWindow)
        {
            NSArray *windows = [UIApplication sharedApplication].windows;

            if(windows.count > 0)
                keyWindow = [windows lastObject];

            keyWindow = [windows objectAtIndex:0];
        }

        UIView *containerView = [[keyWindow subviews] objectAtIndex:0];

        CGRect frame = customView.frame;
        frame.origin.y = -mheight;
        customView.frame = frame;

        NSLog(@"customView - %@", customView);

        [containerView addSubview:customView];

        customView.alpha = 0;
        customView.transform = CGAffineTransformMakeScale(0.01, 0.01);
        customView.center = CGPointMake((ScreenBounds().size.width / 2) - 24, ScreenBounds().size.height / 2);

        NSLog(@"customView After applying transfrom - %@", customView);


        [UIView animateWithDuration:0.3
                              delay:0.0
                            options:UIViewAnimationCurveEaseInOut
                         animations:^{
                             customView.transform = CGAffineTransformMakeScale(1, 1);
                                customView.alpha = 1;
                         }
                         completion:^(BOOL finished){

                             self.isVisible = YES;
                             NSLog(@"customView Displayed .....");
                             NSLog(@"customView - %@", customView);
        }];
    }

    - (void)dismissWithAnimation
    {
        [UIView animateWithDuration:0.3
                              delay:0.0
                            options:UIViewAnimationCurveEaseInOut
                         animations:^{
                             customView.transform = CGAffineTransformMakeScale(0.01, 0.01);
                             customView.alpha = 0;
                         }
                         completion:^(BOOL finished)
                         {
                             self.isVisible = NO;

**//Edit: I tried applying CGAffineTransformIdentity at here**

                             [customView removeFromSuperview];
                         }];
    }

below are the logs,

--> 1st time logs

customView -> <UIView: 0x205a1e50; frame = (24 -150; 272 150); layer = <CALayer: 0x205a1eb0>>

customView After applying transfrom - <UIView: 0x205a1e50; frame = (134.64 283.25; 2.72 1.5); transform = [0.01, 0, 0, 0.01, 0, 0]; alpha = 0; layer = <CALayer: 0x205a1eb0>>

customView Displayed .....
customView - <UIView: 0x205a1e50; frame = (0 209; 272 150); layer = <CALayer: 0x205a1eb0>>


--> 2nd time logs

customView -> <UIView: 0x205a1e50; frame = (24 -204; 272 204); transform = [0.01, 0, 0, 0.01, 0, 0]; alpha = 0; animations = { transform=<CABasicAnimation: 0x2072c620>; opacity=<CABasicAnimation: 0x2072df20>; }; layer = <CALayer: 0x205a1eb0>>

customView After applying transfrom - <UIView: 0x205a1e50; frame = (3.03984e-06 182; 272 204); transform = [0.01, 0, 0, 0.01, 0, 0]; alpha = 0; animations = { transform=<CABasicAnimation: 0x2072c620>; opacity=<CABasicAnimation: 
0x2072df20>; }; layer = <CALayer: 0x205a1eb0>>

customView Displayed .....

customView - <UIView: 0x205a1e50; frame = (-13464 -9916; 27200 20400); layer = <CALayer: 0x205a1eb0>>

What is going worng at here.

JiteshW
  • 2,195
  • 4
  • 32
  • 61

2 Answers2

1

I always check if animation is finished... and also reset the animation before it starts... something like this...

  // skipping some of ur code... 

    customView.transform = CGAffineTransformMakeScale(0.01, 0.01);
        [UIView animateWithDuration:0.3
                              delay:0.0
                            options:UIViewAnimationCurveEaseInOut
                         animations:^{
                             customView.transform = CGAffineTransformMakeScale(1, 1);
                                customView.alpha = 1;
                         }
                         completion:^(BOOL finished){
                         if (finished)
                         {

                             customView.transform = CGAffineTransformMakeScale(1, 1);
                                customView.alpha = 1;

                             self.isVisible = YES;
                             NSLog(@"customView Displayed .....");
                             NSLog(@"customView - %@", customView);
                         }
        }];

//dismiss With animation

- (void)dismissWithAnimation
{
    customView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    [UIView animateWithDuration:0.3
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         customView.transform = CGAffineTransformMakeScale(0.01, 0.01);
                         customView.alpha = 0;
                     }
                     completion:^(BOOL finished) {
                     if (finished)
                     {
                         self.isVisible = NO;
                         customView.transform = CGAffineTransformMakeScale(0.01, 0.01);
                         customView.alpha = 0;
                        [customView removeFromSuperview];
                    }

                     }];
}

// EDIT - Try this.

customView.alpha = 0;

customView.transform = CGAffineTransformIdentity; //Reset the transformation...
customView.transform = CGAffineTransformMakeScale(0.01, 0.01);

customView.center = CGPointMake((ScreenBounds().size.width / 2) - 24, ScreenBounds().size.height / 2);
chuthan20
  • 5,389
  • 1
  • 17
  • 21
  • Can you tell the difference in applying the transfromation on same view in animate & finished block. I tried but my problem still persist. – JiteshW Apr 23 '13 at 05:58
  • the completion block passes u a variable called finished... animation might've completed even without fully finishing the proper animation you wanted.. i.e another view came on top of the current animating view.. then check finished == YES then assign what is the final state when the animation is supposedly finished... so when animated view is back visible... it will appear where it is supposed to. – chuthan20 Apr 23 '13 at 14:46
  • Check out the edit I made to the post... and Reset the transformation by setting it to identity matrix inside showWithAnimation method. – chuthan20 Apr 23 '13 at 15:03
0

Use following function for adding ........

-(void) addSubViewWithPopUpAnimation:(UIView *)subView toView:(UIView *)containerView
{
    [subView setTransform:CGAffineTransformMakeScale(0.1, 0.1)];

    [subView setAlpha:0.0f];

    [containerView addSubview:subView];

    [UIView animateWithDuration:0.3 animations:^
     {
         [subView setTransform:CGAffineTransformMakeScale(1.15, 1.15)];
     }
                     completion:^(BOOL finished)
     {
         if(finished)
         {
             [UIView animateWithDuration:0.15 animations:^
              {
                  [subView setTransform:CGAffineTransformMakeScale(1.0, 1.0)];
                  [subView setAlpha:1.0f];
              }];
         }

     }];
}

And Use following function for removing ........

-(void) removeSubViewWithPopUpAnimation:(UIView *)subView
{
    [subView setAlpha:1.0f];

    [subView setTransform:CGAffineTransformMakeScale(1.0, 1.0)];

    [UIView animateWithDuration:0.3 animations:^
     {
         [subView setTransform:CGAffineTransformMakeScale(0.01, 0.01)];
     }
                     completion:^(BOOL finished)
     {
         if(finished)
         {
             if([subView superview])
             {
                 [subView removeFromSuperview];
                 [subView setAlpha:0.0f];
             } 
         }
     }];


}

this will definitely help you ...........

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76