animateWithDuration无法正常工作

时间:2013-08-15 22:40:39

标签: iphone objective-c uiview uikit

我已经编写了一些代码,我希望这些代码可以随机地将一个块旋转到屏幕上的视图中,暂停,然后将其旋转。该动作应以半随机方式重复。这种方式的作用在于它显示了块,旋转,然后在一段时间后再次显示自己:

  • 它不旋转(它只旋转)
  • 永远不会改变立场

我在这段代码中遗漏了哪些重要内容?

-(void)randomlyShowBlock{
    int randomTime = 1 + arc4random() % (3 - 1);

    [RandomBlock setNeedsDisplay];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, randomTime * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){

        int randomX = 10 + arc4random() % ((int)self.view.frame.size.width - 10);
        int randomY = 10 + arc4random() % ((int)self.view.frame.size.height - 10);

        RandomBlock.frame = CGRectMake(randomX, randomY, RandomBlock.frame.size.width, RandomBlock.frame.size.height);

        [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationOptionCurveEaseInOut
                         animations:^(void) {
                             RandomBlock.transform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
                             RandomBlock.backgroundColor = [[UIColor alloc] initWithRed:50.0/255.0 green:124.0/255.0 blue:203.0/255.0 alpha:1.0];
                         }
                         completion:^(BOOL finished){
                             [UIView animateWithDuration:0.4 delay:1 options:UIViewAnimationOptionCurveEaseInOut
                                              animations:^(void) {
                                                  RandomBlock.transform = CGAffineTransformMakeRotation(DegreesToRadians(0));
                                                  RandomBlock.backgroundColor = [[UIColor alloc] initWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:0];
                                              }
                                              completion:^(BOOL finished){
                                                  NSLog(@"finished animation");
                                                  //model

                                                  [self randomlyShowBlock];
                                              }];
                         }];
    });
}

1 个答案:

答案 0 :(得分:0)

你没有改变动画中的帧,所以它不会移动就不足为奇了。

至于旋转,我的猜测是变换最初不是一个身份变换,而是一个零变换,iOS不知道如何从无到有动画。尝试在动画块之前将其设置为CGAffineTransformIdentity