当前位置:编程学习 > wap >>

iOS图片倒影效果的2种实现

实现一
使用一个继承自UIView的类来包含要实现倒影效果的图片,重写这个UIView子类的绘图方法,以实现图片于倒影,
然后把这个View 添加到相应的地方显示。
代码:
CKReflectionImage.h
[cpp]  
#import <UIKit/UIKit.h>  
  
@inte易做图ce CKReflectionImage : UIView {  
@private  
      
    UIImage *image_;  
      
    /** 
     * Value of gradient start. This value is divided to height of image. 
     */  
    CGFloat visibleReflectionHeight_;  
      
    /** 
     * Padding to top image. 
     */  
    CGFloat paddingToTopImage_;  
}  
  
  
@property (nonatomic, readwrite, retain) UIImage *image;  
@property (nonatomic, readwrite, assign) CGFloat visibleReflectionHeight;  
@property (nonatomic, readwrite, assign) CGFloat paddingToTopImage;  
  
@end  
CKReflectionImage.h
[cpp 
#import "CKReflectionImage.h"  
  
@implementation CKReflectionImage  
  
#pragma mark -  
#pragma mark Properties  
  
@synthesize image = image_;  
@synthesize visibleReflectionHeight = visibleReflectionHeight_;  
@synthesize paddingToTopImage = paddingToTopImage_;  
  
#pragma mark -  
#pragma mark Memory management  
  
- (void)dealloc {  
      
    [image_ release];  
    image_ = nil;  
      
    visibleReflectionHeight_ = 0.0f;  
      
    paddingToTopImage_ = 0.0f;  
      
    [super dealloc];  
      
}  
  
#pragma mark -  
#pragma mark Draw methods  
  
/** 
 * Draws the receiver’s image within the passed-in rectangle. 
 *  
 * @param rect: The portion of the view’s bounds that needs to be updated. 
 */  
- (void)drawRect:(CGRect)rect {  
      
    [super drawRect:rect];  
      
    if (image_ != nil) {  
          
        // Get current context to draw.  
        CGContextRef context = UIGraphicsGetCurrentContext();  
          
        // Reflection image references  
        CGImageRef reflectionImage = NULL;  
        CGImageRef gradientImage = NULL;  
          
        // Frame of image  
        CGRect frame = [self frame];  
        frame.origin.x = 0.0f;  
        frame.origin.y = 0.0f;  
        frame.size.width = CGRectGetWidth(frame);  
        frame.size.height = image_.size.height * CGRectGetWidth(frame) / image_.size.width;  
          
        // Draw initial image in context  
        CGContextSaveGState(context);  
        {  
              
            // Draw image in context, commented but the image show in reverse.  
//            CGContextDrawImage(context, frame, [image_ CGImage]);  
              
            // Push context to draw image.  
            UIGraphicsPushContext(context);  
              
            // Draw original image in top  
            [image_ drawInRect:frame];  
              
            // Pop to context  
            UIGraphicsPopContext();  
              
        }  
        CGContextRestoreGState(context);  
          
        // Create gradient bitmap  
        CGContextSaveGState(context);  
        {  
              
            // Gradient is always black-white and the mask must be in the gray colorspace.  
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();  
              
            // Create a bitmap context  
            CGContextRef gradientContext = CGBitmapContextCreate(NULL, CGRectGetWidth(frame), CGRectGetHeight(frame), 8, 0, colorSpace, kCGImageAlphaNone);  
              
            // Define the start and the end grayscale values (with the alpha, even though our  
            // bitmap context doesn't support alpha gradient requieres it).  
            CGFloat colors[] = {0.0f, 1.0f, 1.0f, 1.0f};  
              
            // Creates the CGGradient  
            CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);  
              
            // Release colorSpace reference  
            CGColorSpaceRelease(colorSpace);  
              
            // Create the start and end points for the gradient vector (straight down).  
            CGPoint gradientStartPoint = CGPointMake(0, (CGRectGetHeight(frame) - visibleReflectionHeight_));  
&
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,