Make shake effect for shortcut in ios?

I am working with a shake effect for a label, which I called animation, when I click a cell in a table, but it does not work. If I call the animation in cellForRowAtIndexPath: (NSIndexPath *) indexPath, it works, but if I refuse it didSelectRowAtIndexPath: (NSIndexPath *) indexPath does not work. Can someone help me ??? thank you 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]; } 
+6
source share
4 answers

There are two ways to do this:

1. Try this feature:

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

in the didSelectRowAtIndexPath function.

Make animation only in cellForRowAtIndexPath.

2.Assign the tag property for your cell in cellForRowAtIndexPath.

 cell.tag = indexPath.row. 

In the didSelectRowAtIndexPath file:

Then, using the subviews of the table, get cell.tag equal to indexPath.row and taking a link to the same cell, and then do the 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]; } } } 
+3
source

use this code for shaking animations 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]; } 
+4
source

Finlay, I got a solution that can help you. To shake a specific label, you can add a UITapGestureRecognizer to get a specific UILabel tap and set the Shake animation to a UITapGestureRecognizer @selector method, such as the 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]; } }]; } 

This code works like: -

enter image description here

+2
source

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

and if you select the first animation, and then use the next part of the code, this function is used to properly animate

 #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 types of animations chosen what you want


1. Shake the animation, for example, remove pp from the ipad or iphone and decide to shake it at the right 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 shaking 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]; } }]; 

}

0
source

Source: https://habr.com/ru/post/951600/


All Articles