Ali
Ali

Reputation: 11560

[UIView animateWithDuration animation reseting after changing label text

Here is strange thing, I am moving UIView position by following code , it was working perfectly in ios7 but in ios8 animation is resetting animation even I am chaning Text of Any label in controller, This really very strange for me

if (!self.setTouch) {
    [UIView animateWithDuration:kViewAnimateWithDuration
    delay:kViewDelay
    options:UIViewAnimationOptionCurveEaseInOut |
    UIViewAnimationOptionAllowUserInteraction
    animations:^{
        //Move frame or transform view
        [self.sliderView setFrame:CGRectMake(self.sliderView.frame.origin.x,-205,self.sliderView.frame.size.width,249)];
        self.setTouch = YES;
        // [self.containerView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y +171, width_b, height_b -171)];
    } completion:^(BOOL finished) {
        self.setTouch = YES;
    }];
} else {
    [UIView animateWithDuration:kViewAnimateWithDuration
    delay:kViewDelay
    options:UIViewAnimationOptionCurveEaseInOut |
    UIViewAnimationOptionAllowUserInteraction
    animations:^{
        [self.sliderView setFrame:CGRectMake(self.sliderView.frame.origin.x,0.0f,self.sliderView.frame.size.width,249.0)];
        self.setTouch = NO;
    } completion:^(BOOL finished) {
        self.setTouch = NO;
    }];
}

Here is code which changes Label Text:

NSTimer *timer= [NSTimer scheduledTimerWithTimeInterval:5.0 target:self
    selector:@selector(change_announcement:) userInfo:nil repeats:YES];

Here is the actual Method:

(IBAction)change_announcement:(id)sender {
    if ([_announcements count] == 0) {
        return;
    }

    AnnouncementDAO *ob = [_announcements objectAtIndex:_index];

    if(_isArabic){
        // [_label_announcementEn setHidden:YES];
        //[_label_announcementAr setHidden:NO];
        [_label_announcementAr setText:ob.announcementArabicTitle];
    } else{
        //  [_label_announcementAr setHidden:YES];
        // [_label_announcementEn setHidden:NO];
        [_label_announcementEn setText:ob.announcementEngTitle];
    }

    if (_index >= [_announcements count]-1) {
        // NSLog(@"Make index 0");
        _index = -1;
    }
    _index ++;
}

When I NSLog sliderView constraints I see following output:

"<NSIBPrototypingLayoutConstraint:0x7b048740 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIView:0x7b04b6a0](LTR)   (Names: '|':UIView:0x7b04b710 )>",
"<NSIBPrototypingLayoutConstraint:0x7b048710 'IB auto generated at build time for view with fixed frame' V:|-(0)-[UIView:0x7b04b6a0]   (Names: '|':UIView:0x7b04b710 )>",
"<NSIBPrototypingLayoutConstraint:0x7b0486e0 'IB auto generated at build time for view with fixed frame' H:[UIView:0x7b04b6a0(768)]>",
"<NSIBPrototypingLayoutConstraint:0x7b0486b0 'IB auto generated at build time for view with fixed frame' V:[UIView:0x7b04b6a0(25)]>",
"<NSIBPrototypingLayoutConstraint:0x7b048680 'IB auto generated at build time for view with fixed frame' H:|-(348)-[UIButton:0x7b0e72b0](LTR)   (Names: '|':UIView:0x7b04b710 )>",
"<NSIBPrototypingLayoutConstraint:0x7b048650 'IB auto generated at build time for view with fixed frame' V:|-(206)-[UIButton:0x7b0e72b0]   (Names: '|':UIView:0x7b04b710 )>",
"<NSIBPrototypingLayoutConstraint:0x7b048620 'IB auto generated at build time for view with fixed frame' H:[UIButton:0x7b0e72b0(73)]>",
"<NSIBPrototypingLayoutConstraint:0x7b0485f0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7b0e72b0(22)]>",
"<NSIBPrototypingLayoutConstraint:0x7b0485c0 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIView:0x7b04d690](LTR)   (Names: '|':UIView:0x7b04b710 )>",
"<NSIBPrototypingLayoutConstraint:0x7b048590 'IB auto generated at build time for view with fixed frame' V:|-(25)-[UIView:0x7b04d690]   (Names: '|':UIView:0x7b04b710 )>",
"<NSIBPrototypingLayoutConstraint:0x7b048560 'IB auto generated at build time for view with fixed frame' H:[UIView:0x7b04d690(768)]>",
"<NSIBPrototypingLayoutConstraint:0x7b048530 'IB auto generated at build time for view with fixed frame' V:[UIView:0x7b04d690(181)]>"

Upvotes: 0

Views: 606

Answers (1)

Fogmeister
Fogmeister

Reputation: 77631

First off.. change your code to this...

[UIView animateWithDuration:kViewAnimateWithDuration
                      delay:kViewDelay
                    options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                 animations:^{
                     CGRect sliderFrame = self.sliderView.frame;

                     if (!self.setTouch) {
                         //Move frame or transform view
                         self.sliderView.frame = CGRectMake(CGRectGetMinX(sliderFrame), -205, CGRectGetWidth(sliderFrame), 249);
                         self.setTouch = YES;
                         //[self.containerView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y +171, width_b, height_b -171)];
                     } else {
                         self.sliderView.frame = CGRectMake(CGRectGetMinX(sliderFrame), 0.0, CGRectGetWidth(sliderFrame), 249);
                         self.setTouch = NO;
                     }
                 }
                 completion:nil];

No need to make it more complicated than it needs to be.

Second... are you using AutoLayout?

EDIT

You ARE using AutoLayout. This is why it isn't working. You can't change the frame of a view directly when using AutoLayout.

In order to animate a view (or just move it at all) you have to change the constraints around that view. You can't touch the frame or center. It just doesn't work.

You need to get a reference to the constraint that you want to change and use that to move the slider view.

I'd recommend looking at Ray Wenderlich's site. There are some awesome AutoLayout tutorials that cover animation with AutoLayout.

Upvotes: 1

Related Questions