多次运行时,UIView动画不一致/滞后?

时间:2015-01-28 18:33:36

标签: objective-c uiview

我正在遇到UIView动画的一些问题,我不确定这是否有问题,或者我的代码是否存在根本性的错误(但我很确定没有'吨)。

我在应用程序中有4个UIView动画,但其中只有2个应该同时运行。其中两个只是一个接一个地运行,除非我打电话

,否则永远不会停止
[self.view1.layer removeAllAnimations];
[self.view2.layer removeAllAnimations];

这是动画:

- (void)slideDown1 {    
    NSLog(@"Anim 1 Started");
    // This sets the top of view1's frame equal to the bottom of self.view.frame
    self.view1TopConstraint.constant    = self.view.frame.size.height;
    // This sets the top of view2's frame equal to the top of self.view.frame
    self.view2TopConstraint.constant    = 0.0f;
    // If we didn't call this block, the changes set above would take effect "immediately"
    [UIView animateWithDuration:5.0f
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         // Calling this inside of an animation causes the changes above to be animated
                         [self.view layoutIfNeeded];
                     }
                     completion:^(BOOL finished) {
                         // This block will execute when the animations finish
                         NSLog(@"Ended 2 ");
                         // This moves self.view1 so that its bottom is aligned with the top of self.view
                         self.view1TopConstraint.constant   = -1.0f * self.view.frame.size.height;
                         // This causes the line above to take place immediately
                         [self.view layoutIfNeeded];
                         if (self.isVisible == YES && !CharacterIsAnimated) {
                             // If the screen is still visible, start the next animation                              
                             if (!isEnded) {
                                 [self slideDown2];                                 
                                 if (!isStarted) {                                    
                                     [self JumpAnimGoingDown];                                      
                                     isStarted = YES;
                                 }
                             }
                         } else if (self.isVisible) {                             
                             jumpWasCanceled = YES;                             
                             NSLog(@"Jump was canceled");
                         }
                     }
     ];
}
- (void)slideDown2 {
    treeIsMoving                        = YES;
    self.view1.hidden                   = NO;
    self.view1TopConstraint.constant    = 0.0f;
    self.view2TopConstraint.constant    = self.view.frame.size.height;
    [UIView animateWithDuration:5.0f
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         [self.view layoutIfNeeded];
                     }
                     completion:^(BOOL finished) {
                         self.view2TopConstraint.constant   = -1.0f * self.view.frame.size.height;
                         [self.view layoutIfNeeded];
                         if (self.isVisible == YES) {                             
                             if (!isEnded) {                                 
                                 [self slideDown1];                                 
                             }
                         }
                     }
     ];
}

另外两个动画以不断的间隔不断地相互切换(这是基于应用程序的玩家的触摸事件)每次我只删除“角色”的所有动画。图层然后运行UpAnim'然后它重置为' Down Anim'他们在这里:

 - (void)JumpAnimGoingUp {
    //This means that when the player trys to press the screen before this animation is over it will not run
    CharacterIsAnimated = YES;
    [UIView animateWithDuration:0.25f
                          delay:0
                        options:UIViewAnimationOptionCurveLinear
                     animations:^{
                         // Calling this inside of an animation causes the changes above to be animated                         
                         if (treeIsMoving == YES) {
                             self.character.frame = CGRectMake(self.view.frame.size.width / WidthDivision, self.character.center.y - (self.view.frame.size.height / 5), (self.view.frame.size.height * 4) / 45, self.view.frame.size.height / 9);
                         } else {
                             self.character.frame = CGRectMake(self.view.frame.size.width / WidthDivision, (self.character.center.y - (self.view.frame.size.height / 18)) - (self.view.frame.size.height / 5), (self.view.frame.size.height * 4) / 45, self.view.frame.size.height / 9);
                         }
                         [self.view layoutIfNeeded];                         
                     }
                     completion:^(BOOL finished) {
                         // This block will execute when the animations finish
                         Score = Score + 1;
                         self.ScoreText.text = [NSString stringWithFormat:@"%i", Score];                         
                         CharacterIsAnimated = NO;
                         if (isStarted) {
                             [self JumpAnimGoingDown];
                             if (jumpWasCanceled) {
                                 [self slideDown2];
                                 jumpWasCanceled = NO;
                                 isStarted = YES;
                             }
                         }
                         [self.view layoutIfNeeded];
                     }
     ];    
}
- (void)JumpAnimGoingDown {
    NSLog(@"IsJump");
    [UIView animateWithDuration:5.0f
                          delay:0
                        options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveLinear)
                     animations:^{
                         self.character.frame = CGRectMake(self.view.frame.size.width / WidthDivision, (self.character.center.y - (self.view.frame.size.height / 18)) + self.view.frame.size.height, (self.view.frame.size.height * 4) / 45, self.view.frame.size.height / 9);
                         [self.view layoutIfNeeded];
                     }
                     completion:^(BOOL finished) {
                         self.character.frame = CGRectMake(self.view.frame.size.width / WidthDivision, CharacterStopHeight - (self.view.frame.size.height / 18) + (self.view.frame.size.height / 58), (self.view.frame.size.height * 4) / 45, self.view.frame.size.height / 9);
                         if (isEnded) {                             
                             self.character.hidden = YES;                             
                         }                         
                         [self.view layoutIfNeeded];
                     }
     ];
}

据我所知,一切都应该正确运行,但经过几次切换两个Up and Down动画后,由两个Up and Down动画运行的对象开始以与速度不一致的速度移动应该移动(这只是在' Down'动画中)。由于我用于每个视图的图像,这只是少量但很快变得清晰,但事实上,向上和向下动画上的对象应该以与用动画制作的两个对象完全相同的速度移动。 '幻灯片'动画我第一次向你展示。

如果代码中有明显错误的地方请说,除此之外,我可以随时了解如何阻止这个有点奇怪的错误

由于

0 个答案:

没有答案