我正在使用最新的 SDK 开发 iOS 5.0+ 应用程序。
我对 CoreGraphics 非常陌生,我不知道如何在CALayer
.
我发现我必须使用CGContextDrawRadialGradient来绘制辐射渐变。
在 Google 上搜索,我看到我必须将辐射渐变添加到 CALayer 的内容中,但要绘制它,我需要一个CGContext,我不知道如何获取它CGContext
。
你知道我该怎么做吗?
我找到了本教程,但它也使用CGContext
.
我的代码是这样的:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *testView;
@end
执行:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
- (void)drawRadiantGradient;
@end
@implementation ViewController
@synthesize testView = _testView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self drawRadiantGradient];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)drawRadiantGradient
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat redBallColors[] = {
1.0, 0.9, 0.9, 0.7,
1.0, 0.0, 0.0, 0.8
};
CGFloat glossLocations[] = {0.05, 0.9};
CGGradientRef ballGradient = CGGradientCreateWithColorComponents(colorSpace, redBallColors, glossLocations, 2);
CGRect circleBounds = CGRectMake(20, 250, 100, 100);
CGPoint startPoint = CGPointMake(50, 270);
CGPoint endPoint = CGPointMake(70, 300);
CGContextDrawRadialGradient(context, ballGradient, startPoint, 0, endPoint, 50, 0);
CGContextAddEllipseInRect(context, circleBounds);
CGContextDrawPath(context, kCGPathStroke);
}
我想创建一个CALayer
,绘制一个辐射渐变,并将其添加CALayer
到_testView
.