2012-02-18 21 views
1

ボタンをクリックした後に円を描こうとしています。私は矩形を作成することはできますが、円を描くことはできません。xcodeでボタンをクリックした後に円を描く

はこれを作るための方法があります:

-(IBAction) buttonTouched 
{ 
} 

コールまたはこれが動作するように合図:任意の入力のための

- (void)drawRect:(CGRect)rect 
{ 
} 

ありがとう!

*編集* * 申し訳ありませんが、それは働いていません。私が間違っていることがわからない:

1) "test"という新しいビューベースのプロジェクトを作成しました。 2) "view"というObjective-CクラスのUIViewを作成しました。 3)IBがビュー上にボタンをドラッグし、それを "buttonTouched"にリンクしました - 内部をタッチしました。

testViewController.h 
#import <UIKit/UIKit.h> 
#import "view.h" 
@interface testViewController : UIViewController { 
} 
-(IBAction) buttonTouched; 
@end 


testViewController.m 
#import "testViewController.h" 
#import "view.h" 
@implementation testViewController 
- (IBAction)buttonTouched { 
    [[self view] setNeedsDisplay]; 
} 

view.m 

#import "view.h" 

@implementation view 

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

- (void)drawRect:(CGRect)rect { 
    CGContextRef contextRef = UIGraphicsGetCurrentContext(); 

    CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0); 

    CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0); 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 
+0

を確認してください、あなたは何を試してみましたか?長方形を作成するコードを表示します。サークルを作成しようとするコードを表示します。 –

答えて

6

これは、あなたがそれが欲しいものを行う必要があります。 Sample code

 // The color is by this line CGContextSetRGBFillColor(context , red , green , blue , alpha); 


    // Draw a circle (filled) 
    CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0); 

    // Draw a circle (border only) 
    CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0); 
+0

あなたはUIViewサブクラスと言うとき、それは、クラス - 新しいファイル - >目的 - cクラス - UIViewを右クリックして作成されたものですか? – user1217340

+0

はいこれはobjective-cクラスのUIViewです。 – BDGapps

+0

私はあなたのために簡単なプロジェクトを書いています。 http://www.mediafire.com/?d9hkhmuj32vlrlv – BDGapps

1

あなたは、システムがdrawRect:を呼び出したい場合は、setNeedsDisplayを呼び出す:

// In your view controller... 
- (IBAction)buttonTouched { 
    [self.view setNeedsDisplay]; 
} 

// In your UIView subclass... 
- (void)drawRect:(CGRect)rect { 
    [UIColor.redColor setFill]; 
    [[UIBezierPath bezierPathWithOvalInRect:self.bounds] fill]; 
} 
0
-(void)drawRect:(CGRect)rect { 
    CGContextRef contextRef = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 0.1); 
    CGContextSetRGBStrokeColor(contextRef, 0, 0, 255, 0.5); 
    // Draw a circle (filled) 
    CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 

    // Draw a circle (border only) 
    CGContextStrokeEllipseInRect(contextRef, CGRectMake(100, 100, 25, 25)); 

    // Get the graphics context and clear it 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    CGContextClearRect(ctx, rect); 

    // Draw a green solid circle 
    CGContextSetRGBFillColor(ctx, 0, 255, 0, 1); 
    CGContextFillEllipseInRect(ctx, CGRectMake(100, 100, 25, 25)); 

    // Draw a yellow hollow rectangle 
    CGContextSetRGBStrokeColor(ctx, 255, 255, 0, 1); 
    CGContextStrokeRect(ctx, CGRectMake(195, 195, 60, 60)); 

    // Draw a purple triangle with using lines 
    CGContextSetRGBStrokeColor(ctx, 255, 0, 255, 1); 
    CGPoint points[6] = { 
     CGPointMake(100, 200), CGPointMake(150, 250), 
     CGPointMake(150, 250), CGPointMake(50, 250), 
     CGPointMake(50, 250), CGPointMake(100, 200) 
    }; 
    CGContextStrokeLineSegments(ctx, points, 6); 
} 
関連する問題