2013-03-22 53 views
1

我有一個項目,我必須在按鈕上創建一個正方形和圓圈。如何在iPhone上點擊「方塊」和「圓圈」按鈕?

有按鈕前面的textfileds,在textfiled上我們給出的值如45,點擊uibutton時動作執行,45 square會自動調整到iphone scfreen本身。

假設屏幕尺寸爲320 * 480,所以正方形會自動隨屏幕調整。

如果我們在文本文件上給出值300,則300個正方形將在屏幕上自動創建和調整。

它會顯示類似方格紙在一個階段,如果我們給像1500

值我沒有任何想法如何做到這一點,如何啓動和從哪裏開始。

我只是在想,它會使用Quartcore Framework,但我沒有從我從哪裏開始項目的任何想法,我搜索。

我想要專家的建議和想法。 任何想法或專家的建議將非常受歡迎。

+0

先生這個項目分配給我,我不知道從哪裏開始。 – 2013-03-22 11:17:04

回答

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 
+0

我們把這些線放在哪裏? myDrawingView * obj = [[myDrawingView alloc] initWithFrame:self.view.frame]; [obj drawSquaresWithNumber:17 andSize:self.view.frame.size]; [self.view addSubview:obj]; – 2013-03-25 10:18:13

+0

把你的viewController的viewDidLoad方法 – 2013-03-25 10:19:06

+0

工作與否..! – 2013-03-25 10:22:06

0

創建一個按鈕,一個標籤和2個文本框(一個用於高度,一個用於寬度)。在標籤上設置1像素邊框,並將標籤的寬度和高度設置爲屏幕尺寸。爲該按鈕創建一個IBAction,並根據文本字段內的內容分配標籤的寬度和高度。這將創建廣場。要創建圓,您可以使用QuartzCore並將拐角半徑設置爲高度/寬度的一半,或者可以通過編程方式進行繪製。這是實現你所要求的最快捷的方式,而不會過多地使用GL在屏幕上繪圖,這對於理解動作和輸入如何工作是一個好的開始。

相關問題