【问题标题】:animateWithDuration not working correctlyanimateWithDuration 无法正常工作
【发布时间】:2013-08-18 06:07:40
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: iphone objective-c uiview uikit


    【解决方案1】:

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

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

    【讨论】:

    • 我希望它在幕后移动,然后进行转换。我会先尝试设置 CGAffineTransformIdentity,看看它是如何工作的 :) - 刚刚检查过,但不起作用 mate
    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 2016-09-01
    • 2012-07-11
    • 2018-04-08
    • 2017-04-20
    • 2018-10-02
    • 2016-09-04
    相关资源
    最近更新 更多