这是一个非常奇怪的情况,我从来没有这样的问题,我真的很感激我在这篇文章中得到的任何帮助。我做了一个简单的游戏。我将在下面给出.h和.m。
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时,UIImageViews重置到屏幕上的原始位置。
请参阅此处显示屏幕截图。
我尝试删除该代码
NSString* myString = [@(timem) stringValue];
timemanage.text = myString;
然后它不再这样做了。我不知道为什么会这样!
摘要:
当计时器更改标签上的数字时,UIImageView将返回其原始位置!我还在实际设计师的图像上添加了约束。这是为什么?
答案 0 :(得分:1)
这是因为自动布局。如果您尚未关闭自动布局,并通过设置框架移动视图,那么当视图需要重绘时(如分数标签更改时),您移动的视图将恢复到其约束定义的位置。要解决此问题,您需要通过调整约束来重新定位视图,或者关闭自动布局。