我用File/New Projet/View-Based應用程序創建了一個非常基本的iPhone應用程序。 那裏沒有NIB文件。iPhone SDK新功能:touchesBegan不叫
這裏是我的appDelegate
.H
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyViewController *viewController;
}
.M
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
這裏是我的loadView方法在我的控制器現在
- (void)loadView {
CGRect mainFrame = [[UIScreen mainScreen] applicationFrame];
UIView *contentView = [[UIView alloc] initWithFrame:mainFrame];
contentView.backgroundColor = [UIColor redColor];
self.view = contentView;
[contentView release];
}
,爲了抓住頭部chesBegan事件,我創建的UIView的一個新的子類:
.H
@interface TouchView : UIView {
}
.M
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touch detected");
}
和修改我的loadView第二行成這樣:
TouchView *contentView = [[UIView alloc] initWithFrame:mainFrame];
touchesBegan爲什麼從不叫?
非常接近。顯然,UIViewController在UIView之前獲取touchesBegan/touchesEnded/etc消息*。 UIViewController默認不會將消息傳遞給UIView(通過調用[super touchesBegan])。所以你需要移動或轉發觸摸*函數*如果你的UIView有一個控制器。它令人困惑,從未被正確記錄,但這是傳統的可可方式。 – Dave 2010-03-04 01:03:29
這是絕對錯誤的好友...它也被稱爲UIView類以及.. http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/ OCC/instm/UIResponder/touchesMoved:withEvent:方法 – harshitgupta 2012-12-10 14:50:30