在ios中为标签制作摇动效果?

时间:2013-08-13 07:09:16

标签: ios objective-c uitableview uilabel shake

我正在使用标签的摇动效果我在桌面视图中按下单元格时调用动画但是它不起作用。如果我在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 个答案:

答案 0 :(得分: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];

}

答案 1 :(得分:3)

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

1.试试这个功能:

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

在didSelectRowAtIndexPath函数中。

仅在cellForRowAtIndexPath中制作动画。

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

cell.tag = indexPath.row.

在didSelectRowAtIndexPath中:

然后使用table的子视图,获取等于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]; 
    } 
  } 
}

答案 2 :(得分:2)

Finlay我找到了解决方案希望这可能会帮助你。对于震动特定标签,您可以添加UITapGestureRecognizer以获取特定UILabel点击并将摇动动画设置为UITapGestureRecognizer的{​​{1}}方法,例如Bellow示例: -

@selector

代码的工作方式如下: -

enter image description here

答案 3 :(得分: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];
    }
}];

}