我想将图像从屏幕的一侧移动到另一侧。iOS中的基本雪碧/ UIImage运动?
我有两个UIImageViews,ball1和ball2。一个在左边,一个在右边。设备处于横向模式。 当用户点击一个按钮时,我希望它们切换边,就像补间动画一样。我曾尝试使用CGPoint,但我无法弄清楚。
我想将图像从屏幕的一侧移动到另一侧。iOS中的基本雪碧/ UIImage运动?
我有两个UIImageViews,ball1和ball2。一个在左边,一个在右边。设备处于横向模式。 当用户点击一个按钮时,我希望它们切换边,就像补间动画一样。我曾尝试使用CGPoint,但我无法弄清楚。
创建两个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);
,然后设置定时器火了一个名为“移动”
timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector (move) userInfo:nil repeats:YES];
创建不是全局声明使用了一个名为stopBalls
方法停止移动球另一个计时器方法。 (假设它需要两秒钟的时间做到这一点。试验及工程的任何更换2.0
)
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector (stopBall) userInfo:nil repeats:YES];
创建一个使用CGPoints和pos
变量移动图像-(void)move
方法。假设图像被命名为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;
}
然后创建stopBalls
方法来阻止球移动。这会重置CGPoint并停止timer
。
-(void)stopBalls
{
pos = CGPointMake(0.0,0.0);
pos2 = CGPointMake(0.0,0.0);
[timer invalidate];
}
只需重新运行启动移动的方法再次执行该操作即可。
同时不要在球移动时多次运行该方法,因为这会使速度加倍,因为另一个定时器(2.0,0.0);
和(-2.0,0.0);
被触发。
CGPoint firstCenter = ball1.center;
CGPoint secondCenter = ball2.center;
[UIView animateWithDuration:1.0 animations:^{
ball1.center = secondCenter;
ball2.center = firstCenter;
}];
很好,谢谢。 – user1536836 2012-07-19 05:48:57
以及雪碧运动试试这个LINK
关于你的问题,你可以使用核心动画,改变球 的立场是应该做的我猜
比我想找的要多一点,但它给了我一个编辑和玩的片段。谢谢! – user1536836 2012-07-19 05:49:25