Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UIView scaling using CGAffineTransformScale

I'm learning to develop for iOS, and as part of education process I have created custom UIView which uses drawRect to draw it's contents (Core Graphics + UIBezierPath based). Everthing works as expected, view is dynamic and renders it's contents depending on it's width/height. Now I want to add dissolve animation which should look like scaling from 100% to 0%. I have written this code to do it:

[UIView animateWithDuration:1 delay:0
                options: UIViewAnimationOptionBeginFromCurrentState
             animations: ^{
     for (CardView *cardView in self.cardsViews) {
         cardView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.01, 0.01);

     }} completion:nil];

However, this code DOES NOT scales my view from 100% to 0%. It just makes cards to dissapear (at most). I have tried many ways of doing scaling, but only effect I have reached so far is zooming from 0% to 100%. This my view does perfectly, but not reverse scaling... Also, it DOES NOT scales up/down even if I try to apply non-animated transformation such as:

cardView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.5, 0.5);

I see that my view look changes A BIT, but not scales to 50%, that's for sure. When I try to apply exactly same animation logic to UILabel, it works perfectly! What have I missed developing custom UIView? Why scaling view may malfunction? Thanks a lot in advance!

UPDATE #1 This code makes my view exactly twice as big and than scales it back to original size:

[UIView animateWithDuration:5 delay:0
                    options: UIViewAnimationOptionBeginFromCurrentState
                 animations: ^{
         for (CardView *cardView in self.cardsViews) {
             cardView.transform = CGAffineTransformMakeScale(0.5, 0.5);
         }
    } completion:nil];
like image 552
Serzhas Avatar asked Feb 13 '23 15:02

Serzhas


2 Answers

It is simple 100% to 0% using CGAffineTransformScale:

cardView.view.transform =CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);

[UIView animateWithDuration:0.3/1.5 animations:^{
        cardView.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
    } completion:^(BOOL finished)
    {
              NSLog(@" Animation complet Block");
    }];
like image 80
Milan Vadgama Avatar answered Feb 27 '23 07:02

Milan Vadgama


What you can try is setting the contentMode of the view to UIViewContentModeRedraw and adding another animation option - UIViewAnimationOptionAllowAnimatedContent - in animateWithDuration method.

like image 24
Rafa de King Avatar answered Feb 27 '23 07:02

Rafa de King