【问题标题】:Make shake effect for label in ios?在ios中为标签制作震动效果?
【发布时间】:2013-08-13 07:09:15
【问题描述】:

我正在处理标签的抖动效果,我在 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];

}

【问题讨论】:

  • 定义“不工作”。在你的didSelectRowAtIndexPath 方法中你从哪里得到cell
  • @maddy 找不到你???
  • 我已经创建了自定义单元格并只是对其应用了动画
  • 在您的didSelectRowAtIndexPath 方法中,您引用了一个名为cell 的变量。这个变量是从哪里来的?请澄清您所说的“不工作”是什么意思?您是否收到编译错误、运行时错误,或者只是没有按预期工作?
  • 它工作正常但我想在选择一行时摇晃怎么办??

标签: ios objective-c uitableview uilabel shake


【解决方案1】:

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

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

}

【讨论】:

  • 它工作正常但我想在选择一行时摇晃怎么办??
  • 您访问选定的单元格并使用像这样的标签访问单元格中的 uilabel UITableViewCell cell = [theTableView cellForRowAtIndexPath:theIndexPath]; UILabel label=(UILabel*)[cell.contentView viewWithTag:2001];[self shakeAnimation:label];
  • 我创建了带有标签的自定义单元格,每一行都有一个标签
【解决方案2】:

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

1.试试这个功能:

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

在 didSelectRowAtIndexPath 函数中。

仅在 cellForRowAtIndexPath 中制作动画。

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

cell.tag = indexPath.row.

在 didSelectRowAtIndexPath 中:

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

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

【讨论】:

  • 自定义单元格的对象,然后在 cellForRowAtIndexPath 中写入 cell.tag = indexPath.row
  • 它有效,但如果我按下第一行意味着只有它在晃动,值得注意的是所有行都在晃动
  • 你能描述更多你在说什么吗?
【解决方案3】:

Finlay 我为您提供了解决方案,希望对您有所帮助。对于摇动特定标签,您可以添加UITapGestureRecognizer 以获取特定的UILabel 点击并将摇动动画设置为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];


        }
    }];
}

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

【讨论】:

  • 从 iOS 4.0 开始不鼓励使用这些动画方法。你真的应该使用基于块的动画方法。
  • 我只是想知道为什么没有人愿意像苹果推荐和鼓励 iOS4 及更高版本的开发者那样使用基于块的动画?
【解决方案4】:

使用这个功能 - (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中删除一个应用程序,并以适当的角度决定摇动

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多