0
這是一個非常奇怪的情況,我從來沒有像這樣的問題,我真的很感謝我在這篇文章中獲得的任何幫助。我做了一個簡單的遊戲。我會給下面的.h和.m。使用NSTimer和動畫的UIImageView和UILabel的奇怪行爲
Game30s.h:
//
// Game30s.h
// Speedy
//
// Created by Ajay Venkat on 15/10/2014.
// Copyright (c) 2014 AJTech. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Game30s : UIViewController {
IBOutlet UILabel *scorelabel;
IBOutlet UIButton *bar1;
IBOutlet UIButton *bar2;
BOOL isCracked;
BOOL isCracked2;
NSTimer *go;
NSTimer *collision;
IBOutlet UIImageView *wall;
IBOutlet UIButton *start;
IBOutlet UIButton *contin;
NSTimer *time;
NSTimer *timestart;
int timem;
IBOutlet UILabel *timemanage;
int score;
}
-(void)tensecs;
-(void)timetwo;
-(void)goRight;
-(void)col;
-(IBAction)Statt;
-(IBAction)Tap1;
-(IBAction)Tap2;
@property (nonatomic) BOOL gameCenterEnabled;
@property (nonatomic, strong) NSString *leaderboardIdentifier;
@end
Game30s.m:
//
// Game30s.m
// Speedy
//
// Created by Ajay Venkat on 15/10/2014.
// Copyright (c) 2014 AJTech. All rights reserved.
//
#import "Game30s.h"
@import GameKit;
@interface Game30s()
@end
@implementation Game30s {
int cracknum;
float speed;
int score2;
int totscore;
}
- (void)viewDidLoad {
[super viewDidLoad];
timestart = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timetwo) userInfo:nil repeats:YES];
contin.hidden = YES;
start.hidden = NO;
bar1.hidden = YES;
bar2.hidden = YES;
[self authenticateLocalPlayer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(IBAction)Tap1 {
int w = ([[UIScreen mainScreen] bounds].size.width);
int h = ([[UIScreen mainScreen] bounds].size.height);
w = w/2;
int r1x = (arc4random() %240) + w ;
int r1y = (arc4random() %h);
bar1.center = CGPointMake(r1x, r1y);
speed = speed + 0.5;
score = score + 1;
}
-(IBAction)Tap2 {
int w = ([[UIScreen mainScreen] bounds].size.width);
int h = ([[UIScreen mainScreen] bounds].size.height);
w = w/2;
int r2x = (arc4random() %240) + w ;
int r2y = (arc4random() %h);
speed = speed + 0.7;
bar2.center = CGPointMake(r2x, r2y);
score = score + 1;
}
-(void)goRight {
bar1.center = CGPointMake(bar1.center.x - speed, bar1.center.y);
bar2.center = CGPointMake(bar2.center.x - speed, bar2.center.y);
}
-(void)tensecs {
if (timem > 0) {
timem = timem - 1;
NSString* myString = [@(timem) stringValue];
timemanage.text = myString;
} else if (timem == 0) {
[timestart invalidate];
NSString* myString = [@(score) stringValue];
scorelabel.text = myString;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:score forKey:@"thirtysecond"];
bar1.hidden = YES;
bar2.hidden = YES;
start.hidden = NO;
start.enabled = YES;
contin.hidden = NO;
contin.enabled = YES;
[go invalidate];
[time invalidate];
}
}
-(void)col {
if (CGRectIntersectsRect(bar1.frame, wall.frame)) {
int r2x = (arc4random() %300) + 300 ;
int r2y = (arc4random() %375);
bar2.center = CGPointMake(r2x, r2y);
int r1x = (arc4random() %300) + 300 ;
int r1y = (arc4random() %375);
bar1.center = CGPointMake(r1x, r1y);
}
if (CGRectIntersectsRect(bar2.frame, wall.frame)) {
int r2x = (arc4random() %300) + 300 ;
int r2y = (arc4random() %375);
bar2.center = CGPointMake(r2x, r2y);
int r1x = (arc4random() %300) + 300 ;
int r1y = (arc4random() %375);
bar1.center = CGPointMake(r1x, r1y);
}
}
-(IBAction)Statt {
timem = 30;
score = 0;
contin.hidden = YES;
contin.enabled = NO;
time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tensecs) userInfo:nil repeats:YES];
bar1.hidden = NO;
bar2.hidden = NO;
int r2x = (arc4random() %300) + 300 ;
int r2y = (arc4random() %375);
bar2.center = CGPointMake(r2x, r2y);
int r1x = (arc4random() %300) + 300 ;
int r1y = (arc4random() %375);
bar1.center = CGPointMake(r1x, r1y);
cracknum = 0;
go = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(goRight) userInfo:nil repeats:YES];
collision = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(col) userInfo:nil repeats:YES];
speed = 3;
start.hidden = YES;
start.enabled = NO;
}
-(void)authenticateLocalPlayer{
// Instantiate a GKLocalPlayer object to use for authenticating a player.
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
if (viewController != nil) {
// If it's needed display the login view controller.
[self presentViewController:viewController animated:YES completion:nil];
} else {
if ([GKLocalPlayer localPlayer].authenticated) {
// If the player is already authenticated then indicate that the Game Center features can be used.
_gameCenterEnabled = YES;
// Get the default leaderboard identifier.
[[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
} else{
_leaderboardIdentifier = @"thirtysecondtappers";
}
}];
} else {
_gameCenterEnabled = NO;
}
}
};
}
@end
所以這是代碼。在方法-(void)tensecs
我說:
NSString* myString = [@(timem) stringValue];
timemanage.text = myString;
而且我把一切正常聯繫起來!然後,在模擬器或任何設備上發生的情況是,時間標籤從30減少到0的正確倒數。但每次數字下降時(例如29到28),UIImageView將重置爲其屏幕上的原始位置。
請參閱我在此處顯示屏幕截圖。
我試着刪除那些代碼
NSString* myString = [@(timem) stringValue];
timemanage.text = myString;
,然後它沒有這樣做了。我不知道爲什麼會發生這種情況!
摘要:
的UIImageView的是回到其原始位置時,計時器標籤上改變了許多!此外,我還在實際設計師的圖像中添加了約束條件。這是爲什麼?
是的!!!!!!你們真是太棒了!還有一個問題。 AutoLayout的轉向是否會導致任何問題? – Xcoder 2014-10-18 05:43:28
@Xcoder,這取決於你如何設置你的觀點,以及你想要做什麼。您仍然可以在IB中使用舊的彈簧和支柱,或者如果您不想使用自動佈局,則可以使用代碼定位事物的方式。 – rdelmar 2014-10-18 05:45:55
還有一個問題,我有imageView,它都工作正常,剛纔我改變了圖像,圖像不會顯示出來。 :(因此我將它恢復到原來的圖像,並且它完美地顯示出來,所以我刪除了圖像查看並放入了我想要的圖像,但它仍然不起作用!不知道爲什麼嗎?:) – Xcoder 2014-10-18 05:50:32