-1
일반보기를 드래그하여 내 circleView.m에 연결했습니다. 그런 다음 그 뷰 위에 둥근 rect 버튼을 드래그하고 IBAction을 연결했습니다. 현재로서는 뷰가로드되면 원이 자동으로 화면에 그려집니다. 내가 뭘하고 싶은가 drawRect 또는 다른 무승부 방법을 사용하여 버튼을 누르면 화면에 원을 그립니다. 여기 내 코드입니다 :내 컨트롤러에서 IBAction을 사용하여 내 사용자 정의보기에서 원을 그리려면 어떻게해야합니까?
drawCircleViewController.h
#import <UIKit/UIKit.h>
@interface drawCircleViewController : UIViewController
@end
drawCircleViewController.m
#import "drawCircleViewController.h"
#import "circleView.h"
@interface drawCircleViewController()
@property (nonatomic, weak) IBOutlet circleView *circleV;
@end
@implementation drawCircleViewController
@synthesize circleV = _circleV;
- (IBAction)buttonPressedToDrawCircle:(id)sender {
// This is action I want to use to draw the circle in my circleView.m
}
@end
circleView.h
#import <UIKit/UIKit.h>
@interface circleView : UIView
@end
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
어쩌면 처음에 뷰 (원을 그리는 원)에서 원을 그리는 데 사용한 코드를 가져 와서 IBAction 메소드로 옮기시겠습니까? – David