2016-12-16 9 views
0

私はBig Nerd Ranch Guide(第4版、Objective Cが欲しい)のiOSプログラミングの第5章を見ていたので、UIViewクラスをサブクラス化し、 AppDelegateでは、サブビューでtouchesBeganというイベントが発生していますが、ですが、AppDelegateが信号を受け取りました。touches UIViewのサブクラスの代わりにAppDelegateで捕捉された

didFinishLaunchingWithOptionsAppDelegateにおける方法

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ための2つの初期化方法は、次のよう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まず、makeKeyAndVisiblekeyWindowwindowを設定します、そして、あなたのwindowsの全ての前面にwindowをもたらすでしょうmakeKeyAndVisible必要があります。

- (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をYESに設定しましたが、既存の回答は役に立ちませんでした。私は初心者ですが、私にとって怪しいのはAppDelegateでウィンドウがどのように起動されたかです。 – TPWang

+0

あなたはこれらのものを表示していません(あなたのtouchesBeganメソッド、またはuserInteractionEnabledをtrueに設定したコード)。 –

+0

あなたのコードは、アプリケーションウィンドウのルートビューコントローラを割り当てても初期化しないビューコントローラに設定しています。間違っている。なぜアプリケーションウィンドウに直接ビューをインストールしようとしているのですか?そして、ルートビューコントローラが設定される方法を混乱させるのはなぜですか? –

関連する問題