在ios中为标签制作震动效果?
我正在使用标签的抖动效果在tableview中按单元格时调用了动画,但它不起作用。如果我在CellForRowatineXpath:(NSIndexPath*)indexPath中调用动画,则它起作用,但如果我在DidSelectRowatineXpath:(NSIndexPath*)indexPath中调用动画,则它不起作用。有人能帮我吗???提前谢谢在ios中为标签制作震动效果?,ios,objective-c,uitableview,uilabel,shake,Ios,Objective C,Uitableview,Uilabel,Shake,我正在使用标签的抖动效果在tableview中按单元格时调用了动画,但它不起作用。如果我在CellForRowatineXpath:(NSIndexPath*)indexPath中调用动画,则它起作用,但如果我在DidSelectRowatineXpath:(NSIndexPath*)indexPath中调用动画,则它不起作用。有人能帮我吗???提前谢谢 -(void)shakeAnimation:(UILabel*) label { const int reset = 5;
-(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];
}
-(void)shakeAnimation:(UILabel*)标签{
常数int reset=5;
常量int maxShakes=6;
//如果同时晃动两个控件,则将其作为变量而不是静态变量或类变量传递
静态int shakes=0;
静态int translate=重置;
[UIView animateWithDuration:0.09-(shakes*.01)//将每次抖动的持续时间从.09减少到.04
延迟:0.01f//edge等待延迟
选项:(枚举UIViewAnimationOptions)UIViewAnimationCurveEaseInOut
动画:^{label.transform=CGAffineTransformMakeTransform(translate,0);}
完成:^(布尔完成){
如果(震动<最大震动){
shakes++;
//油门下降运动
如果(翻译>0)
翻译——;
//转向
翻译*=-1;
[自抖动动画:标签];
}否则{
label.transform=CGAffineTransformity;
shakes=0;//准备好下一次了吗
translate=reset;//准备好下次使用
返回;
}
}];
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath
{
[self-shakeAnimation:cell.MyHomeMenuCountlabel];
}
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];
}
}];
}
它的工作原理如下:-
将此代码用于视图的抖动动画
-(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.尝试此功能:
NSArray *arrIndexPath = [NSArray arrayWithObject:indexPath];
[dropTable reloadRowsAtIndexPaths:arrIndexPath withRowAnimation:UITableViewRowAnimationFade];
在didSelectRowAtIndexPath函数中
仅在cellForRowAtIndexPath中制作动画
2.为CellForRowatineXpath中的单元格指定标记属性
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];
}
}
}
使用此功能
-(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];
}
}];
}定义“不工作”。在你的
didSelectRowAtIndexPath
方法中,你从哪里得到cell
呢?@maddy找不到你???我已经创建了自定义单元格,并在你的didSelectRowAtIndexPath
方法中应用了动画。你有一个名为cell
的变量引用。这个变量来自哪里?请澄清你所说的“不工作”是什么意思?您是否遇到编译错误、运行时错误,或者某些东西没有按预期工作?它工作正常,但我希望在选择行时抖动如何执行?从iOS 4.0开始,不鼓励使用这些动画方法。你真的应该改为使用基于块的动画方法。我只是想知道为什么没有人愿意像苹果推荐的那样使用基于块的动画,并鼓励iOS4及更高版本的开发人员?它工作正常,但我想知道当选择一行时如何做??你访问选定的单元格,并使用类似标签的方式访问单元格中的uilabel此UITableViewCell单元格=[表格视图单元格用于行索引路径:索引XPath];UILabel标签=(UILabel*)[cell.contentView视图带标签:2001];[自抖动动画:标签];我创建了带有标签的自定义单元格,每一行都会有一个自定义单元格的labelObject,然后在CellForRowatineXpath中写入cell.tag=indexPath.row。它可以工作,但如果我按第一行,则表示只有它在抖动,值得注意的是,所有行都在抖动。您能详细描述一下您在说什么吗?让我们来看看