【发布时间】:2013-07-23 21:29:57
【问题描述】:
单击按钮后,我试图画一个圆圈。我可以创建一个矩形,但不能画一个圆。
有没有办法做到这一点:
-(IBAction) buttonTouched
{
}
打电话或发信号让它工作:
- (void)drawRect:(CGRect)rect
{
}
感谢您的任何意见!
*编辑** 抱歉,它不起作用。不知道我做错了什么:
1) 创建了一个名为“test”的新的基于视图的项目。 2)创建了一个名为“view”的Objective-C类UIView。 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
【问题讨论】:
-
你试过什么?向我们展示创建矩形的代码。向我们展示尝试创建圆圈的代码。