2010-12-14 17 views
1

OK, ich denke, meine vorherige Frage war nicht im Detail. Ich biete weitere Details an.Keine Berührungen ausgelöst!

Bevor ich den vollständigen Quellcode posten kann, muss ich fragen, ob meine Frage: Meine Frage ist Warum das folgende Programm nicht touchesBegan, touchesMoved und touchesEnded aufruft? Es funktionierte früher im iOS-SDK. Meine aktuelle xcode-Version ist 3.2.5

Ein Kommentar: Der folgende Code Dosis alle Berührungen anrufen, wenn Sie es gegen iPad Simulator ausführen. Das ist sehr merkwürdig, warum der iPhone-Simulator und das eigentliche Gerät nicht auf das Problem reagieren.

main.m

#import <UIKit/UIKit.h> 
int main(int argc, char *argv[]) { 

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate"); 
    [pool release]; 
    return retVal; 
} 

AppDelegate.h

#import <UIKit/UIKit.h> 
@class GLView; 

@interface AppDelegate : NSObject <UIApplicationDelegate> 
{ 
    UIWindow* mp_window; 
    GLView*  mp_viewController; 
} 

@end 

AppDelegate.mm

#import "AppDelegate.h" 
#import "GLView.h" 

@implementation AppDelegate 

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

    mp_window = [[UIWindow alloc] initWithFrame: screenBounds]; 
    mp_viewController = [[GLView alloc] initWithFrame: screenBounds]; 


    [mp_window addSubview: mp_viewController]; 
    [mp_window makeKeyAndVisible]; 

    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{} 

- (void)applicationWillTerminate:(UIApplication *)application 
{} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    // Handle any background procedures not related to animation here. 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    // Handle any foreground procedures not related to animation here. 
} 

- (void)dealloc 
{ 
    [mp_viewController release]; 
    [mp_window release]; 

    [super dealloc]; 
} 

@end 

GLView.h

#import <OpenGLES/EAGL.h> 
#import <QuartzCore/QuartzCore.h> 

@interface GLView : UIView <UIAccelerometerDelegate> 
{ 
@private 
    EAGLContext*  mp_context; 
} 

- (void) gameLoop; 

- (void) drawView: (float) interpolation; 

- (void)touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event; 
- (void)touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event; 
- (void)touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event; 

- (void)accelerometer: (UIAccelerometer *) accelerometer 
     didAccelerate: (UIAcceleration *) acceleration; 

@end 

GLView.mm

#import "GLView.h" 
#import <OpenGLES/ES2/gl.h> 

@implementation GLView 

+ (Class) layerClass 
{ 
    return [CAEAGLLayer class]; 
} 

- (id) initWithFrame: (CGRect) frame 
{ 
    if (self = [super initWithFrame: frame]) 
    { 
     CAEAGLLayer* eaglLayer = (CAEAGLLayer*) super.layer; 
     eaglLayer.opaque = YES; 

     EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES1; 
     mp_context = [[EAGLContext alloc] initWithAPI: api]; 

     if (!mp_context || ![EAGLContext setCurrentContext: mp_context]) 
     { 
      [self release]; 
      return nil; 
     } 

     [mp_context renderbufferStorage: GL_RENDERBUFFER 
          fromDrawable: eaglLayer]; 

     //TODO: to setup the size of frame for render 
     ////// 

     //acc 
     [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(0.1f)]; 
     [[UIAccelerometer sharedAccelerometer] setDelegate:self]; 

     //multi - touch 
     [self setUserInteractionEnabled:YES]; 
     [self setMultipleTouchEnabled:YES]; 


     [self performSelectorOnMainThread:@selector(gameLoop) 
           withObject:nil waitUntilDone:NO]; 
    } 

    return self; 
} 

- (void) gameLoop 
{ 
    while(1) 
    { 
      //Get all touches 
     while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 
        0.002f, 
        TRUE) == kCFRunLoopRunHandledSource); 
      //render scene 
      [drawView 1.0f]; 
    } 
} 

- (void) drawView: (float) interpolation 
{ 
    //TODO: adding render image here 
    //NSLog(@"Render: %f", interpolation); 
    [mp_context presentRenderbuffer: GL_RENDERBUFFER]; 
} 

- (void) dealloc 
{ 

    if ([EAGLContext currentContext] == mp_context) 
     [EAGLContext setCurrentContext: nil]; 


    [mp_context release]; 
    //TODO: relese all objects here 
    [super dealloc];  
} 

- (void)touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event 
{ 
    int hash; 
    CGPoint points; 
    for(UITouch * touch in touches) 
    { 
     hash = [touch hash]; 
     points = [touch locationInView: self]; 
     NSLog(@"Touch Began: %d, %f, %f", hash, points.x, points.y); 
    } 
} 


- (void)touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event 
{ 
    int hash; 
    CGPoint points; 
    for(UITouch * touch in touches) 
    { 
     hash = [touch hash]; 
     points = [touch locationInView: self]; 
     NSLog(@"Touch Moved: %d, %f, %f", hash, points.x, points.y); 
    } 
} 


- (void)touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event 
{ 
    int hash; 
    CGPoint points; 
    for(UITouch * touch in touches) 
    { 
     hash = [touch hash]; 
     points = [touch locationInView: self]; 
     NSLog(@"Touch Ended: %d, %f, %f", hash, points.x, points.y); 
    } 
} 

- (void) accelerometer: (UIAccelerometer *) accelerometer 
     didAccelerate: (UIAcceleration *) acceleration 
{ 
    NSLog(@"Accelerometer: (%f, %f, %f)", 
      acceleration.x, 
      acceleration.y, 
      acceleration.z); 
} 

@end 

Dies ist nur ein Testfall. Mein Hauptanliegen ist, warum mit neuen SDK, Berührungen nicht aufgerufen werden? Bitte sag mir nicht, dass ich NSTimer oder andere Timer benutzen muss. Ich habe gearbeitet, bevor ich das neue SDK installiere. Ich frage mich nur, ob jemand anderes Problem in diesem Quellcode sehen kann.

Danke, Memphis

Antwort

0

Ich weiß es nicht mit Sicherheit, was dein Problem ist. Ich bemerke jedoch, dass Sie keinen View-Controller haben - während Ihre UIApplicationDelegate-Subklasse ein Mitglied von mp_viewController hat, ist es eine UIView anstelle eines UIViewControllers. Es ist möglich, dass dies die Ursache Ihres Problems ist.

Ich schlage vor, einen UIViewController hinzuzufügen, der mit Ihrer Ansicht verknüpft ist und ihn der rootViewController-Eigenschaft des Anwendungsdelegaten zuweist.

+0

Würden Sie bitte einen einfachen Quellcode für mich erstellen. Eine andere Sache, die wirklich nervig ist, warum funktioniert es in iPad? – Memphis

Verwandte Themen