Last active
December 14, 2015 21:19
-
-
Save monjer/5150776 to your computer and use it in GitHub Desktop.
模拟UIAlertView的模态背景遮罩
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MJModalOverlayer.h | |
| // Overlayer | |
| // | |
| // Created by manjun.han on 13-3-13. | |
| // Copyright (c) 2013年 com.hmj. All rights reserved. | |
| // | |
| #import <UIKit/UIKit.h> | |
| @interface MJModalOverlayer : UIView | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MJOverlayer.m | |
| // Overlayer | |
| // | |
| // Created by manjun.han on 13-3-13. | |
| // Copyright (c) 2013年 com.hmj. All rights reserved. | |
| // | |
| #import "MJModalOverlayer.h" | |
| @implementation MJModalOverlayer | |
| - (id)init | |
| { | |
| self = [super init] ; | |
| if (self) { | |
| self.backgroundColor = [UIColor clearColor] ; | |
| } | |
| return self ; | |
| } | |
| - (id)initWithFrame:(CGRect)frame | |
| { | |
| self = [super initWithFrame:frame] ; | |
| if(self){ | |
| self.backgroundColor = [UIColor clearColor] ; | |
| } | |
| return self ; | |
| } | |
| - (void)drawRect:(CGRect)rect | |
| { | |
| CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); | |
| CGContextRef ctx = UIGraphicsGetCurrentContext(); | |
| // 渐变颜色 | |
| UIColor *gradientOuter = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.4]; | |
| UIColor *gradientInner = [UIColor colorWithRed: 0 green: 0 blue: 0 alpha: 0.1]; | |
| NSArray *radialGradientColors = @[(id)gradientInner.CGColor, (id)gradientOuter.CGColor]; | |
| // 渐变locations | |
| CGFloat radialGradientLocations[] = {0, 0.5, 1}; | |
| CGGradientRef radialGradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)radialGradientColors, radialGradientLocations); | |
| // 剪切路径 | |
| UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)]; | |
| CGContextSaveGState(ctx); | |
| [rectanglePath addClip]; | |
| CGContextDrawRadialGradient(ctx, radialGradient, CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2), rect.size.width / 4, | |
| CGPointMake(rect.origin.x + rect.size.width / 2, rect.origin.y + rect.size.height / 2), rect.size.width, | |
| kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation); | |
| CGContextRestoreGState(ctx); | |
| // 释放资源 | |
| CGGradientRelease(radialGradient); | |
| CGColorSpaceRelease(colorSpace); | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment