将 UIImageView 设置在屏幕右侧
set UIImageView at the right side off the screen
我有一个名为 ufo 的 UIImageView。它在屏幕外向左移动,直到您再也看不到它为止,我希望它在屏幕右侧重生并移入。左侧正在工作,但右侧没有。
if (ufo.center.x < (-ufo.frame.size.width/2)) {
ufo.center = CGPointMake((backGround.frame.size.width - (ufo.frame.size.width / 2)), ufo.center.y);
}
它完全在右侧重生,没有离开屏幕。我知道 CGPointMake 中应该有一个 +,但它在左侧出现故障!
有人可以帮忙吗?
谢谢。
根据您的标准,我会做类似以下的事情:
if (ufo.center.x < (backGround.frame.origin.x - (ufo.bounds.size.width / 2.0)))
{
//just guessing, since you haven't shown your animation code, but, add the following line:
[ufo.layer removeAllAnimations];
//you haven't shown enough, so here is another shot in the dark:
[timer invalidate];
ufo.center = CGPointMake((backGround.frame.size.width + (ufo.bounds.size.width / 2.0)), ufo.center.y);
}
以下 用于模仿您的游戏行为,基于一些猜测(因为您没有展示足够多)和您迄今为止提供的信息。
根据您的标准,您的 UFO 现在在我的屏幕上从右向左然后向右飞:
- (void)viewDidLoad
{
[super viewDidLoad];
[self createMyUFOandMyBackground];
}
- (void)createMyUFOandMyBackground
{
myBackground = [[UIImageView alloc] initWithFrame:self.view.bounds];
myBackground.image = [UIImage imageNamed:@"background"];
[self.view addSubview:myBackground];
myUFO = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ufo"]];
myUFO.center = (CGPoint){myBackground.bounds.size.width + (myUFO.bounds.size.width / 2.0f), myBackground.center.y};
[self.view addSubview:myUFO];
[self createTimer];
}
- (void)createTimer
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(moveUFOToLeft) userInfo:nil repeats:YES];
}
- (void)moveUFOToLeft
{
temp = 0;
if (myUFO.center.x < (myBackground.frame.origin.x - (myUFO.bounds.size.width / 2.0)))
{
[myTimer invalidate];
myTimer = nil;
myUFO.center = CGPointMake((myBackground.frame.size.width + (myUFO.bounds.size.width / 2.0)), myUFO.center.y);
[self restartMyTimerAfterSeconds];
}
else
{
temp = - arc4random_uniform(10);
myUFO.center = CGPointMake(myUFO.center.x + temp, myUFO.center.y);
}
}
- (void)restartMyTimerAfterSeconds
{
//This is specific to your game; I will leave that to you.
[self createTimer];
}
我假设您在 backGround
之上添加了 ufo
并尝试在计时器的帮助下将 ufo 从背景视图上方的右向左移动。
在计时器方法中使用以下源代码
//Constant which will allow ufo to be moved from right to left
CGFloat temp = -2;
//Create new point after adding moving offset for ufo
CGPoint point = CGPointMake(ufo.center.x + temp, ufo.center.y);
//Check weather new points is moved away from background if so then assign new center to the right
if ((point.x + CGRectGetWidth(ufo.bounds)/2) < 0.0f) {
point.x = CGRectGetWidth(ufo.bounds)/2 + CGRectGetMaxX(backGround.bounds)
}
//Assign the new center to ufo for providing movement from its last position.
ufo.center = point;
我有一个名为 ufo 的 UIImageView。它在屏幕外向左移动,直到您再也看不到它为止,我希望它在屏幕右侧重生并移入。左侧正在工作,但右侧没有。
if (ufo.center.x < (-ufo.frame.size.width/2)) {
ufo.center = CGPointMake((backGround.frame.size.width - (ufo.frame.size.width / 2)), ufo.center.y);
}
它完全在右侧重生,没有离开屏幕。我知道 CGPointMake 中应该有一个 +,但它在左侧出现故障!
有人可以帮忙吗?
谢谢。
根据您的标准,我会做类似以下的事情:
if (ufo.center.x < (backGround.frame.origin.x - (ufo.bounds.size.width / 2.0)))
{
//just guessing, since you haven't shown your animation code, but, add the following line:
[ufo.layer removeAllAnimations];
//you haven't shown enough, so here is another shot in the dark:
[timer invalidate];
ufo.center = CGPointMake((backGround.frame.size.width + (ufo.bounds.size.width / 2.0)), ufo.center.y);
}
以下 用于模仿您的游戏行为,基于一些猜测(因为您没有展示足够多)和您迄今为止提供的信息。
根据您的标准,您的 UFO 现在在我的屏幕上从右向左然后向右飞:
- (void)viewDidLoad
{
[super viewDidLoad];
[self createMyUFOandMyBackground];
}
- (void)createMyUFOandMyBackground
{
myBackground = [[UIImageView alloc] initWithFrame:self.view.bounds];
myBackground.image = [UIImage imageNamed:@"background"];
[self.view addSubview:myBackground];
myUFO = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ufo"]];
myUFO.center = (CGPoint){myBackground.bounds.size.width + (myUFO.bounds.size.width / 2.0f), myBackground.center.y};
[self.view addSubview:myUFO];
[self createTimer];
}
- (void)createTimer
{
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.05f target:self selector:@selector(moveUFOToLeft) userInfo:nil repeats:YES];
}
- (void)moveUFOToLeft
{
temp = 0;
if (myUFO.center.x < (myBackground.frame.origin.x - (myUFO.bounds.size.width / 2.0)))
{
[myTimer invalidate];
myTimer = nil;
myUFO.center = CGPointMake((myBackground.frame.size.width + (myUFO.bounds.size.width / 2.0)), myUFO.center.y);
[self restartMyTimerAfterSeconds];
}
else
{
temp = - arc4random_uniform(10);
myUFO.center = CGPointMake(myUFO.center.x + temp, myUFO.center.y);
}
}
- (void)restartMyTimerAfterSeconds
{
//This is specific to your game; I will leave that to you.
[self createTimer];
}
我假设您在 backGround
之上添加了 ufo
并尝试在计时器的帮助下将 ufo 从背景视图上方的右向左移动。
在计时器方法中使用以下源代码
//Constant which will allow ufo to be moved from right to left
CGFloat temp = -2;
//Create new point after adding moving offset for ufo
CGPoint point = CGPointMake(ufo.center.x + temp, ufo.center.y);
//Check weather new points is moved away from background if so then assign new center to the right
if ((point.x + CGRectGetWidth(ufo.bounds)/2) < 0.0f) {
point.x = CGRectGetWidth(ufo.bounds)/2 + CGRectGetMaxX(backGround.bounds)
}
//Assign the new center to ufo for providing movement from its last position.
ufo.center = point;