Ios 在屏幕右侧设置UIImageView
我有一个叫做ufo的图像视图。它在屏幕外向左移动,直到你再也看不到它,我希望它在屏幕右侧重生并移入。左侧工作,右侧不工作Ios 在屏幕右侧设置UIImageView,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,我有一个叫做ufo的图像视图。它在屏幕外向左移动,直到你再也看不到它,我希望它在屏幕右侧重生并移入。左侧工作,右侧不工作 if (ufo.center.x < (-ufo.frame.size.width/2)) { ufo.center = CGPointMake((backGround.frame.size.width - (ufo.frame.size.width / 2)), ufo.center.y); } if(ufo.cente
if (ufo.center.x < (-ufo.frame.size.width/2)) {
ufo.center = CGPointMake((backGround.frame.size.width - (ufo.frame.size.width / 2)), ufo.center.y);
}
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);
}
- (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];
}
if(ufo.center.x<(backGround.frame.origin.x-(ufo.bounds.size.width/2.0)))
{
//只是猜测一下,因为您尚未显示动画代码,但请添加以下行:
[不明飞行物层移除警报];
//你的表现还不够好,下面是另一个黑暗中的镜头:
[计时器失效];
ufo.center=CGPointMake((backGround.frame.size.width+(ufo.bounds.size.width/2.0)),ufo.center.y);
}
以下是根据一些猜测(因为您没有充分展示)和您迄今为止提供的信息模拟您的游戏行为
根据您的标准,您的UFO现在在我的屏幕上从右到左再回到右飞行:
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);
}
- (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];
}
-(void)viewDidLoad
{
[超级视图下载];
[自创MyufoandMyBackground];
}
-(void)createMyUFOandMyBackground
{
myBackground=[[UIImageView alloc]initWithFrame:self.view.bounds];
myBackground.image=[UIImage ImageName:@“background”];
[self.view addSubview:myBackground];
myUFO=[[UIImageView alloc]initWithImage:[UIImageName:@“ufo”];
myUFO.center=(CGPoint){myBackground.bounds.size.width+(myUFO.bounds.size.width/2.0f),myBackground.center.y};
[self.view addSubview:myUFO];
[自创定时器];
}
-(void)创建计时器
{
myTimer=[NSTimer scheduledTimerWithTimeInterval:0.05f目标:自选择器:@selector(moveUFOToLeft)用户信息:无重复:是];
}
-(void)moveUFOToLeft
{
温度=0;
if(myUFO.center.x<(myBackground.frame.origin.x-(myUFO.bounds.size.width/2.0)))
{
[myTimer失效];
myTimer=nil;
myUFO.center=CGPointMake((myBackground.frame.size.width+(myUFO.bounds.size.width/2.0)),myUFO.center.y);
[自重启MyTimeRafterSeconds];
}
其他的
{
温度=-arc4random_均匀(10);
myUFO.center=CGPointMake(myUFO.center.x+temp,myUFO.center.y);
}
}
-(无效)restartMyTimerAfterSeconds
{
//这是特定于你的游戏,我将留给你。
[自创定时器];
}
我假设您在背景
上添加了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从右向左移动的常量
CGFloat温度=-2;
//为ufo添加移动偏移后创建新点
CGPoint=CGPointMake(ufo.center.x+温度,ufo.center.y);
//检查是否将新点移离背景,如果是,则将新中心指定到右侧
如果((点x+CGRectGetWidth(ufo边界)/2)<0.0f){
point.x=CGRectGetWidth(ufo.bounds)/2+CGRectGetMaxX(backGround.bounds)
}
//将新中心指定给ufo,以便从其最后位置移动。
ufo.中心=点;
是的,我尝试过,但它会重新出现在屏幕的左侧,而不是右侧,这毫无意义:(是的,我复制了你的,但仍然是相同的错误:(我知道它们应该像这样重新出现在屏幕右侧,但它们没有任何意义。我的动画代码是什么?我没有任何动画我只是在中心的x位置添加一个名为“temp”的随机速度变量(该变量是负数)