我有一个动画,可以沿 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/