0

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

  • 它不旋转(它只旋转)
  • 它永远不会改变位置

我从这段代码中遗漏了什么重要的东西?

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

1 回答 1

0

您没有更改动画中的帧,因此它没有移动也就不足为奇了。

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

于 2013-08-15T22:49:29.730 回答