0

I'm trying to do a simple animate of a UIView and the problem is that the view doesn't expand/collapse as I want it to do, it only snaps to the end of the animation without moving there. Here is my code:

- (IBAction)policeReportedChanged:(id)sender {
    if (self.policeReportedSwitch.isOn) {
        [self.policeReportNumberColl setHidden:NO];

        [UIView animateWithDuration:1.0f
                         animations:^
         {
             [self.policeReportNumberColl setFrame:CGRectMake(0, 130, 320, 65)];
             [self.damageView setFrame:CGRectMake(0, 200, 320, 200)];
         }
                         completion:^(BOOL finished)
         {

         }
         ];



    } else {
        [UIView animateWithDuration:1.0f
                         animations:^
         {
             [self.policeReportNumberColl setFrame:CGRectMake(0, 0, 0, 0)];
             [self.damageView setFrame:CGRectMake(0, 135, 320, 200)];
         }
                         completion:^(BOOL finished)
         {
             [self.policeReportNumberColl setHidden:YES];
         }
         ];

    }

I have set the animation to reveal the UIView policeReportNumberColl when the switch is turned on and collapsed when it isn't. damageView is a UIView below that should move up or down to take the space where the revealed view are or not. Inside policeReportNumberColl are one UILabel and one UITextfield.

I have checked around a lot but can't find anything other than this should work.

EDIT:

I have played around a bit and figured out that it has something to to with which object is sending the action. It DOES work it I connect the animation to a Touch up inside on a UIButton but it does NOT work if I use on Value Changed on my UISwitch like I want to. See code below:

//Does work
- (IBAction) buttonClicked
{
    [UIView animateWithDuration:1.0f
                     animations:^
     {
         [self.damageView setFrame:CGRectMake(0, 0, 0, 0)];
     }
                     completion:^(BOOL finished)
     {
         NSLog(@"Klar");
     }
     ];

}

//Does NOT Work
- (IBAction)switchValueChanged {


    [UIView animateWithDuration:1.0f
                     animations:^
     {
         [self.damageView setFrame:CGRectMake(0, 0, 0, 0)];
     }
                     completion:^(BOOL finished)
     {
         NSLog(@"Klar");
     }
     ];
}

Does anyone have any idea how to do this on a UISwitch onValueChanged?

PaperThick
  • 2,749
  • 4
  • 24
  • 42
  • It's been quite a while that I haven't been into animations, but shouldn't you `commitAnimations` after setting it up? `[UIView commitAnimations];` – Neeku Nov 29 '13 at 09:41
  • @Neeku Hi, that is only when you use the old way of doing animations, see: http://stackoverflow.com/a/18907703/1436948 . I have tried doing it the other way as well – PaperThick Nov 29 '13 at 09:43
  • Ah, alright then. But do try different methods to see which works. This might be helpful: http://stackoverflow.com/questions/18184412/uiview-animation-not-working-to-resize-view There are other ways of implementing animation, too; with `QuartzCore`, etc. – Neeku Nov 29 '13 at 09:48
  • try to turn off autolayout – Neil Galiaskarov Nov 29 '13 at 10:32
  • @NeilGaliaskarov Hi, Thanks but it is off – PaperThick Nov 29 '13 at 11:37
  • What if you launch the animation from a timer ? does it change anything ? – Eino Gourdin Nov 29 '13 at 12:13
  • @EinoGourdin I tried with a delay in the animation with the same result. Is there something else you were thinking about? – PaperThick Nov 29 '13 at 12:20
  • Yes, can you try to call the animate method from a different function, which you launch with an NSTimer ? Maybe there's something with the onValueChanged that flushes the animation stack or something. Also, did you check if it's called on the main thread ? – Eino Gourdin Nov 29 '13 at 12:28
  • It turns out that it was a bug in the DCRoundSwitch I was using and someone had already pointed out the issue and gived a solution on GitHub: https://github.com/domesticcatsoftware/DCRoundSwitch/issues/30 Thanks everyone for replying! – PaperThick Nov 29 '13 at 12:32

0 Answers0