用animation从完整的UIImage中填充UIImageView的百分比

我有一个名为bigBottleUIImageView

 bigBottle.image = [UIImage imageNamed:@"fullBootle.png"]; 

fullBootle.png是一个装满100%的瓶子。

我需要两个步骤:

  1. 首先用百分比(让我们说50%填充)做一个图像作品 – 也就是说,一个瓶子从底部填充到一半。
  2. animationbigBottle从瓶子下降到50%的填充效果。 我的代码如下:

     CGRect captureRect = CGRectMake(0,22,18,72); UIImage *captureImg; UIGraphicsBeginImageContext(captureRect.size); CGContextRef context = UIGraphicsGetCurrentContext(); [self.bigBottle.layer renderInContext:context]; captureImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); self.bigBottle.image = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(captureImg.CGImage, captureRect)]; CGRect captureRect1 = CGRectMake(0,37,18,72); self.bigBottle.frame=CGRectMake(45, 174,18,1); CGRect newFrameOfMyView1 = CGRectMake(45,129, 18, 45); [UIView animateWithDuration:3.0f animations:^{ self.bigBottle.frame = newFrameOfMyView1; } completion:^(BOOL finished){ NSLog( @"done bottle: 1" ); }]; 

这是使这个瓶子的animation更简单,更高效的内存的另一种方法? 我有几十瓶随机填充百分比(让我们说瓶80是20%填满),并没有模糊的结果图像?

这是我的简单的实现你的问题。 创buildCALayer的新子类并实现一个名为percent的dynamic属性,然后将其animation为,

ImageLayer.h

 #import <QuartzCore/QuartzCore.h> @interface ImageLayer : CALayer @property (nonatomic,assign )float percent; @property (nonatomic, strong) NSString *imageName; @end 

ImageLayer.m

 #import "ImageLayer.h" @implementation ImageLayer @dynamic percent; + (BOOL)needsDisplayForKey:(NSString *)key{ if([key isEqualToString:@"percent"]){ return YES; }else return [super needsDisplayForKey:key]; } - (void)drawInContext:(CGContextRef)ctx{ UIGraphicsPushContext(ctx); UIImage *image = [UIImage imageNamed:@"Bottle.jpg"]; [image drawInRect:self.bounds]; float heightToBeCovered = self.percent * CGRectGetHeight(self.bounds); float verticalYPosition = CGRectGetHeight(self.bounds) - heightToBeCovered; UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, verticalYPosition, CGRectGetWidth(self.bounds), heightToBeCovered )]; [[UIColor blackColor] setFill]; [path fill]; UIGraphicsPopContext(); } @end 

ViewController.m

 @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; ImageLayer *imageLayer = [ImageLayer layer]; imageLayer.position = self.view.center; imageLayer.frame = self.view.bounds; [self.view.layer addSublayer:imageLayer]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"percent"]; [animation setFromValue:@1]; [animation setToValue:@0]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; [animation setDuration:10.0]; [imageLayer addAnimation:animation forKey:@"imageAnimation"]; } @end 

不,我只是用简单的animation做这个填充:

 self.bigBottle.frame =CGRectMake(self.bigBottle.frame.origin.x,self.bigBottle.frame.origin.y + self.bigBottle.frame.size.height,18,0); /*so the height is 0 on start*/ [self.bigBottle setClipsToBounds:YES]; //so image will be bounds to UIImageView bounds [UIView animateWithDuration:2.0f animations:^{ self.bigBottle.frame =CGRectMake(self.bigBottle.frame.origin.x,self.bigBottle.frame.origin.y -43 ,18,self.bigBottle.frame.size.height+43); //where 43 is a value I generate random