当需要为UIView
添加动画,而动画是多个的时候,就会出现completion block
嵌套,如下:
}];
}];
}];
}];
}];
在iOS7中,有一个方法可以优雅地解决以上问题:
animateKeyFramesWithDuration: delay: options: animations: completion:
在这个completion block
中,我们可以用一下方法添加另一个动画效果:
addKeyframeWithRelativeStartTime: relativeDuration: animations:
addKeyframe...
的这个方法就可以避免
在一个completion block
中创建一个新的动画.
以下的动画实现了UIView
上下来回转动:
[UIView animateKeyframesWithDuration:5.0 delay:0.0 options:0 animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
self.verticalPosition.constant = 200.0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.25 animations:^{
self.verticalPosition.constant = 50.0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.125 animations:^{
self.verticalPosition.constant = 200.0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.875 relativeDuration:0.0625 animations:^{
self.verticalPosition.constant = 50.0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.9375 relativeDuration:0.03125 animations:^{
self.verticalPosition.constant = 200.0;
[self.view layoutIfNeeded];
}];
[UIView addKeyframeWithRelativeStartTime:0.96875 relativeDuration:0.015625 animations:^{
self.verticalPosition.constant = 50.0;
[self.view layoutIfNeeded];
}];
} completion:nil];