ios - UIView 动画的持续时间比指定的快得多

标签 ios uiview uiviewanimation

我正在使用 UIView 动画来设置“加载屏幕”,然后在 NSURLSessionDataTask 完成后使加载屏幕消失。理想情况下,下面的代码将在单色背景上绘制一个矩形 subview ,然后旋转并平移该矩形 subview ,直到 DataTask 执行其完成 block :

void getData()
{
    ....
    NSURLSessionDataTask *dataTask = [sharedSessionMainQueue dataTaskWithRequest:req 
                                    completionHandler: ^(NSData *data,
                                    NSURLResponse *response, NSError *error){

         NSMutableArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data
                                                                      options:0
                                                                        error:nil];

         dispatch_async(dispatch_get_main_queue(), ^{

             ...
             [UIView animateWithDuration:10 animations:^{
                 [_ballView removeFromSuperview];
                 [_tempview removeFromSuperview];
             }];

         }];

        [self.view addSubview:_tempview];
        _ballView = [[UIView alloc] initWithFrame:CGRectMake(_width/2 - 10, _height*.9, 10.0f, 80.0f)];
        _ballView.center = CGPointMake(_width*.5, _height*.3);
        _ballView.backgroundColor = BACKGROUND_COLOR; //[UIColor orangeColor];
        [self.view addSubview: _ballView];

        CGFloat angleToRotate = 20*M_PI;

        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionAutoreverse animations:^{
            [UIView setAnimationRepeatCount:100];
            CGRect f = _ballView.frame;
            f.origin.y = _height*.1;
            _ballView.frame = f;
            _ballView.transform = CGAffineTransformMakeRotation(angleToRotate);

        } completion:nil];

        [dataTask resume];
    }

}

但我有两个问题。首先,无论我在任何一个动画 block 中指定多长时间,动画都非常快速地完成 - 远少于一秒。其次, block 平移但不旋转。

关于发生了什么的任何想法?完成 block 内的 UIView 动画会发生什么奇怪的事情吗?

谢谢!

最佳答案

第一个 block 动画(使用 removeFromSuperview 调用)几乎会立即完成,因为 removeFromSuperview 是不可动画的。你应该使用一些类似的东西

[UIView animateWithDuration:10 animations:^{
    _ballView.alpha = 0;
    _tempview.alpha = 0;
} completion:^(BOOL finished) {
    [_ballView removeFromSuperview];
    [_tempview removeFromSuperview];
}];

第二个 block 中的问题是,您需要将度数转换为弧度,但无论如何,它不起作用,因为它将从相同的起始角度旋转。你需要使用 NSTimer,或者更简单更好的 - CAAnimation:
CABasicAnimation* rotationAnimation;
CGFloat duration = 2, repeat = 50;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 * duration ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = repeat;

[view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

关于ios - UIView 动画的持续时间比指定的快得多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390677/

相关文章:

ios - UITableView 显示从 0 开始的行号

cocoa-touch - UISegmentedControl 不随 super View 缩放

ios - 悬停动画上的 UIButton 比例 - iOS Objective-C

ios - UIActivityIndi​​catorView在正确的时间显示

html - CSS 未在 IOS < ver 10 设备上加载

ios - 如何为 ionic 框架编写自定义/自己的插件

ios - 有条件地在 UITabBar 选择上显示 View Controller

swift - UIView Nib 中的 UICollectionView

ios - 仅为 UIImageView 的一部分设置 alpha 值

ios - UIView动画开头跳转