iphone - 将简单动画转换为基于 block 的动画

标签 iphone animation

我有一个动画,可以沿 x 轴稍微反弹 View 。问题是使用旧的动画机制,并且正如文档所述,建议 iOS4+ 使用基于 block 的动画。我想知道如何将这个简单的动画变成基于 block 的动画:

[UIView beginAnimations:@"bounce" context:nil];
[UIView setAnimationRepeatCount:2];
[UIView setAnimationRepeatAutoreverses:YES];
self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
[UIView commitAnimations];

此外,我希望 View 在动画结束后返回到其起始位置,上面的方法会弹起 View 并将其 x 值增加 10 像素。

感谢您的帮助。

最佳答案

基于 block 的动画的格式是这样的:

[UIView animateWithDuration:<#(NSTimeInterval)#>
                      delay:<#(NSTimeInterval)#>
                    options:<#(UIViewAnimationOptions)#>
                 animations:<#^(void)animations#>
                 completion:<#^(BOOL finished)completion#>];

下面的代码将 View 弹起两次返回到原始位置也许存在更好的方法:

[UIView animateWithDuration:0.2f
                      delay:0.0f
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
                 }
                 completion:^(BOOL finished) {
                     self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y);
                     [UIView animateWithDuration:0.2f
                                           delay:0.0f
                                         options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut
                                      animations:^{
                                          self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
                                      }
                                      completion:^(BOOL finished) {
                                          self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y);
                                      }];
                 }];

和这个类似,但是返回原点不太顺利

[UIView animateWithDuration:0.2f
                      delay:0.0f
                    options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     self.view.center = CGPointMake(self.view.center.x + 10, self.view.center.y);
                     self.view.center = CGPointMake(self.view.center.x - 10, self.view.center.y);
                 }
                 completion:nil];

关于iphone - 将简单动画转换为基于 block 的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8463984/

相关文章:

iphone - 我如何连接到名为 "A"的受密码保护的 wifi

ios - 无法在 spritekit [SWIFT] 中为 GameScene 设置正确的大小

android - 你如何在 Android 的 MapView 上放置一个图钉?

已更新的适配器内部 View 的 Android ListView 动画

javascript - react 姿势延迟姿势组高度转换,直到 children 之后

android - 如何将图标添加到 TextFormField 下面的 errorText?

iphone - 创建应用程序屏幕截图的动画?

ios - 如何设置 iOS 应用程序使用 USB 音频输入和输出到内部扬声器

javascript - 一侧颜色为 'lineargradient' 的 Canvas 动画较暗并且无法正确调整大小?

angular - 如何在 false *ngIf 删除元素之前捕获它?