将UIGravityBehavior添加到子视图的iOS并不总是有效

时间:2014-03-13 19:32:47

标签: ios iphone uiview uidynamicbehavior uikit-dynamics

我正在以编程方式将UIGravityBehavior添加到子视图中但不总是有效。我在主视图中有4个视图,标签为101,102,103,104,如图所示:

enter image description here

但由于某种原因,它似乎仅适用于带有标签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];

}

我的问题是为什么只适用于这2个子视图?,我做错了什么?

我真的很感谢你的帮助

1 个答案:

答案 0 :(得分:1)

请找到以下工作代码:

P.S。我已经冒昧地改变了一些你可以根据自己的口味改变的实施方式。

-(void)viewsGoinDown
{
    NSPredicate *tagPredicate = [NSPredicate predicateWithFormat:@"self.tag >= 100"];
    NSArray *resultArray = [[self.view subviews] filteredArrayUsingPredicate:tagPredicate];
    NSUInteger sleeping = 0;
    for (NSUInteger i =0 ; i < resultArray.count; i++)
    {

        UIView *mySubview = resultArray [i];
        [self performSelector:@selector(moveDownViews:) withObject:mySubview afterDelay:sleeping];
        sleeping++;
    }

}

-(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];
}