【问题标题】:Duration when animating ImageView is ignored动画 ImageView 时的持续时间被忽略
【发布时间】:2013-10-24 12:36:08
【问题描述】:

我正在尝试为一系列图像制作动画。

图片之间的变化不一定要有动画,但我是用动画来控制时间的:

-(void)nextImage
{
    [UIView animateWithDuration:0.5 animations:^{
        self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d",index++]];
    }completion:^(BOOL completed){
        if (index < 50)
        {
            [self nextImage];
        }
    }];
}

图像在变化,但无论我使用什么持续时间,它都会忽略时间并尽可能快地进行。

如果我更改 alpha,也会发生同样的情况:

-(void)nextImage
{
    [UIView animateWithDuration:0.5 animations:^{
        self.imageView.alpha = 1 - index++/100
    }completion:^(BOOL completed){
        if (index < 50)
        {
            [self nextImage];
        }
    }];
}

【问题讨论】:

    标签: ios objective-c uiimageview


    【解决方案1】:

    只有UIView 的部分属性是animatable:

    @property frame
    @property bounds
    @property center
    @property transform
    @property alpha
    @property backgroundColor
    @property contentStretch
    

    UIImageViewimage 属性不可设置动画。

    如果您要更新 UIImageView 内的图像,请改用其他技术,例如块:

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),    
        dispatch_get_current_queue(), ^{
           // update your image
        });
    

    (替代方案:performSelectorAfterDelay:NSTimer。不过我建议使用块。)

    我认为您的 alpha 动画由于您的部门中的 int 截断而无法正常工作。试试这个:

    self.imageView.alpha = 1.0f - (float)index++/100.0f;
    

    原始除法的问题在于表达式a / b(两者都是整数)被执行为整数除法。所以如果a b,结果将是0——换句话说,所有值的完全透明的alpha设置。

    【讨论】:

    • 好的,但是为什么在为 alpha 设置动画时不起作用?
    【解决方案2】:

    Alpha 版无法正常工作,因为您已编写

    self.imageView.alpha = 1 - index++/100;
    

    这里的所有内容都是int,因此您的结果只能是整数值,即 1 或 0。请改用它:

    self.imageView.alpha = 1.0f - index++/100.0f;
    

    编译器将能够将index 隐式转换为浮点数,但您可以显式地编写:

    self.imageView.alpha = 1.0f - (CGFloat)(index++)/100.0f;
    

    【讨论】:

      【解决方案3】:

      [UIView animateDuration:animations:completion]; 无法做到这一点 尝试使用 NSTimer 并调用每一步都更改图像的函数。

      【讨论】:

        【解决方案4】:

        你也可以使用 UIImageView 动画属性,例如:

        // arrayWithImages
        arrayPictures = [[NSMutableArray alloc] initWithCapacity:50];
        
        // The name of your images should 
        for (int i=0; i<=49; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d.jpg",i]];
            [arrayPictures addObject:image];
        }
        
        imageView.animationDuration = 0.5;
        imageView.animationImages = arrayPictures;
        
        [imageView startAnimating];
        

        根据图像的数量,您可能会遇到一些内存问题,并且必须使用较低级别的解决方案为图像设置动画。

        【讨论】:

          【解决方案5】:

          如果他们只是动画持续时间问题,那么可能是动画未启用,因为我在我的 ios 应用程序中遇到了这个问题。这是简单的解决方案:

          只需要添加 [UIView setAnimationsEnabled:YES];在开始动画块之前。所以你的完整代码将是这样的:

          -(void)nextImage
          {
              [UIView setAnimationsEnabled:YES]
              [UIView animateWithDuration:0.5 animations:^{
                  self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d",index++]];
              }completion:^(BOOL completed){
                  if (index < 50)
                  {
                      [self nextImage];
                  }
              }];
          }
          

          【讨论】:

            【解决方案6】:

            为什么不使用 ImageView 的 images 属性并设置要动画的图像数组?

            【讨论】:

            • 这只会改变显示的图像,不会有动画。
            • 请看原题:“图片之间的变化不一定要有动画”
            猜你喜欢
            • 2021-05-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-15
            相关资源
            最近更新 更多