Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS中的基本精灵/UI图像移动?_Iphone_Ios - Fatal编程技术网

Iphone iOS中的基本精灵/UI图像移动?

Iphone iOS中的基本精灵/UI图像移动?,iphone,ios,Iphone,Ios,我想把图像从屏幕的一边移到另一边 我有两个视图,ball1和ball2。一个在左边,一个在右边。设备处于横向模式。 当用户单击一个按钮时,我希望他们切换到另一边,就像一个动作夹。我试过使用CGPoint,但我想不出来。对于精灵运动,试试这个 CGPoint firstCenter = ball1.center; CGPoint secondCenter = ball2.center; [UIView animateWithDuration:1.0 animations:^{ ball1.

我想把图像从屏幕的一边移到另一边

我有两个视图,ball1和ball2。一个在左边,一个在右边。设备处于横向模式。
当用户单击一个按钮时,我希望他们切换到另一边,就像一个动作夹。我试过使用CGPoint,但我想不出来。

对于精灵运动,试试这个

CGPoint firstCenter = ball1.center;
CGPoint secondCenter = ball2.center;
[UIView animateWithDuration:1.0 animations:^{
    ball1.center = secondCenter;
    ball2.center = firstCenter;
}];
关于您的问题,您可以使用核心动画并更改球的位置
我想应该可以了,在你的标题中创建两个CGPoints和一个NSTimer

CGPoint pos1;
CGPoint pos2;
NSTimer *timer;
在移动图像之前的任何其他方法中,设置将用作图像速度的CGPoints。因为您是水平移动的,所以CGPoint分别要求x和y参数。在此示例中,使用
(2.0,0.0)
(-2.0,0.0)分别用于右侧和左侧

pos = CGPointMake(2.0,0.0);    
pos2 = CGPointMake(-2.0,0.0);    
然后将计时器设置为触发一个名为“move”的方法

创建另一个未全局声明的计时器,以使用名为“
停止球”
的方法停止球的移动。(假设这需要两秒钟的时间。尝试并用任何有效的方法替换
2.0

创建
-(void)move
方法,该方法使用CGPoints和
pos
变量移动图像。假设图像名为
ball1
ball2

-(void)move
{
    ball1.center = CGPointMake(ball1.center.x+pos1.x,ball1.center.y +pos1.y);
    if (ball1.center.x > 2024 || ball1.center.x < 0)
        pos1.x = -pos1.x;
    if (ball1.center.y > 768 || ball1.center.y < 0)
        pos1.y = -pos1.y;

    ball2.center = CGPointMake(ball2.center.x+pos2.x,ball2.center.y +pos2.y);
    if (ball2.center.x > 2024 || ball2.center.x < 0)
        pos2.x = -pos2.x;
    if (ball2.center.y > 768 || ball2.center.y < 0)
        pos2.y = -pos2.y;
}
只需重新运行用于启动移动的方法即可再次执行


当球移动时,也不要运行该方法一次以上,因为这会使速度加倍,就像另一个使用
(2.0,0.0)的计时器一样
(-2.0,0.0)被触发。

比我要查找的内容多一点,但它给了我一个片段可以编辑和使用。非常感谢。
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector (stopBall) userInfo:nil repeats:YES];
-(void)move
{
    ball1.center = CGPointMake(ball1.center.x+pos1.x,ball1.center.y +pos1.y);
    if (ball1.center.x > 2024 || ball1.center.x < 0)
        pos1.x = -pos1.x;
    if (ball1.center.y > 768 || ball1.center.y < 0)
        pos1.y = -pos1.y;

    ball2.center = CGPointMake(ball2.center.x+pos2.x,ball2.center.y +pos2.y);
    if (ball2.center.x > 2024 || ball2.center.x < 0)
        pos2.x = -pos2.x;
    if (ball2.center.y > 768 || ball2.center.y < 0)
        pos2.y = -pos2.y;
}
-(void)stopBalls
{
    pos = CGPointMake(0.0,0.0);    
    pos2 = CGPointMake(0.0,0.0); 
    [timer invalidate];
}