我试过了,好像this..but不能在视图上绘制任何东西。我使用带有视图控制器的故事板...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view
[self.view setMultipleTouchEnabled:NO];
[self.view setBackgroundColor:[UIColor whiteColor]];
bizerpath = [[UIBezierPath alloc]init];
[bizerpath setLineWidth:2.0];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self.view];
[bizerpath moveToPoint:p];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint p = [touch locationInView:self.view];
[bizerpath addLineToPoint:p]; // (4)
[self.view setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect // (5)
{
[[UIColor blackColor] setStroke];
[bizerpath stroke];
NSLog(@"welcome to scrible");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view touchesEnded:touches withEvent:event];
}
-(void)viewWillAppear:(BOOL)animated
{
[self.view setNeedsDisplay];
}我没有在视图上画任何线条…我不能触摸或绘制任何线条screen..so任何人可以帮助在这个issuance...thks事先…
发布于 2016-12-15 01:54:02
您的子类化UIView的关键思想丢失了。
而不是上述代码,添加两个附加文件,名为
GridView.h
#import <UIKit/UIKit.h>
IB_DESIGNABLE @interface GridView : UIView
@endGridView.m
#import "GridView.h"
enum GRID_BOX_TYPE
{
GRID_BOX_BLANK = 0,
GRID_BOX_RED,
GRID_BOX_BLUE
};
#define NUMBER_OF_ROWS 5
#define NUMBER_OF_COLS 4
#define GRID_LINE_WIDTH 2
@implementation GridView
{
enum GRID_BOX_TYPE twoDGrid[NUMBER_OF_ROWS][NUMBER_OF_COLS];
CGSize gridSizeRatio;
}
#pragma mark init methods
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self){
//do initialization here
[self commonInitializer];
}
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self){
//do initialization here
[self commonInitializer];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
// CGContextClearRect(context, rect);
//global settings
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
for(int i=0;i<NUMBER_OF_ROWS;i++)
{
for(int j=0;j<NUMBER_OF_COLS;j++)
{
enum GRID_BOX_TYPE blockType = twoDGrid[i][j];
switch (blockType) {
case GRID_BOX_BLANK :
{
//draw white box
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextStrokeRectWithWidth(context, CGRectMake(j * rect.size.width * gridSizeRatio.width,
i * rect.size.height * gridSizeRatio.height,
rect.size.width * gridSizeRatio.width - GRID_LINE_WIDTH,
rect.size.height * gridSizeRatio.height - GRID_LINE_WIDTH), GRID_LINE_WIDTH);
CGContextDrawPath(context, kCGPathFill);
}
break;
case GRID_BOX_RED:
{
//draw red box
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextStrokeRectWithWidth(context, CGRectMake(j * rect.size.width * gridSizeRatio.width,
i * rect.size.height * gridSizeRatio.height,
rect.size.width * gridSizeRatio.width - GRID_LINE_WIDTH,
rect.size.height * gridSizeRatio.height - GRID_LINE_WIDTH), GRID_LINE_WIDTH);
CGContextDrawPath(context, kCGPathFill);
}
break;
case GRID_BOX_BLUE:
{
//draw blue box
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextStrokeRectWithWidth(context, CGRectMake(j * rect.size.width * gridSizeRatio.width,
i * rect.size.height * gridSizeRatio.height,
rect.size.width * gridSizeRatio.width - GRID_LINE_WIDTH,
rect.size.height * gridSizeRatio.height - GRID_LINE_WIDTH), GRID_LINE_WIDTH);
CGContextDrawPath(context, kCGPathFill);
}
break;
default:
break;
}
}
}
}
#pragma mark private initializer
-(void)commonInitializer
{
twoDGrid[0][0]= GRID_BOX_RED;
twoDGrid[0][1]= GRID_BOX_RED;
twoDGrid[0][2]= GRID_BOX_RED;
twoDGrid[0][3]= GRID_BOX_RED;
twoDGrid[1][0]= GRID_BOX_RED;
twoDGrid[1][1]= GRID_BOX_BLANK;
twoDGrid[1][2]= GRID_BOX_BLANK;
twoDGrid[1][3]= GRID_BOX_RED;
twoDGrid[2][0]= GRID_BOX_RED;
twoDGrid[2][1]= GRID_BOX_BLUE;
twoDGrid[2][2]= GRID_BOX_BLUE;
twoDGrid[2][3]= GRID_BOX_RED;
twoDGrid[3][0]= GRID_BOX_RED;
twoDGrid[3][1]= GRID_BOX_BLANK;
twoDGrid[3][2]= GRID_BOX_BLANK;
twoDGrid[3][3]= GRID_BOX_RED;
twoDGrid[4][0]= GRID_BOX_RED;
twoDGrid[4][1]= GRID_BOX_RED;
twoDGrid[4][2]= GRID_BOX_RED;
twoDGrid[4][3]= GRID_BOX_RED;
gridSizeRatio = CGSizeMake(1.0/NUMBER_OF_COLS, 1.0/NUMBER_OF_ROWS);
}
@end然后从故事板中拖动一个UIView并将其设置为GridView类型。下面是如何在情节提要/ nib上设置UIView的自定义类。在此处的类字段中输入GridView。

预览

https://stackoverflow.com/questions/41139228
复制相似问题