如何在目标c中开发涂鸦屏幕?

时间:2016-12-14 09:39:00

标签: ios objective-c

我试过这样..但是无法在view.i上使用带有视图控制器的故事板...

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

我没有在视图上绘制任何线条......我无法触摸或绘制任何线路屏幕..所以任何人都可以在此发行中提供帮助......提前告知...

1 个答案:

答案 0 :(得分: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类型。以下是在storyboard / nib上为UIView设置自定义类的方法。在此处的类字段中输入GridView

enter image description here

预览

enter image description here