在xcode中单击按钮后绘制一个圆圈

时间:2012-02-18 22:04:38

标签: iphone 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

3 个答案:

答案 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);

答案 1 :(得分: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];
}

答案 2 :(得分: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);
}