1

ボタンをクリックして正方形と円を作成する必要があるプロジェクトがあります。

button の前に textfileds があり、textfiled では 45 などの値を指定し、uibutton をクリックするとアクションが実行され、45 の正方形が iPhone scfreen 自体で自動的に調整されます。

画面サイズが 320 * 480 であると仮定すると、正方形は画面に合わせて自動的に調整されます。

Textfiled に値 300 を指定すると、300 個の正方形が作成され、画面上で自動的に調整されます。

1500 のような値を与えると、ある段階では方眼紙のように表示されます。

それを行う方法、開始方法、開始場所がわかりません。

Quartcore Framework を使用することを考えているだけですが、検索したプロジェクトを開始する場所からアイデアがありません。

専門家からの提案やアイデアが欲しい。専門家からのアイデアや提案は大歓迎です。

4

2 に答える 2

0
// Step 1 : Add QuartzCore.framework in your project
// Step 2 : Create following two files .h and .m
// Step 3 : How to Use

// Import your custom file
#import "myDrawingView.h"

// Create object and add to your viewcontroller
myDrawingView *obj = [[myDrawingView alloc] initWithFrame:self.view.frame];
[obj drawSquaresWithNumber:17 andSize:self.view.frame.size];
[self.view addSubview:obj];

//------------------------------------------------------
// filename : myDrawingView.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>

@interface myDrawingView : UIView
{
    int nSquares;
    CGSize mazeSize;
}

- (void)drawSquaresWithNumber:(int)numberOfSquares andSize:(CGSize)screenSize;

@end
//------------------------------------------------------
    // filename : myDrawingView.m
#import "myDrawingView.h"

@implementation myDrawingView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code

    // Calculate height and width for each sqaure.
    float area = mazeSize.height*mazeSize.width;

    float squareSize = (area/nSquares)/100;
    NSLog(@"row : %f %f",mazeSize.width/squareSize,mazeSize.height/squareSize);

    int row, col;
    row = ceil(mazeSize.width/squareSize);
    col = ceil(mazeSize.height/squareSize);

    float height = mazeSize.height/row;
    float width = mazeSize.width/col;
    NSLog(@"%d %d",row,col);
    NSLog(@"h %f w %f",height,width);
    NSLog(@"square size : %f",squareSize);
    // Create Current Context To Draw
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Draw Line
    CGContextSetLineWidth(context, 0.5f);
    CGContextSetFillColorWithColor(context, [myDrawingView randomColor].CGColor);
    CGContextSetStrokeColorWithColor(context, [myDrawingView randomColor].CGColor);

    int x ,y, cnt;
    x = y = 0;
    cnt = 1;
    // A loop for number of squares

    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            if(cnt<=nSquares)
            {
                CGRect rect = CGRectMake(x, y, width, height);
                // Draws Squares
                UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
                // To draw Oval uncomment
//                    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
                [path fill];
                [path stroke];
            }
            x += width;
            cnt++;
        }
        x = 0;
        y += height;
    }
}

+ (UIColor *) randomColor {

    CGFloat red =  arc4random()%256;
    CGFloat blue = arc4random()%256;
    CGFloat green = arc4random()%256;
    return [UIColor colorWithRed:abs(red)/255.0f green:abs(green)/255.0f blue:abs(blue)/255.0f alpha:1.0];
}

- (void)drawSquaresWithNumber:(int)numberOfSquares andSize:(CGSize)screenSize
{
    nSquares = numberOfSquares;
    mazeSize = screenSize;
    [self setNeedsDisplay];
}

@end
于 2013-03-22T12:32:39.497 に答える