我正在以编程方式将 UIGravityBehavior 添加到子视图中,但并不总是有效。我在主视图中有 4 个视图,标签为 101,102,103,104,如图所示:
但由于某种原因,它似乎只适用于标签 103 和 104 的视图。这是我的代码:
-(void)viewsGoinDown
{
NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"self.tag >= 100"];
NSArray *resultArray = [[self.view subviews] filteredArrayUsingPredicate:tagPredicate];
for (NSUInteger i =0 ; i < resultArray.count; i++)
{
NSUInteger sleeping = 2;
UIView *mySubview = resultArray [i];
if (i == 0)
{
sleeping = 0;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(sleeping);
dispatch_async(dispatch_get_main_queue(), ^()
{
[self moveDownViews:mySubview];
});
});
}
}
-(void)moveDownViews:(UIView*)myView
{
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[myView]];
self.gravityBehavior.magnitude = 1;
[self.animator addBehavior:self.gravityBehavior];
}
我的问题是为什么只适用于这两个子视图?,我做错了什么?
我会非常感谢你的帮助