gpt4 book ai didi

ios - 将 UIImageView 设置在屏幕右侧

转载 作者:行者123 更新时间:2023-11-29 12:15:22 24 4
gpt4 key购买 nike

我有一个名为 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];
}

关于ios - 将 UIImageView 设置在屏幕右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32149360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com