1

我有两种方法,setupAnimationstartAnimating。我正在使用 UIDynamics,特别是 4 个单独UIDynamicAnimators的,4 个单独的,UIGravityBehaviors每个单独包含一个项目(因为每个项目的重力方向不同),另外一个UIDynamicAnimator包含我的所有项目和一个UICollisionBehavior.

这是我的代码:

这是我的viewWillAppear方法。我把这两种动画方法放在这里是为了尝试使用更新的横向边界,但它仍然不起作用。

-(void)viewWillAppear:(BOOL)animated
{

    [self displayNextQuestion];
    self.questionLabel.text = question[@"Question"];
    [self setupAnimation];

    [self startAnimating];


    self.bounceAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.collisions = [[UICollisionBehavior alloc]initWithItems:self.answers];
    self.collisions.translatesReferenceBoundsIntoBoundary = YES;
    [self.bounceAnimator addBehavior:self.collisions];






    NSLog(@"VIEW WILL APEAR");
    if(self.bounceAnimator)
        NSLog(@"Animator exists");
    else
        NSLog(@"Animator doesnt exist");
    if([self.bounceAnimator.behaviors containsObject:self.collisions])
        NSLog(@"Collisions is in Animtor");
    else
        NSLog(@"Collission is NOT in animator");
}

我的viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupQuestions];
    [self performSelectorInBackground:@selector(getNextQuestion) withObject:self];
    [self addToSubviews];
}

这是我的两种动画方法:

setupAnimation

-(void)setupAnimation
{
    self.animator1 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.animator2 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.animator3 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    self.animator4 = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];


    self.gravity1 = [[UIGravityBehavior alloc]init];
    self.gravity2 = [[UIGravityBehavior alloc]init];
    self.gravity3 = [[UIGravityBehavior alloc]init];
    self.gravity4 = [[UIGravityBehavior alloc]init];

    [self.animator1 addBehavior:self.gravity1];
    [self.animator2 addBehavior:self.gravity2];
    [self.animator3 addBehavior:self.gravity3];
    [self.animator4 addBehavior:self.gravity4];



    [self.gravity1 setGravityDirection:CGVectorMake(0.0, 0.05)];

    //G1

 [self.gravity2 setGravityDirection:CGVectorMake(0.0, -0.05)];

    //G2
 [self.gravity3 setGravityDirection:CGVectorMake(0.05, 0.0)];

    //G3

  [self.gravity4 setGravityDirection:CGVectorMake(-0.05, 0.0)];

    //G4






}

startAnimating

-(void)startAnimating
{
    [self.gravity1 addItem:self.answers[0]];
    [self.gravity2 addItem:self.answers[1]];
    [self.gravity3 addItem:self.answers[2]];
    [self.gravity4 addItem:self.answers[3]];
    [self.collisions addItem:self.answers[0]];
    [self.collisions addItem:self.answers[1]];
    [self.collisions addItem:self.answers[2]];
    [self.collisions addItem:self.answers[3]];

}

我遇到了有关横向模式和视图边界的问题。

基本上,项目正确地出现在屏幕上,并且重力行为正确地应用于它们。但是,当它们撞到视图“墙”或边界时不会发生碰撞。我在这个视图控制器上也有一个模态展开 segue。我有一件重力向上的物品。当我在项目向上移动时点击按钮启动 segue时,我看到与视图边界发生冲突。

我怀疑这与视图控制器生命周期有关,并且未设置视图边界viewDidLoad,但我不完全确定如何修复它。请帮忙!

编辑:这是 NSLogs 显示的内容:

2014-08-16 19:02:37.504 FermiGame[9709:60b] VIEW DID LOAD
2014-08-16 19:02:37.504 FermiGame[9709:60b] Animator doesnt exist
2014-08-16 19:02:37.504 FermiGame[9709:60b] Collission is NOT in animator
2014-08-16 19:02:37.506 FermiGame[9709:60b] VIEW WILL APEAR
2014-08-16 19:02:37.506 FermiGame[9709:60b] Animator exists
2014-08-16 19:02:37.507 FermiGame[9709:60b] Collisions is in Animtor
2014-08-16 19:02:38.024 FermiGame[9709:60b] VIEW DID APPEAR
2014-08-16 19:02:38.025 FermiGame[9709:60b] Animator exists
2014-08-16 19:02:38.026 FermiGame[9709:60b] Collisions is in Animtor
4

0 回答 0