没有触发任何触摸!

发布于 2024-10-08 03:50:00 字数 5961 浏览 6 评论 0原文

好吧,我想我之前的问题不够详细。 我正在提供更多细节。

在发布完整的源代码之前,我必须问我的问题: 我的问题是为什么下面的程序不调用touchesBegan、touchesMoved和touchesEnded?它曾经在以前的 iOS SDK 中工作。我当前的 xcode 版本是 3.2.5

一条评论:如果您在 iPad 模拟器上运行以下代码,则它会调用所有触摸。所以这很奇怪,为什么 iPhone 模拟器和实际设备没有响应这个问题。

ma​​in.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

这只是一个测试用例。我主要关心的是为什么使用新的 SDK,触摸不会被调用?请不要告诉我必须使用 NSTimer 或其他计时器。在安装新的 SDK 之前我做了一些工作。我只是想知道是否有人可以看到该源代码中除此之外的任何问题。

谢谢, 孟菲斯

OK, I guess my previous question wasn't in detail.
I'm providing more details.

Before posting full source code I have to ask if my question:
My question is Why the following program doesn't call touchesBegan, touchesMoved and touchesEnded? It used to work in previous iOS SDK. My current xcode version is 3.2.5

One comment: The following code dose call all touches if you run it against iPad Simulator. So that's very odd why iPhone simulator and the actual device don't respond to the problem.

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

This is only a test case. my main concern is why with new SDK, touches won't be called? Please don't tell me I have to use NSTimer or other timers. I did work before I install the new SDK. I'm just wondering if anyone can see any problem in that source code other than that.

Thanks,
Memphis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2024-10-15 03:50:12

我不确定你的问题是什么。但是,我确实注意到,您没有视图控制器 - 虽然您的 UIApplicationDelegate 子类具有 mp_viewController 成员,但它是 UIView 而不是 UIViewController。这可能是您问题的根源。

我建议添加与您的视图关联的 UIViewController 并将其分配给应用程序委托的 rootViewController 属性。

I don't know with certainty what your problem is. I do note, however, that you don't have a view controller--while your UIApplicationDelegate subclass has a mp_viewController member, it's a UIView instead of a UIViewController. It is possible that this is the source of your problem.

I suggest adding a UIViewController associated with your view and assigning it to the application delegate's rootViewController property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文