2014-02-23 71 views
0

我有一個UIImageView命名bigBottle從全面的UIImage填充的UIImageView%的動畫

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

fullBootle.png填充到100%的瓶子。

我需要兩個步驟:

  1. 首先要以百分比圖像的作物(突然想到說50%的填充) - 也就是說,一瓶裝滿了一半,從底部)。
  2. Animate bigBottle從瓶子的下方向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"); 
    }]; 
    

這是另一種方式,使灌裝瓶更簡單的這部動畫,和更多的內存效率?我有幾十瓶隨機填充百分比(讓我們說瓶子80填滿了20%)並且沒有模糊的結果圖像?

回答

6

這是我對你的問題的簡單實現。創建的CALayer的一個新的子類,並實行動態屬性調用%的,然後動畫它,

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 

視圖控制器。 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 
+0

Thenk你。我還沒有足夠的積分投票,但我很快就會做。 – user3344236

+1

那麼,你的例子填充矩形,我用animation.fillMode = kCAFillModeForwards;保持結果可見。請看看這個屏幕截圖。這確實是我需要的那種動畫的最後階段。因此,瓶子從底部到頂部填充,從1高度開始,填充滿足瓶子的整個輪廓。 http://tinypic.com/view.php?pic=2h7lzs4&s=8 – user3344236

0

不,我只是讓這個填充了簡單的動畫:

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