Make shake effect for label in ios?
Asked Answered
N

4

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];

}
Nymphet answered 13/8, 2013 at 7:9 Comment(9)
Define "not working". And where are you getting cell in your didSelectRowAtIndexPath method?Strep
@maddy can't get you???Nymphet
i have created custom cell and just applying animation to itNymphet
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?Strep
it is working fine but i want to shake when a row is selected how to do it??Nymphet
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?Strep
no error nothing happening while am pressing the animation not applyingNymphet
let us continue this discussion in chatNymphet
add your custom cell code ?Quadruplet
L
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]; 
    } 
  } 
}
Little answered 13/8, 2013 at 7:32 Comment(4)
Object of your custom cell, then write cell.tag = indexPath.row in cellForRowAtIndexPathLittle
it works but if i press first row means only it is shaking and notable one all rows are shakingNymphet
Can you describe more what you are saying?Little
let us continue this discussion in chatNymphet
Q
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];

}
Quadruplet answered 13/8, 2013 at 7:27 Comment(3)
it is working fine but i want to shake when a row is selected how to do it??Nymphet
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];Quadruplet
i created custom cell with labels every row will have one labelNymphet
L
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]; 
    } 
  } 
}
Little answered 13/8, 2013 at 7:32 Comment(4)
Object of your custom cell, then write cell.tag = indexPath.row in cellForRowAtIndexPathLittle
it works but if i press first row means only it is shaking and notable one all rows are shakingNymphet
Can you describe more what you are saying?Little
let us continue this discussion in chatNymphet
A
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

Ambroid answered 13/8, 2013 at 7:27 Comment(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.Strep
I'm just wondering why nobody wants to use the block-based animation as the Apple recommends and encourages developers on iOS4 and above?Soubise
H
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];
    }
}];

}

Harday answered 13/8, 2013 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.