Big Nerd Ranch Guide (제 4 판, Objective C가 필요했습니다)의 iOS 프로그래밍의 5 장을 살펴보고 UIView
클래스의 서브 클래 싱 방법에 따라 서브 뷰를 추가했습니다. AppDelegate
에서 하위보기가 touchesBegan
이벤트,을 수신하지만 AppDelegate
이 신호를 수신 중임을 나타냅니다. AppDelegate
에touchesBegan이 UIView의 하위 클래스 대신 AppDelegate에 걸렸습니다
didFinishLaunchingWithOptions
있어서 다음 HypnosisView
위한
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:[UIViewController alloc]];
CGRect firstFrame = self.window.bounds;
HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame];
[self.window addSubview:firstView];
[firstView becomeFirstResponder];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
개의 초기화 방법 UIView
의 서브 클래스 정의된다
#import "HypnosisView.h"
@interface HypnosisView()
@property (strong, nonatomic) UIColor *circleColor;
@end
@implementation HypnosisView
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
CGRect bounds = self.bounds;
CGRect frame = self.frame;
// Figure out the center of the bounds rectangle
CGPoint center;
center.x = frame.origin.x + frame.size.width/2.0;
center.y = frame.origin.y + frame.size.height/2.0;
// The largest circle will circumscribe the view
float maxRadius = hypot(bounds.size.width, bounds.size.height)/2.0;
UIBezierPath *path = [[UIBezierPath alloc] init];
for (float currentRadius = maxRadius; currentRadius > 0; currentRadius -= 20) {
[path moveToPoint:CGPointMake(center.x + currentRadius, center.y)];
[path addArcWithCenter:center
radius:currentRadius
startAngle:0.0
endAngle:M_PI * 2
clockwise:YES];
}
// Configure line with to 10 points
path.lineWidth = 10;
// Configure the drawing color to light gray
[self.circleColor setStroke];
// Draw the line!
[path stroke];
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// All HypnosisViews start with a clear background color
self.backgroundColor = [UIColor clearColor];
self.circleColor = [UIColor lightGrayColor];
self.userInteractionEnabled = YES;
}
return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSLog(@"%@ was touched", self);
}
코드가 책에서 비롯된 것이라면 여기에 게시해서는 안됩니다. 그것은 그들의 지적 재산권입니다. –