Objective c 如何使用控制器内部的iAction在自定义视图中绘制圆?
我绘制了一个通用视图,并将其连接到circleView.m。然后,我在视图顶部拖出一个圆形的rect按钮,并将一个iAction连接到它。从现在起,当视图加载时,圆将自动绘制到屏幕上。我想做的是只在使用drawRect或其他绘制方法按下按钮时在屏幕上绘制圆。这是我的密码: drawCircleViewController.hObjective c 如何使用控制器内部的iAction在自定义视图中绘制圆?,objective-c,draw,ibaction,Objective C,Draw,Ibaction,我绘制了一个通用视图,并将其连接到circleView.m。然后,我在视图顶部拖出一个圆形的rect按钮,并将一个iAction连接到它。从现在起,当视图加载时,圆将自动绘制到屏幕上。我想做的是只在使用drawRect或其他绘制方法按下按钮时在屏幕上绘制圆。这是我的密码: drawCircleViewController.h #import <UIKit/UIKit.h> @interface drawCircleViewController : UIViewController
#import <UIKit/UIKit.h>
@interface drawCircleViewController : UIViewController
@end
circleView.h
#import <UIKit/UIKit.h>
@interface circleView : UIView
@end
#导入
@接口电路视图:UIView
@结束
circleView.m
#import "circleView.h"
@implementation circleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawCircleAtPoint:(CGPoint)p
withRadius:(CGFloat)radius
inContext:(CGContextRef)context
{
UIGraphicsPushContext(context);
CGContextBeginPath(context);
CGContextAddArc(context, p.x, p.y, radius, 0, 2*M_PI, YES);
CGContextStrokePath(context);
UIGraphicsPopContext();
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint midpoint;
midpoint.x = self.bounds.origin.x + self.bounds.size.width / 2;
midpoint.y = self.bounds.origin.y + self.bounds.size.height / 2;
#define DEFAULT_SCALE 0.90
CGFloat size = self.bounds.size.width / 2;
if (self.bounds.size.height < self.bounds.size.width) size = self.bounds.size.height / 2;
size *= DEFAULT_SCALE;
CGContextSetLineWidth(context, 5.0);
[[UIColor blueColor] setStroke];
[self drawCircleAtPoint:midpoint withRadius:size inContext:context];
}
@end
#导入“circleView.h”
@实施概况
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
}
回归自我;
}
-(无效)drawCircleAtPoint:(CGPoint)p
withRadius:(CGFloat)半径
inContext:(CGContextRef)上下文
{
UIGraphicsPushContext(context);
CGContextBeginPath(上下文);
CGContextAddArc(上下文,p.x,p.y,半径,0,2*M_PI,是);
CGContextStrokePath(上下文);
UIGraphicsPopContext();
}
-(void)drawRect:(CGRect)rect
{
CGContextRef context=UIGraphicsGetCurrentContext();
CGPoint中点;
middpoint.x=self.bounds.origin.x+self.bounds.size.width/2;
middpoint.y=self.bounds.origin.y+self.bounds.size.height/2;
#定义默认的_比例0.90
CGFloat size=self.bounds.size.width/2;
如果(self.bounds.size.height
根据您所拥有的,最简单的方法可能是隐藏圆形视图,并在按下按钮时显示
否则,您可以在视图中保留一个布尔值,以指示按钮是否已被点击,并在drawRect期间检查:(并使用setNeedsDisplay触发更改)。可以尝试使用您用于在视图上首先绘制圆的代码(自动绘制圆的代码)并将其移动到IBAction方法中?
#import "circleView.h"
@implementation circleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawCircleAtPoint:(CGPoint)p
withRadius:(CGFloat)radius
inContext:(CGContextRef)context
{
UIGraphicsPushContext(context);
CGContextBeginPath(context);
CGContextAddArc(context, p.x, p.y, radius, 0, 2*M_PI, YES);
CGContextStrokePath(context);
UIGraphicsPopContext();
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint midpoint;
midpoint.x = self.bounds.origin.x + self.bounds.size.width / 2;
midpoint.y = self.bounds.origin.y + self.bounds.size.height / 2;
#define DEFAULT_SCALE 0.90
CGFloat size = self.bounds.size.width / 2;
if (self.bounds.size.height < self.bounds.size.width) size = self.bounds.size.height / 2;
size *= DEFAULT_SCALE;
CGContextSetLineWidth(context, 5.0);
[[UIColor blueColor] setStroke];
[self drawCircleAtPoint:midpoint withRadius:size inContext:context];
}
@end