使对象以自定义速度从1点移动到另一点

时间:2013-07-25 13:54:16

标签: iphone objective-c

我制作了一个应用程序,其中有一个对象一直向移动点移动 - 这就是为什么我没有使用任何动画功能。问题是我做了这个功能:

CGPoint center = self.im.center; // "i" is a CGPoint, im is an imageview.
if (!CGPointEqualToPoint(self.im.center, i))
{
    a = (i.y-center.y)/(i.x-center.x);
    //Y = a*X+b - this is a linear function in math
    b = (center.y-(a*center.x));
    if (i.y>center.y) {
        self.im.center = CGPointMake(((center.y+1)-b)/a, center.y+1);
    }
    else
    {
        self.im.center = CGPointMake(((center.y-1)-b)/a, center.y-1);
    }
}

问题是函数越接近直线水平线越快,因为变化主要是X轴,这意味着如果我将Y加1,则对X的变化越大,这意味着它将移动得更快。

如果有其他方法可以做到这一点,我很乐意尝试,如果你知道其他方式告诉我!

1 个答案:

答案 0 :(得分:0)

管理以寻找不同的解决方案

CGPoint center = self.im.center;//im = the image view
x = center.x;//starting point
y = center.y;//starting point
double distance = sqrtf(powf(i.x - x, 2) + powf(i.y - y, 2));// i = cgpoint (ending point)
float speedX = (2 * (i.x - x)) / distance;//(the 2 is the speed)
float speedY = (2 * (i.y - y)) / distance;//(the 2 is the speed)
self.im.center = CGPointMake(center.x+speedX, center.y+speedY);//im = the image view
相关问题