6

我正在处理标签的抖动效果,我在 tableview 中按下单元格时调用了动画,但它不起作用。如果我在 cellForRowAtIndexPath:(NSIndexPath *)indexPath 中调用动画,它正在工作,但如果我在 didSelectRowAtIndexPath:(NSIndexPath *)indexPath 中调用它它不工作。任何人都可以帮助我吗???提前致谢

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

}
4

4 回答 4

4

将此代码用于视图的摇动动画

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

}
于 2013-08-13T07:27:47.830 回答
3

有两种方法可以做到这一点:

1.试试这个功能:

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

在 didSelectRowAtIndexPath 函数中。

仅在 cellForRowAtIndexPath 中制作动画。

2. 在 cellForRowAtIndexPath 中为您的单元格分配标签属性。

cell.tag = indexPath.row.

在 didSelectRowAtIndexPath :

然后使用表格的子视图,获取等于 indexPath.row 的 cell.tag 并获取相同单元格的引用,然后做动画:

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]; 
    } 
  } 
}
于 2013-08-13T07:32:38.027 回答
2

Finlay 我给你解决了希望这可以帮助你。对于摇动特定的标签,您可以添加UITapGestureRecognizer以获得特定的 UILabel 轻按并将 Shake 动画设置为像 Bellow 示例UITapGestureRecognizer的方法:-@selector

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


        }
    }];
}

它的代码工作方式如下: -

在此处输入图像描述

于 2013-08-13T07:27:43.957 回答
0

使用这个函数 - (void)shakeView:(UIView *)viewToShake 并在这个函数中写下你想要的任何动画代码,然后从你想要动画项目的地方调用它

如果你选择第一个动画,那么也使用下面的代码片段这个函数用于正确完成动画

#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));
}

这里有两种类型的动画选择任何你想要的


1. 摇动动画,比如从 ipad 或 iphone 中删除一个 pp,并以适当的角度决定摇动

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.正常抖动动画

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

}

于 2013-08-13T08:09:26.783 回答