2016-12-14 1 views
0

я пытался как this..but не мог ничего на view.i используемой раскадровки с видом на контроллере рисовать ...Как разработать экран scribble в объективе c?

- (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]; 
} 

я не нарисовать любую линию на взгляде .... я не мог прикосновение или нарисовать любого lin eon screen..so любой может помочь в этом выпуске ... thks заранее ...

ответ

0

Отсутствует ключевая идея вашего подкласса UIView.

Вместо выше кода, добавить два дополнительных файла с именем

GridView.h

#import <UIKit/UIKit.h> 

IB_DESIGNABLE @interface GridView : UIView 
@end 

GridView.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. Вот как вы настраиваете пользовательский класс для UIView на раскадровке/nib. Введите GridView в поле класса здесь.

enter image description here

Просмотр

enter image description here