6

Am working with shake effect for label i called the animation while pressing cell in tableview but it is not working.if i call animation in cellForRowAtIndexPath:(NSIndexPath *)indexPath it is working but if i cal it in didSelectRowAtIndexPath:(NSIndexPath *)indexPath it is not working. could any one help me??? thanks in advance

   -(void)shakeAnimation:(UILabel*) label {
    const int reset = 5;
    const int maxShakes = 6;

    //pass these as variables instead of statics or class variables if shaking two controls simultaneously
    static int shakes = 0;
    static int translate = reset;

    [UIView animateWithDuration:0.09-(shakes*.01) // reduce duration every shake from .09 to .04
                          delay:0.01f//edge wait delay
                        options:(enum UIViewAnimationOptions) UIViewAnimationCurveEaseInOut
                     animations:^{label.transform = CGAffineTransformMakeTranslation(translate, 0);}
                     completion:^(BOOL finished){
                         if(shakes < maxShakes){
                             shakes++;

                             //throttle down movement
                             if (translate>0)
                                 translate--;

                             //change direction
                             translate*=-1;
                             [self shakeAnimation:label];
                         } else {
                             label.transform = CGAffineTransformIdentity;
                             shakes = 0;//ready for next time
                             translate = reset;//ready for next time
                             return;
                         }
                     }];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [self shakeAnimation:cell.MyHomeMenuCountlabel];

}
IamDev
  • 299
  • 4
  • 17
  • 2
    Define "not working". And where are you getting `cell` in your `didSelectRowAtIndexPath` method? – rmaddy Aug 13 '13 at 07:14
  • @maddy can't get you??? – IamDev Aug 13 '13 at 07:21
  • i have created custom cell and just applying animation to it – IamDev Aug 13 '13 at 07:23
  • 1
    In your `didSelectRowAtIndexPath` method you have a reference to a variable named `cell`. Where is this variable coming from? And please clarify what you mean by "not working"? Are you getting a compile error, a runtime error, or is something just not working as expected? – rmaddy Aug 13 '13 at 07:28
  • it is working fine but i want to shake when a row is selected how to do it?? – IamDev Aug 13 '13 at 07:51
  • Yes, I understand that. But you have not explained what happens when you try to shake the label when a row is selected. Tell us what happens? Are you getting a compile error, a runtime error, or is something just not working as expected? – rmaddy Aug 13 '13 at 07:53
  • no error nothing happening while am pressing the animation not applying – IamDev Aug 13 '13 at 08:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35331/discussion-between-vijay-07-and-rmaddy) – IamDev Aug 13 '13 at 08:24
  • add your custom cell code ? – NANNAV Aug 13 '13 at 09:23

4 Answers4

4

use this code for shake animation for views

-(void)shakeAnimation:(UILabel*) label
 {
        CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"];
        [shake setDuration:0.1];
        [shake setRepeatCount:5];
        [shake setAutoreverses:YES];
        [shake setFromValue:[NSValue valueWithCGPoint:
                             CGPointMake(label.center.x - 5,label.center.y)]];
        [shake setToValue:[NSValue valueWithCGPoint:
                           CGPointMake(label.center.x + 5, label.center.y)]];
        [label.layer addAnimation:shake forKey:@"position"];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell cell = [theTableView cellForRowAtIndexPath:theIndexPath]; 
UILabel label=(UILabel*)[cell.contentView viewWithTag:2001];
[self shakeAnimation:label];

}
NANNAV
  • 4,875
  • 4
  • 32
  • 50
  • it is working fine but i want to shake when a row is selected how to do it?? – IamDev Aug 13 '13 at 07:50
  • you access selected cell and access uilabel in cell using tag like this UITableViewCell *cell = [theTableView cellForRowAtIndexPath:theIndexPath]; UILabel* label=(UILabel*)[cell.contentView viewWithTag:2001];[self shakeAnimation:label]; – NANNAV Aug 13 '13 at 08:16
  • i created custom cell with labels every row will have one label – IamDev Aug 13 '13 at 08:33
3

There are 2 ways to do this :

1.Try this function :

NSArray *arrIndexPath = [NSArray arrayWithObject:indexPath];
[dropTable reloadRowsAtIndexPaths:arrIndexPath withRowAnimation:UITableViewRowAnimationFade];

in didSelectRowAtIndexPath function.

Make animation in cellForRowAtIndexPath only.

2.Assign a tag property to your cell in cellForRowAtIndexPath.

cell.tag = indexPath.row.

In didSelectRowAtIndexPath :

Then using subviews of table, get the cell.tag equal to indexPath.row and taking the reference of same cell and then do animation:

for (MyHomeViewCell *cell in [table subviews]) { // change name of table here 
  if ([cell isKindOfClass:[MyHomeViewCell class]]) { 
    if (cell.tag == indexPath.row) { 
        [self shakeAnimation:cell.MyHomeMenuCountlabel]; 
    } 
  } 
}
Samkit Jain
  • 2,523
  • 16
  • 33
2

Finlay i got you solution hope this may Helps you. For shake particular Label you can add UITapGestureRecognizer for getting Particular UILabel tap and set Shake animation into UITapGestureRecognizer's @selector method like Bellow example:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    UILabel *cellTitle=[[UILabel alloc]initWithFrame:CGRectMake(100, 7, 300, 30)];
    cellTitle.userInteractionEnabled = YES;
    cellTitle.tag = indexPath.section;
    [cellTitle setBackgroundColor:[UIColor clearColor]];
    [cellTitle setFont:[UIFont fontWithName:@"Helvetica" size:14]];
    [cellTitle setText:_objects[indexPath.row]];
    [cellTitle setTextColor:[UIColor blackColor]];
    [cell.contentView addSubview:cellTitle];


    UITapGestureRecognizer *labelTap = [[UITapGestureRecognizer alloc] init];
    [labelTap addTarget:self action:@selector(viewPatient:)];
    [labelTap setDelegate:nil];
    [labelTap setNumberOfTapsRequired:1];
    [cellTitle addGestureRecognizer:labelTap]; // cellTitle add here
    [labelTap release];
    return cell;
}



-(void)viewPatient:(id)sender
{

    UILabel *lObjLabel = (UILabel *)[sender view];

    CGFloat t = 2.0;
    CGAffineTransform translateRight  = CGAffineTransformTranslate(CGAffineTransformIdentity, t, 0.0);
    CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, -t, 0.0);

    lObjLabel.transform = translateLeft;

    [UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{

        [UIView setAnimationRepeatCount:10];
        lObjLabel.transform = translateRight;
    } completion:^(BOOL finished) {

        if (finished) {
            [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                lObjLabel.transform = CGAffineTransformIdentity;
            } completion:NULL];


        }
    }];
}

It code working like:-

enter image description here

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
  • 2
    The use of these animation methods is discouraged as of iOS 4.0. You really should be using the block-based animation methods instead. – rmaddy Aug 13 '13 at 07:29
  • 1
    I'm just wondering why nobody wants to use the block-based animation as the Apple recommends and encourages developers on iOS4 and above? – holex Aug 13 '13 at 07:33
0

use this function - (void)shakeView:(UIView *)viewToShake and write down any animation code you want in this function and call it from where you want to animate you item

and if you choose first animation then also use following code piece too this function is used to finish animation properly

#define RADIANS(degrees) ((degrees * M_PI) / 180.0)

- (void) wobbleEnded:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
UIView* item = (__bridge UIView *)context;
item.transform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(90.0));
}

and here are two type of animation chose whatever you want


1. shake animation like deleting a pp from ipad or iphone and decide shaking by proper angle

CGAffineTransform leftWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(88.0));
CGAffineTransform rightWobble = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(92.0));

viewToShake.transform = leftWobble;  // starting point

[UIView beginAnimations:@"wobble" context:(__bridge void *)(viewToShake)];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:2000];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(wobbleEnded:finished:context:)];

viewToShake.transform = rightWobble; // end here & auto-reverse

[UIView commitAnimations];

2.normal shake animation

CGFloat t = 2.0;
CGAffineTransform translateRight  = CGAffineTransformTranslate(CGAffineTransformIdentity, 85, 0.0);
CGAffineTransform translateLeft = CGAffineTransformTranslate(CGAffineTransformIdentity, 95, 0.0);

viewToShake.transform = translateLeft;

[UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{
    [UIView setAnimationRepeatCount:2.0];
    viewToShake.transform = translateRight;
} completion:^(BOOL finished) {
    if (finished) {
        [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            viewToShake.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 95, 0.0);
        } completion:NULL];
    }
}];

}

Anurag Soni
  • 1,077
  • 10
  • 14