2012-02-18 4 views
1

버튼을 클릭 한 후 원을 그리려합니다. 직사각형을 만들 수는 있지만 원을 그릴 수는 없습니다.xcode에서 버튼 클릭 후 원 그리기

이 할 수있는 방법이 있나요 :

-(IBAction) buttonTouched 
{ 
} 

전화 또는이 작업을 신호 : 모든 입력에 대한

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

감사합니다!

* 편집 * * 죄송합니다, 그것은 작동하지 않습니다. 내가 뭘 잘못하고 있는지 확실하지 :

1) "test"라는 새로운보기 기반 프로젝트를 만들었습니다. 2) "view"라는 Objective-C 클래스 UIView를 생성했습니다. 3) IB가보기에 단추를 끌어서 "buttonTouched"에 연결했습니다 - 내부를 만졌습니다. 이 시스템이 drawRect:를 호출 할 경우

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 하위 클래스를 말할 때 클래스 - 새 파일 -> objective-c class-UIView를 마우스 오른쪽 버튼으로 클릭하여 만든 것입니까? – user1217340

+0

예 객관적인 - c 클래스 - UIView입니다. – BDGapps

+0

나는 따라하기 쉬운 짧은 프로젝트를 작성했다. http://www.mediafire.com/?d9hkhmuj32vlrlv – BDGapps

1

, 당신은 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); 
}