#import "AppDelegate.h"app
@interface AppDelegate ()ide
@end ui
@implementation AppDelegatethis
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {spa
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];.net
self.window.backgroundColor = [UIColor whiteColor];rest
self.window.rootViewController = [[UIViewController alloc] init];orm
self.window.rootViewController.view.userInteractionEnabled = NO;事件
//===================================get
//不能接收事件:感覺不到手指的觸摸(至關於死人)
//能接收事件:能感覺到手指的觸摸
//能並非可以接收事件就能夠響應事件,可是能響應事件的前提是能夠接收事件
//默認狀況下:UIWindow、UIView和UIButton能夠接收事件;UILabel和UIImageView不能夠接受事件。
//1.父視圖不能接收事件,則子視圖沒法接受事件。
//若是原本能夠接收事件的視圖,添加到了不能接收事件的視圖上,那麼原本能夠接收事件的視圖也接收不到事件了
//UIButton能夠接收事件,而且能夠響應事件
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(-30, -30, 80, 80)];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
//window默承認以接收事件,因此添加到window的子視圖原本能夠接收事件的能夠接收事件
[_window addSubview:button];
//UIImageView默認不能夠接收事件
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
imageView.backgroundColor = [UIColor yellowColor];
[_window addSubview:imageView];
//UIImageView默認不能夠接收事件,因此添加到UIImageView上面的子視圖不能接收事件
[imageView addSubview:button];
//設置控件是否能夠接收事件
//YES:能夠進行用戶交互
//NO:不能夠進行用戶交互
imageView.userInteractionEnabled = YES;
//關閉按鈕的用戶交互
// button.userInteractionEnabled = NO;
//2.子視圖超出父視圖的部分,不能接收事件
//3.同一個父視圖下,最上面的視圖,首先遭遇事件,若是可以響應,就不向下傳遞事件。若是不能接收,事件向下傳遞。
UIButton * button2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];
button2.backgroundColor = [UIColor purpleColor];
[button2 addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:button2];
[self.window sendSubviewToBack:button2];
//imageView能夠接收事件,點擊事件讓imageView去接收,imageView下層的按鈕接收不到事件
//imageView不能夠接收事件,會將事件傳遞到下一層
imageView.userInteractionEnabled = NO;
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - 按鈕點擊
- (void)onclicked:(UIButton *)button{
NSLog(@"按鈕被點擊了");
}
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end