我有不同的 UIView
,它们应该同时被激活。我的实际做法如下:
- (void)moveRectangles {
NSUInteger count = [self.dataSource numberOfRectangles];
for (NSUInteger i = 0; i < count; i++) {
[self snapToPositionWithIndex:i];
}
}
其中 snapToPositionWithIndex:
的实现方式如下。
- (void)snapToPositionWithIndex:(NSUInteger)index {
CGPoint point = [[self.positions objectAtIndex:index] CGPointValue];
UIView *rectangleView = [self.items objectAtIndex:index];
[UIView animateWithDuration:self.animationTimeInterval animations:^{
rectangleView.center = point;
} completion:nil];
}
如您所见,我为每个矩形创建了一个动画 block 。这是应该遵循的正确方法,还是有更简单、更高效的方法来实现这一目标?如果我有很多 View 要移动怎么办?
最佳答案
你的方法很好。您也可以使用单个动画 block 来编写它,其中 for 循环位于 block 内,但我认为这种方法并不比您的方法好。无论如何,系统都会为每个正在动画的对象创建一个单独的核心动画。
关于ios - 关于如何同时为不同的 UIView 设置动画的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28476647/