Рисование круга после нажатия кнопки в xcode - PullRequest
1 голос
/ 19 февраля 2012

Я пытаюсь нарисовать круг после нажатия кнопки. Я могу создать прямоугольник, но не могу нарисовать круг.

Есть ли способ сделать это:

-(IBAction) buttonTouched 
{
}

позвоните или подайте сигнал на работу:

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

Спасибо за любой вклад!

* Изменить ** Извините, это не работает. Не уверен, что я делаю не так:

1) Создан новый проект на основе представления с именем «test». 2) Создан класс Objective C UIView с именем «view». 3) В IB перетащили кнопку на вид, связали ее с «buttonTouched» - Touched Up Inside.

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

Ответы [ 3 ]

6 голосов
/ 19 февраля 2012

Это должно делать то, что вы хотите.Проверьте Пример кода

     // 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);
1 голос
/ 19 февраля 2012

Если вы хотите, чтобы система звонила 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 голосов
/ 08 октября 2012
-(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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...