2016-12-16 32 views
0

我正在閱讀The Big Nerd Ranch Guide(第4版,想要Objective C)的iOS編程第5章,我遵循子類化UIView類的說明並添加了子視圖在AppDelegate,事情是子視圖不捕獲touchesBegan事件,AppDelegate捕捉信號。touchesBegan被AppDelegate捕獲而不是UIView的子類

didFinishLaunchingWithOptions方法在AppDelegate

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; 

HypnosisView兩個初始化方法中,子類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

在你Appdelegate.m,你應該makeKeyAndVisiblewindow首先,makeKeyAndVisible將設置windowkeyWindow,並會帶來window到所有windows的前面。

- (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方法沒有被調用?

+0

我已經添加了應該被調用的touchesBegan函數,但不是,HypnosisView沒有得到觸摸事件,但在AppDelegate中調用了相同的函數,這是我不明白。 而且我確實將userInteractionEnabled設置爲YES,順便說一句,現有的答案都沒有幫助,我是一個初學者,但對我來說看起來很腥的是窗口是如何在AppDelegate中啓動的,您可以看一下嗎? – TPWang

+0

您沒有顯示任何這些東西(您的touchesBegan方法或將userInteractionEnabled設置爲true的代碼)。 –

+0

您的代碼還將應用程序窗口的根視圖控制器設置爲您分配但未初始化的視圖控制器,是錯的。爲什麼你會試圖直接在應用程序窗口中安裝視圖,並搞亂了根視圖控制器的設置? –

相關問題