• 给视图加上倒影效果


    给视图加上倒影效果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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];
    }
     
     

    给视图加上颜色渐变的背景

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    @interface ColorGradientView : NSView
    {
      NSColor *startingColor;
      NSColor *endingColor;
      int angle;
    }
      
    // Define the variables as properties
    @property(nonatomic, retain) NSColor *startingColor;
    @property(nonatomic, retain) NSColor *endingColor;
    @property(assign) int angle;
      
    @end
      
    @implementation ColorGradientView
      
    // Automatically create accessor methods
    @synthesize startingColor;
    @synthesize endingColor;
    @synthesize angle;
      
    - (id)initWithFrame:(NSRect)frame;
    {
      if (self = [super initWithFrame:frame]) {
        [self setStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:1.0]];
        [self setEndingColor:nil];
        [self setAngle:270];
      }
      return self;
    }
      
    - (void)drawRect:(NSRect)rect;
    {
      if (endingColor == nil || [startingColor isEqual:endingColor]) {
        // Fill view with a standard background color
        [startingColor set];
        NSRectFill(rect);
      }
      else {
        // Fill view with a top-down gradient
        // from startingColor to endingColor
        NSGradient* aGradient = [[[NSGradient alloc]
            initWithStartingColor:startingColor
            endingColor:endingColor] autorelease];
        [aGradient drawInRect:[self bounds] angle:angle];
      }
    }
      
    - (void)setStartingColor:(NSColor *)newColor;
    {
     [startingColor autorelease];
     startingColor = [newColor retain];
      
     [self setNeedsDisplay:YES];
    }
      
    - (void)setEndingColor:(NSColor *)newColor;
    {
     [endingColor autorelease];
     endingColor = [newColor retain];
      
     [self setNeedsDisplay:YES];
    }
      
    - (void)dealloc;
    {
     [self setStartingColor:nil];
     [self setEndingColor:nil];
     [super dealloc];
    }
      
    @end
      • 一个效果更好的视图倒影效果,其倒影的影像会在其底部逐渐消失。带有渐隐效果的倒影方案首先要把视图内容复制到一个缩短到位图中,然后应用一个渐变蒙版。然后这些效果作为一个UIImage返回并被添加到原始的视图中,成为一个新的UIImageCiew。 
        要使这种倒影有效,必须要禁用视图的剪裁功能,即将视图的clipsToView属性设置为NO。
         
         
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        44
        45
        46
        47
        48
        49
        50
        51
        52
        53
        54
        55
        56
        57
        + (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];
        }
  • 相关阅读:
    各自的特点:
    errno
    内存(堆)的动态申请和释放
    printf和scanf中的%控制
    linux中软件包管理
    Ubuntu新装系统要装软件
    数组
    linux c中需要记住的东西
    硬件设计中的一些思路
    传输线理论
  • 原文地址:https://www.cnblogs.com/ranger-jlu/p/3877938.html
Copyright © 2020-2023  润新知