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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9344556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:20:47  来源:igfitidea点击:

Drawing a circle after button click in xcode

iphonexcode

提问by user1217340

I am trying to draw a circle after clicking a button. I can create a rectangle but cannot draw a circle.

单击按钮后,我试图画一个圆圈。我可以创建一个矩形,但不能画一个圆。

Is there a way to make this:

有没有办法做到这一点:

-(IBAction) buttonTouched 
{
}

call or signal this to work:

调用或发出信号使其工作:

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

Thanks for any input!

感谢您提供任何意见!

*Edit**Sorry, it's not working. Not sure what I'm doing wrong:

*编辑* *抱歉,它不起作用。不知道我做错了什么:

1) Created a new view-based project named "test". 2) Created a Objective-C class UIView named "view". 3) In IB dragged a button onto the view, linked it to "buttonTouched" - Touched Up Inside.

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

回答by BDGapps

This should do what you want it to. Check 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);

回答by rob mayoff

If you want the system to call drawRect:, you call setNeedsDisplay:

如果您希望系统调用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];
}

回答by SachinVsSachin

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