开发者社区> 问答> 正文

UIImageView通过startAnimating方法,播放经过CGImage翻转后的图像无效

UIImageView通过startAnimating方法,播放经过CGImage翻转后的图像时,该图像全是原始图像,即旋转生成的UIImage的array在startAnimating无效。该怎么解决?

大家可以测试以下代码:

//TEST 
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30.0, 30.0, 50.0, 50.0)]; 
UIImage* image = [UIImage imageNamed:@"Image.png"]; 
UIImage* orientImage = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDownMirrored]; 

imageView.animationImages = [[NSArray alloc] initWithObjects:orientImage, orientImage, nil]; 
imageView.animationDuration = 0.3; 
imageView.animationRepeatCount = 10; 
[imageView startAnimating];

[self.view addSubview:imageView];

PS:这里的翻转不是旋转,是OrientationUpMirrored,就是水平翻转、左右方向换过来;这里说的动画,就是像UIImageView的startAnimating的精灵动画,不是UIView animation那种关键帧动画。

展开
收起
a123456678 2016-07-27 14:51:15 2777 0
1 条回答
写回答
取消 提交回答
  • 首先,

    imageView.animationImages = [[NSArray alloc] initWithObjects:orientImage, orientImage, nil];
    你不觉得两个对象是一样的?

    其次,
    你这个

     [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDownMirrored];
    创建出来的UIImage是不是和原图一样呢?
    
    翻转的动画为什么不用CoreAnimation来实现呢?不需要用UIImageView的startAnimating
    
    CALayer *layer = [CALayer layer]
    // ... set bounds, position, etc.
    layer.contents = [UIImage imageNamed:@"image"].CGImage;
    
    [self.view.layer addSubLayer:layer];
    
    CAKeyFrameAnimation *transformAnim = [CAKeyFrameAnimation animWithKeyPath:@"transform.rotation.x"];
    // ... set duration, beginTime, etc.
    transformAnim.keyTimes = @[@(0.0), @(1.0)];
    transformAnim.values = @[(0.0), @(M_PI / 2)];
    [layer addAnimation:transformAnim forKey:@"transform.rotation.x"];
    2019-07-17 20:00:35
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载