2012-02-18 38 views
1

我試圖在單擊按鈕後繪製一個圓。我可以創建一個矩形,但不能繪製一個圓。在xcode按鈕單擊後畫一個圓圈

有沒有辦法讓這個:

-(IBAction) buttonTouched 
{ 
} 

來電或信號這項工作:

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

感謝任何投入!

*編輯* * 對不起,它不工作。不知道我在做什麼錯:

1)創建一個名爲「測試」的新的基於視圖的項目。 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

你嘗試過什麼?向我們展示創建矩形的代碼。向我們展示嘗試創建圈子的代碼。 – 2012-02-18 22:10:59

回答

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 2012-02-19 06:27:42

+0

是的,它是objective-c類-UIView。 – BDGapps 2012-02-19 12:43:03

+0

我已經爲你寫了一個簡短的項目,這很容易遵循。 http://www.mediafire.com/?d9hkhmuj32vlrlv – BDGapps 2012-02-19 18:24:27

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); 
}