gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:43:37 24 4
gpt4 key购买 nike

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

}

最佳答案

将此代码用于 View 的摇动动画

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

}

关于ios - 在 ios 中为标签制作抖动效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202770/

24 4 0