Vasil Garov
Vasil Garov

Reputation: 4931

UIView animation completes immediately

I've been struggling with this for a long and I just can't find out what's wrong here. I actually do have an idea - I think that somehow the view I am adding and trying to animate is not yet included to the view hierarchy and that's why core animation isn't wasting cycles to animate. I did some other animations in the project successfully but I am running them in the ViewController's viewDidLoad method. Here is my code:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.opaque = NO;
        self.backgroundColor = [UIColor colorWithWhite: 1.0 alpha: 0.7];
        [self addTrack];
    }
    return self;
}

- (void)addTrack
{
    //init the track here
    UIView *track = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    track.backgroundColor = [UIColor whiteColor];
    [self addSubview: track];

    //transform the track for its initial scale
    float scaleFactorX = 0.01;
    track.layer.anchorPoint = CGPointMake(0, 0);
    track.layer.position = CGPointMake(0, 0);
    track.transform = CGAffineTransformMakeScale(scaleFactorX, 1.0);

    //animate the track here
    [UIView animateWithDuration:8 delay:0.3 options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         NSLog(@"animation is running.");
                         track.transform = CGAffineTransformIdentity;
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"animation finished.");
                     }];
}

So the result is that the "track" is scaling to its original size immediately. I also tried to call addTrack inside the didMoveToSuperview method so I can make sure that the containing view is added to the view hierarchy but with the same result. I tried other animations inside that class like adding alpha animation to the containing view but with no success again ..

Upvotes: 1

Views: 1995

Answers (2)

Preetam Jadakar
Preetam Jadakar

Reputation: 4561

Try calling your animations after loading the view so that you can see it.

-(void)viewDidAppear:(BOOL)animated
{
     [self addTrack];
}

Upvotes: 4

Sergey Grishchev
Sergey Grishchev

Reputation: 12051

You shouldn't have put animations in the init method to start with. Try calling them immediately after your view was actually displayed (added on a superview) and see what happens then.

Upvotes: 1

Related Questions