我想在我的应用程序中来回摆动图像,类似于当你按下它时iPhone图标的摆动.最好的方法是什么?
这是我第一次涉足不使用动画GIF的动画.我认为这个想法是来回轻微旋转图像以产生摆动效果.我已经看过使用CABasicAnimation和CAKeyframeAnimation.CABasicAnimation每次重复时都会创建一个抖动,因为它会跳转到from位置并且不会向内插回.CAKeyframeAnimation似乎是解决方案,除了我无法让它工作.我肯定错过了什么.这是我使用CAKeyframeAnimation的代码(不起作用):
NSString *keypath = @"wobbleImage";
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:keypath];
animation.duration = 1.0f;
animation.delegate = self;
animation.repeatCount = 5;
CGFloat wobbleAngle = 0.0872664626f;
NSValue *initial = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(0.0f, 0.0f, 0.0f, 1.0f)];
NSValue *middle = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(wobbleAngle, 0.0f, 0.0f, 1.0f)];
NSValue *final = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(-wobbleAngle, 0.0f, 0.0f, 1.0f)];
animation.values = [NSArray arrayWithObjects:initial, middle, final, nil];
[imageView.layer addAnimation:animation forKey:keypath];
Run Code Online (Sandbox Code Playgroud)
或者可能有一个我只是缺少的完全简单的解决方案.感谢任何指针.谢谢!
我正在编写一个iPad应用程序,它提供类似于Pages呈现方式的用户文档(作为实际文档的大图标).当用户点击编辑按钮时,我也想模仿摇晃行为.当您在iPhone和iPad上点按并按住图标时,这与图标在主屏幕上所做的抖动模式相同.
我在互联网上搜索并找到了一些算法,但它们只是让视图来回摇摆,这完全不像Apple的摇摆.似乎有一些随机性,因为每个图标都有点不同.
有没有人知道或者知道某些代码可以重新创建相同的微动模式(或者非常接近它的东西)?谢谢!!!
我制作了一个视频,演示了我想要完成的动画:就是这样.
请注意,视频说明了从侧面看到的操作.相机图标代表用户的POV.它基本上是通过比例变换完成的Z轴之间的平移模拟,同时沿X轴发生同步的两步3D旋转.
看起来和听起来很简单,对吗?错误.这是理想的代码,它不起作用:
[UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
view.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {}];
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
CATransform3D rotation = CATransform3DIdentity;
rotation.m34 = 1.0 / - 1800;
rotation = CATransform3DRotate(rotation, - 20 * M_PI / 180.0f, 1, 0, 0);
view.layer.transform = rotation;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
view.layer.transform = CATransform3DIdentity;
} completion:^(BOOL finished) {}];
}];
Run Code Online (Sandbox Code Playgroud)
事实证明,如果这样做,3D旋转将被完全忽略.我尝试过其他一些方法,但都失败了.
CGAffineTransform当然,如果出现需要改变其中一些的解决方案,我可以适应.
在此先感谢您的帮助.如果您需要澄清,请询问.
我想知道如何制作一个首先缩放到尺寸并旋转该尺寸而不是按原始尺寸旋转的 UIView 动画?
我尝试了很多方法,比如在完成一个UIView动画时包装UIView动画,使用UIView animateKeyframes等等。
但是,我无法完美地制作这样的动画,请给我提示或关键字来搜索,谢谢!