zoukankan      html  css  js  c++  java
  • 为视图添加倒影效果

    const CGFloat kReflectPercent = -0.25f;
    const CGFloat kReflectOpacity = 0.3f;
    const CGFloat kReflectDistance = 10.0f;
    + (void) addSimpleReflectionToView: (UIView *) theView
    {
    CALayer *reflectionLayer = [CALayer layer];
    reflectionLayer.contents = [theView layer].contents;
    reflectionLayer.opacity = kReflectOpacity;
    reflectionLayer.frame = CGRectMake(0.0f, 0.0f,
    theView.frame.size.width, theView.frame.size.height *
    kReflectPercent);
    CATransform3D stransform = CATransform3DMakeScale(1.0f, -1.0f,
    1.0f);
    CATransform3D transform = CATransform3DTranslate(stransform, 0.0f,
    -(kReflectDistance + theView.frame.size.height), 0.0f);
    reflectionLayer.transform = transform;
    reflectionLayer.sublayerTransform = reflectionLayer.transform;
    [[theView layer] addSublayer:reflectionLayer];
    }

    使用Core Graphics创建倒影:

    + (CGImageRef) createGradientImage: (CGSize)size
    {
    CGFloat colors[] = {0.0, 1.0, 1.0, 1.0};
    // Create gradient in gray device color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, size.width,
    size.height, 8, 0, colorSpace, kCGImageAlphaNone);
    CGGradientRef gradient =
    CGGradientCreateWithColorComponents(colorSpace, colors,
    NULL, 2);
    CGColorSpaceRelease(colorSpace);
    // Draw the linear gradient
    CGPoint p1 = CGPointZero;
    CGPoint p2 = CGPointMake(0, size.height);
    CGContextDrawLinearGradient(context, gradient, p1, p2,
    kCGGradientDrawsAfterEndLocation);
    // Return the CGImage
    CGImageRef theCGImage = CGBitmapContextCreateImage(context);
    CFRelease(gradient);
    CGContextRelease(context);
    return theCGImage;
    }
    // Create a shrunken frame for the reflection
    + (UIImage *) reflectionOfView: (UIView *)theView
    withPercent: (CGFloat) percent
    {
    // Retain the width but shrink the height
    CGSize size = CGSizeMake(theView.frame.size.width,
    theView.frame.size.height * percent);
    // Shrink the view
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *partialimg =
    UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // build the mask
    CGImageRef mask = [ImageHelper createGradientImage:size];
    CGImageRef ref = CGImageCreateWithMask(partialimg.CGImage, mask);
    UIImage *theImage = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    CGImageRelease(mask);
    return theImage;
    }
    const CGFloat kReflectDistance = 10.0f;
    + (void) addReflectionToView: (UIView *) theView
    {
    theView.clipsToBounds = NO;
    UIImageView *reflection = [[UIImageView alloc] initWithImage:
    [ImageHelper reflectionOfView:theView withPercent: 0.45f]];
    CGRect frame = reflection.frame;
    frame.origin = CGPointMake(0.0f, theView.frame.size.height +
    kReflectDistance);
    reflection.frame = frame;
    // add the reflection as a simple subview
    [theView addSubview:reflection];
    [reflection release];
    }

  • 相关阅读:
    hdu5360 Hiking(水题)
    hdu5348 MZL's endless loop(欧拉回路)
    hdu5351 MZL's Border(规律题,java)
    hdu5347 MZL's chemistry(打表)
    hdu5344 MZL's xor(水题)
    hdu5338 ZZX and Permutations(贪心、线段树)
    hdu 5325 Crazy Bobo (树形dp)
    hdu5323 Solve this interesting problem(爆搜)
    hdu5322 Hope(dp)
    Lightoj1009 Back to Underworld(带权并查集)
  • 原文地址:https://www.cnblogs.com/GnagWang/p/2182237.html
Copyright © 2011-2022 走看看