2016-12-16 18 views
0

Big Nerd Ranch Guide (제 4 판, Objective C가 필요했습니다)의 iOS 프로그래밍의 5 장을 살펴보고 UIView 클래스의 서브 클래 싱 방법에 따라 서브 뷰를 추가했습니다. AppDelegate에서 하위보기가 touchesBegan 이벤트,을 수신하지만 AppDelegate이 신호를 수신 중임을 나타냅니다. AppDelegatetouchesBegan이 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); 
} 
+0

코드가 책에서 비롯된 것이라면 여기에 게시해서는 안됩니다. 그것은 그들의 지적 재산권입니다. –

답변

1

, 당신은 당신의 window 첫째, makeKeyAndVisiblewindowkeyWindow에 설정됩니다 makeKeyAndVisible해야하고 windows의 모든 전면에 window을 가져올 것이다.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    [self.window setRootViewController:[UIViewController alloc]]; 

    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 

    CGRect firstFrame = self.window.bounds; 

    HypnosisView *firstView = [[HypnosisView alloc] initWithFrame:firstFrame]; 
    [self.window addSubview:firstView]; 
    [firstView becomeFirstResponder]; 



    return YES; 
} 
+0

고마워요! 그게 효과가 있었어! 그러나 배경색을 설정하는 순서가 다른 이유는 무엇입니까? – TPWang

+0

@TPWang은 배경색을 설정하지 않지만'[self.window addSubview : firstView];', 내 편집 된 답변을 참조하십시오. – aircraft

-1

UIView의 오브젝트는 일반적으로 터치 이벤트에 반응하지 않는다. 보기에 대해 userInteractionEnabled 플래그를 true로 설정 했습니까?

그리고 touchesBegan 메서드는 호출되지 않습니다. 당신의 Appdelegate.m에서

+0

호출해야하는 touchesBegan 함수를 추가했으나 HypnosisView가 이벤트에 영향을 미치지는 않지만 AppDelegate에서 같은 함수가 호출되었습니다. 이것이 이해할 수없는 것입니다. 그리고 예를 들어 userInteractionEnabled를 설정했는데 기존 답변이 도움이되지 않았기 때문에 저는 초보자입니다.하지만 필자에게보기 엔 AppDelegate에서 창을 시작하는 방법이 있습니다. – TPWang

+0

사용자가 touchesBegan 메소드 또는 true로 설정 한 코드를 표시하지 않았습니다. –

+0

코드는 앱 윈도우의 루트보기 컨트롤러를 할당하지만 초기화하지 않는보기 컨트롤러로 설정하고 있습니다. 잘못된 것입니다. 왜 앱 윈도우에 뷰를 직접 설치하려고 시도하고 루트 뷰 컨트롤러가 설정되는 방식을 망쳐 놓은 채로 당신은 주변에 머물고 있습니까? –