我的主视图似乎没有连接到实际的屏幕

时间:2013-12-07 13:18:00

标签: ios xcode ios-simulator

我正在开展一场大型比赛,但我似乎无法上手。 当我运行此代码时,我可以看到它到达主视图,但当我使用模拟器时,我只是得到一个空白图像。有什么建议? 此外,我从另一个项目拖动主视图,因为似乎没有人知道如何创建MainView

#import "JKGAppDelegate.h"

@implementation JKGAppDelegate

 @synthesize window = _window;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
mainView = [[MainView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
mainView.backgroundColor = [UIColor grayColor];
[self.window addSubview: mainView];
[self.window makeKeyAndVisible];
return YES;
}

- (void) startGameLoop {
NSString *deviceOS = [[UIDevice currentDevice] systemVersion];
bool forceTimerVariant = TRUE;

if (forceTimerVariant || [deviceOS compare: @"3.1" options: NSNumericSearch] == NSOrderedAscending) {
    //33 frames per second -> timestep between the frames = 1/33
    NSTimeInterval fpsDelta = 0.0303;
    timer = [NSTimer scheduledTimerWithTimeInterval: fpsDelta
                                             target: self
                                           selector: @selector( loop )
                                           userInfo: nil
                                            repeats: YES];

} else {
    int frameLink = 2;
    timer = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget: self selector: @selector( loop )];
    [timer setFrameInterval: frameLink];
    [timer addToRunLoop: [NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
}

NSLog(@"Game Loop timer instance: %@", timer);
}

- (void) stopGameLoop {
[timer invalidate];
timer = nil;
}

- (void) loop {
[mainView setNeedsDisplay]; //triggers MainView's drawRect:-method





}

- (void) applicationDidBecomeActive: (UIApplication *) application {
[self startGameLoop];
}

- (void) applicationWillResignActive: (UIApplication *) application {
[self stopGameLoop];
 }

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

- (void) dealloc {
[self stopGameLoop];

}

@end





#import "MainView.h"

int W=1024;
int H=768;

@implementation MainView

-(int) getRndBtw: (int) bottom and: (int) top{
int rnd = bottom + (arc4random()% (top+1-bottom));
return rnd;
}
- (void) drawRect: (CGRect) rect {      
 W = rect.size.width; 
H = rect.size.height;

static int cnt = 0;
cnt++;      
NSLog(@"Game Loop: %i", cnt); 

//if (!carBlueImage) {
carBlueImage = [UIImage imageNamed: @"derp.png"];
static int y = 0;
y += 3;   
if (y > H) y = -100;
[carBlueImage drawAtPoint: CGPointMake(y, H/2)];
for(int i = 0; i < 50; i++){
    int x;
    int y;
    x = [self getRndBtw:0 and:W];
    y = [self getRndBtw:0 and:H];
    [carBlueImage drawAtPoint:CGPointMake(x, y)];
    x = [self getRndBtw:0 and:H];
    y = [self getRndBtw:0 and:W];
    NSLog(@"ADT");
    [carBlueImage drawAtPoint:CGPointMake(x, y)];
    CGContextRef gc = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(gc, 1, 1, 1, 1);
    CGContextMoveToPoint(gc, 0, 0);
    CGContextAddLineToPoint(gc, W, H);
    CGContextStrokePath(gc);
}

NSMutableArray *arrayOfImageViews = [NSMutableArray array];
for (int i = 0; i < 30; i++) {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map.png"]];
    //[self.view addSubview:imageView];

    [arrayOfImageViews addObject:imageView];
}

}

@end

1 个答案:

答案 0 :(得分:0)

<强> JKGAppDelegate.h

@property(nonatomic,strong) UIViewController *viewController;

<强> JKGAppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  mainView = [[MainView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
  mainView.backgroundColor = [UIColor grayColor];
  self.viewController = [[UIViewController alloc] initWithNibName:@"viewController"];
  [self.viewController.view addSubview:mainView];
  self.window.rootViewController = self.viewController;
  [self.window addSubview: mainView];
  [self.window makeKeyAndVisible];
  return YES;
}
相关问题