0

我正在以编程方式将 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];

}

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

我会非常感谢你的帮助

4

1 回答 1

1

请在下面找到工作代码:

PS 我冒昧地更改了一些实现,您可以根据自己的喜好更改回来。

-(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];
}
于 2014-03-14T10:55:03.600 回答