iphone – 如何使“摇动”动画
发布时间:2020-12-15 01:57:03 所属栏目:百科 来源:网络整理
导读:在我的xcode项目中,我有一个UIImage。我想让它摇摆(动画)从左到右一点点。我写了这段代码,但是它不起作用。 -(void)errorShake{ [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.0]; [lockImage setTransform:CGAffineTransfo
在我的xcode项目中,我有一个UIImage。我想让它摇摆(动画)从左到右一点点。我写了这段代码,但是它不起作用。
-(void)errorShake { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:1.0]; [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(-20 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(-15 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(-10 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(-5 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(20 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(15 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(10 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(5 * M_PI / 180.0)]; [lockImage setTransform:CGAffineTransformMakeRotation(0 * M_PI / 180.0)]; [UIView commitAnimations]; } 请你告诉我,我怎么能让我的图像震动? 解决方法
这是我用于这个效果的代码。
-(void)shakeView { CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"position"]; [shake setDuration:0.1]; [shake setRepeatCount:2]; [shake setAutoreverses:YES]; [shake setFromValue:[NSValue valueWithCGPoint: CGPointMake(lockImage.center.x - 5,lockImage.center.y)]]; [shake setToValue:[NSValue valueWithCGPoint: CGPointMake(lockImage.center.x + 5,lockImage.center.y)]]; [lockImage.layer addAnimation:shake forKey:@"position"]; } 正如Jere所说。确保包含导入: #import <QuartzCore/QuartzCore.h> 您也可以将其添加为UIView中的类别。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |