【问题标题】:animate a UIView while moving it in objective c在目标 c 中移动 UIView 时为其设置动画
【发布时间】:2017-11-06 07:58:13
【问题描述】:

我需要在 UIView 从一个地方移动到另一个地方时为其设置动画,我正在使用以下内容:

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        _whileSpitImageView.image = [UIImage imageNamed:@"picture.png"];
         _whileSpitImageView.frame = CGRectMake(160, 300, 50, 50);
    } completion:nil];

}

但是视图只是在移动并且在移动时没有动画,有什么建议吗?

【问题讨论】:

  • “没有动画”是什么意思?
  • 你的didSelectItemAtIndexPath 方法满了吗?你在哪里创建_whileSpitImageView 并使用自动布局或设置框架创建它?
  • 它只是 UIView 中移动的一个图像,我需要它在移动时进行动画处理,也许设置一系列图像并在移动时对其进行动画处理?
  • 再一次,“动画化”是什么意思?你的意思是改变imageView的图片?
  • 图像需要在画布上移动,并且需要改变颜色,例如在移动时...

标签: ios objective-c uiview uiviewanimation


【解决方案1】:

您在滥用 UIView animateWithDuration。问题是 animateWithDuration 只能为 UIView 的某些属性设置动画,这些属性是 frame、bounds、center、transform、alpha、backgroundColor 和 contentStretch (Animations From Apple)。

您可能想要逐步制作动画。例如:

var imageView: UIImageView?
UIView.animate(withDuration: 0.3, animations: {
    imageView?.alpha = 0.0},
    completion: {(_ finished: Bool) -> Void in
    imageView?.image = UIImage(named: "newImage")
    UIView.animate(withDuration: 0.3, animations: {
        imageView?.alpha = 1.0
    })
})

注意。我不确定语法是否完全正确,我使用了在线转换器。但逻辑是存在的。原文Objective-C预翻译如下

UIImageView *imageView;
[UIView animateWithDuration:0.3 animations:^{
    imageView.alpha = 0.0;
} completion:^(BOOL finished) {
    [imageView setImage:[UIImage imageNamed:@"newImage"]];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.alpha = 1.0;
    }];
}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多