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那种关键帧动画。
uiimageview 动画 ios objective-c
男人的浪漫
10 years, 3 months ago
Answers
首先,
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"];
kichan
answered 10 years, 3 months ago