0

移動点に向かって常に移動するオブジェクトがあるアプリを作成しました。そのため、アニメーション機能は使用しませんでした。問題は、私がこの関数を作成したことです:

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 への変化が大きくなり、移動が速くなることを意味します。

これを行う別の方法があれば、私は喜んでそれを試しますので、他の方法を知っているなら教えてください!

4

1 に答える 1

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
于 2013-07-25T15:16:59.390 に答える