Everything's Beta

things I don't get to do at work :)

Archive for the ‘iphone’ tag

Jiggly goodness

without comments

This is barely worth the effort of making a post for. However, since it took me more than a couple doc. references to figure this out, I figured it will help someone else.

Situation:

I have a text field inside a custom UITableViewCell. When a user tries to edit the text when it is disabled, I want to make it obvious. So, I decided to have the whole cell jiggle back and forth prettily when this happens.

Fix:

Add jiggle animation to the pertinent layer when the cell is selected.

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (!textField.enabled && !first){
        [self jiggle:self.layer]; // send in textField.layer if you just want the text to jiggle
    }
    first = false;  //hack: setSelected is called when the table is loaded...
}
 
-(void) jiggle:(CALayer*) layer{
 
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setFromValue:[NSValue valueWithCGPoint:CGPointMake(layer.position.x-2, layer.position.y)]];
    [animation setToValue:[NSValue valueWithCGPoint:CGPointMake(layer.position.x+2, layer.position.y)]];
    [animation setDuration:0.1f];
    animation.repeatCount = 3;
    animation.autoreverses = YES;
 
    [layer addAnimation:animation forKey:@"positionAnimation"];
 
}

If you want to know more about CABasicAnimation, start here.

Written by srijak

October 26th, 2009 at 12:30 am

Posted in code

Tagged with ,