Question

the label is not animating  . . . .  



  UILabel * m_pLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 100, 40)];

  [m_pLabel setBackgroundColor:[UIColor redColor]];

  [self.view addSubview:m_pLabel];


  CABasicAnimation * m_pAnimationObj = [CABasicAnimation animationWithKeyPath:@"key"];

  [m_pAnimationObj setFromValue:[NSValue valueWithCGRect:CGRectMake(100, 10, 100, 40)]];

  [m_pAnimationObj setToValue:[NSValue valueWithCGRect:CGRectMake(100, 500, 100, 40)]];

  m_pAnimationObj.duration = 5.0;

  m_pAnimationObj.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

  [m_pLabel.layer addAnimation:m_pAnimationObj forKey:@"key"];
Was it helpful?

Solution

Try this..hope this will help you.

 UILabel * m_pLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, 100, 40)];

    [m_pLabel setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:m_pLabel];


    CABasicAnimation * m_pAnimationObj = [CABasicAnimation animationWithKeyPath:@"transform.translation"];

    [m_pAnimationObj setFromValue:[NSValue valueWithCGRect:CGRectMake(100, 10, 100, 40)]];

    [m_pAnimationObj setToValue:[NSValue valueWithCGRect:CGRectMake(100, 500, 100, 40)]];

    m_pAnimationObj.duration = 1.0;

    m_pAnimationObj.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

    [m_pLabel.layer addAnimation:m_pAnimationObj forKey:@"animations"];

OTHER TIPS

there are a lot of tutorial on How to Animate UIView : How To Use UIView Animation Tutorial also here is an example which might be helpful

//UIViewAnimationOptionAutoreverse option, try the code below:

UIView * testView = [[UIView alloc] initWithFrame:CGRectMake(20.0f, 100.0f, 300.0f,    200.0f)];
[testView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:testView];

[UIView animateWithDuration:0.3f
                  delay:0.0f
                options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
             animations:^{
                 [testView setFrame:CGRectMake(0.0f, 100.0f, 300.0f, 200.0f)];
             }
             completion:nil];

[testView release];

Currently you're asking the animation to work on the @"key" property when it should logically be the @"frame" property. But, that won't work on a layer anyway. The animation has to know what to apply the specified change to, but it also needs to be able to apply the change to a 'true' property. "frame" is a derived property.

To change the location you need to animate 'position' and to change the size you need to animate 'bounds' properties.

See this Apple tech note: http://developer.apple.com/library/mac/qa/qa1620/_index.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top