1.我们学习了很多的UI和知识点,但是并非多姿多彩的,现在我们就如何在view上绘图学习两种方法,一种是Quartz绘图,这种绘图方式比较简单.但是还有一种更加强大的接口OpenGL,但是代码的实现也相对是比较麻烦的.
今天就来学习Quartz绘图吧.
首先,我们来定义几个常量.
//
// BIDCOstants.h
// 第十六章:使用Quartz绘图
//
// Created by lichan on 13-11-27.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#ifndef _______Quartz___BIDCOstants_h
#define _______Quartz___BIDCOstants_h
typedef enum {
kLineShape = 0,
kRectShape,
kEllipseShape,
KImageShape
}ShapeType;
typedef enum {
kRedColorTab = 0,
kBlueColorTab ,
kYellowColorTab,
kGreenColorTab,
kRandomColorTab
}colorTabIndex;
#define degreesToRadian(x) (M_PI *(x)/180.0)
#endif
2)下面就owner下面的view添加如下控制类.
//
// BIDQuartzFun.h
// 第十六章:使用Quartz绘图
//
// Created by lichan on 13-11-27.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "BIDCOstants.h"
@interface BIDQuartzFun : UIView
@property (nonatomic)CGPoint firstTouch;
@property (nonatomic)CGPoint lastTouch;
@property (nonatomic,strong)UIColor *currentColor;
@property (nonatomic)ShapeType shapeType;
@property (nonatomic,strong)UIImage *drawImage;
@property (nonatomic)BOOL useRandomColor;
@property CGRect reDrawRect;
-(CGRect)currentRect;
@end
1.m文件
//
// BIDQuartzFun.m
// 第十六章:使用Quartz绘图
//
// Created by lichan on 13-11-27.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "BIDQuartzFun.h"
#import "UIColor+BIDRandom.h"
@implementation BIDQuartzFun
@synthesize firstTouch,lastTouch,currentColor,drawImage,useRandomColor,shapeType;
@synthesize reDrawRect;
-(CGRect)currentRect
{
return CGRectMake(firstTouch.x, firstTouch.y, lastTouch.x-firstTouch.x, lastTouch.y - firstTouch.y);
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder]) {
currentColor = [UIColor redColor];
useRandomColor = NO;
self.drawImage = [UIImage imageNamed:@"screen.png"];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (useRandomColor) {
self.currentColor = [UIColor randomColor];
}
UITouch *touch = [touches anyObject];
firstTouch = [touch locationInView:self];
lastTouch = [touch locationInView:self];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self];
if (shapeType == KImageShape) {
CGFloat horizontalOffset = drawImage.size.width/2;
CGFloat verticalOffset = drawImage.size.height/2;
reDrawRect = CGRectUnion(reDrawRect, CGRectMake(lastTouch.x -horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
}
else
reDrawRect = CGRectUnion(reDrawRect, self.currentRect);
reDrawRect = CGRectInset(reDrawRect, -2.0, -2.0);
[self setNeedsDisplayInRect:reDrawRect];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
lastTouch = [touch locationInView:self];
if (shapeType == KImageShape) {
CGFloat horizontalOffset = drawImage.size.width/2;
CGFloat verticalOffset = drawImage.size.height/2;
reDrawRect = CGRectUnion(reDrawRect, CGRectMake(lastTouch.x -horizontalOffset, lastTouch.y - verticalOffset, drawImage.size.width, drawImage.size.height));
}
reDrawRect = CGRectUnion(reDrawRect, self.currentRect);
[self setNeedsDisplayInRect:reDrawRect];
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
//检索对当前上下文的引用,以便知道要绘图的位置
CGContextSetLineWidth(context, 2.0f);
CGContextSetStrokeColorWithColor(context, currentColor.CGColor);
//椭圆和矩形
CGContextSetFillColorWithColor(context, currentColor.CGColor);
// CGRect currentRect = CGRectMake(firstTouch.x, firstTouch.y, lastTouch.x-firstTouch.x, lastTouch.y - firstTouch.y);
switch (shapeType) {
case kLineShape:
CGContextMoveToPoint(context, firstTouch.x, firstTouch.y);
CGContextAddLineToPoint(context, lastTouch.x, lastTouch.y);
CGContextStrokePath(context);
break;
case kRectShape:
CGContextAddRect(context, self.currentRect);
CGContextDrawPath(context, kCGPathFillStroke);
break;
case kEllipseShape:
CGContextAddEllipseInRect(context, self.currentRect);
CGContextDrawPath(context, kCGPathFillStroke);
break;
case KImageShape:
{
CGFloat horizontalOffset = drawImage.size.width/2;
CGFloat verticalOffset = drawImage.size.height/2;
CGPoint drawPoint = CGPointMake(lastTouch.x - horizontalOffset, lastTouch.y - verticalOffset);
[drawImage drawAtPoint:drawPoint];
break;
}
default:
break;
}
}
@end
3)下面就UIColor添加一个 随机方法
//
// UIColor+BIDRandom.h
// 第十六章:使用Quartz绘图
//
// Created by lichan on 13-11-27.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIColor (BIDRandom)
+(UIColor *)randomColor;
@end
.m文件
//
// UIColor+BIDRandom.m
// 第十六章:使用Quartz绘图
//
// Created by lichan on 13-11-27.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "UIColor+BIDRandom.h"
@implementation UIColor (BIDRandom)
+(UIColor *)randomColor
{
static BOOL seeded = NO;
if (!seeded) {
seeded = YES;
srandom(time(NULL));
}
CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
@end
3.然后我们定义自己的viewCOntroller 也就是app运行时的记载文件
//
// BIDViewController.h
// 第十六章:使用Quartz绘图
//
// Created by lichan on 13-11-27.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface BIDViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *colorControl;
- (IBAction)changeColor:(id)sender;
- (IBAction)changeShape:(id)sender;
@end
.m文件
//
// BIDViewController.m
// 第十六章:使用Quartz绘图
//
// Created by lichan on 13-11-27.
// Copyright (c) 2013年 com.lichan. All rights reserved.
//
#import "BIDViewController.h"
#import "BIDQuartzFun.h"
@interface BIDViewController ()
@end
@implementation BIDViewController
@synthesize colorControl;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)changeColor:(id)sender {
UISegmentedControl *control = sender;
NSInteger index= [control selectedSegmentIndex];
BIDQuartzFun *quartzView = (BIDQuartzFun *)self.view;
switch (index) {
case kRedColorTab:
quartzView.currentColor = [UIColor redColor];
quartzView.useRandomColor = NO;
break;
case kYellowColorTab:
quartzView.currentColor = [UIColor yellowColor];
quartzView.useRandomColor = NO;
break;
case kBlueColorTab:
quartzView.currentColor = [UIColor blueColor];
quartzView.useRandomColor = NO;
break;
case kGreenColorTab:
quartzView.currentColor = [UIColor greenColor];
quartzView.useRandomColor = NO;
break;
case kRandomColorTab:
quartzView.useRandomColor = YES;
break;
default:
break;
}
}
- (IBAction)changeShape:(id)sender {
UISegmentedControl *control = sender;
[(BIDQuartzFun *)self.view setShapeType:[control selectedSegmentIndex]];
if ([control selectedSegmentIndex] == KImageShape) {
colorControl.hidden =YES;
}
else
colorControl.hidden = NO;
}
@end