私が目指しているパターンは、車のギアシフターのパターンです。これは、90度の角度で約9本の直線で構成されており、ユーザーがUIViewをドラッグする必要があります。
私の最初のアプローチは、ビューの中心の座標を確認し、特定の条件を満たす場合は、x/yを適切に有効/無効にすることでした。これにより、維持するのが面倒になる長い複雑なifステートメントが作成されました。ほとんどの場合、機能します。ときどきスタックしますが、修正するために何をする必要があるかはわかっています。
2番目のアイデアは、移動したいビューに固定する正方形を許可するのに十分な幅のスペースを中央に配置して、パスの周りに非常に単純な長方形のコレクションを作成することです。そうすれば、衝突検出を使用して、パスに沿ってビューを維持できるようになります。これは、非常に基本的なブロックパスでは機能するように見えますが、より複雑なパスでは実行できません。
私の望む効果を達成する他の方法はありますか?
ところで、これが最初のシナリオの私の非常にラフで非効率的なテストです:
// The reason we aren't using if else is because we want to allow both x and y movement if they are on a corner.
if(knob.center.x >= 218 && knob.center.x <= 240 && knob.center.y == 140) {
// First line
canMoveX = TRUE;
if(translation.x + knob.center.x < 218) {
translation.x = knob.center.x - 218;
}
else if(translation.x + knob.center.x > 240) {
translation.x = 240 - knob.center.x;
}
}
if(knob.center.x == 240 && knob.center.y >= 140 && knob.center.y <= 230) {
// Second Line
canMoveY = TRUE;
if(translation.y + knob.center.y < 140) { translation.y = knob.center.y - 140; }
else if(translation.y + knob.center.y > 230) { translation.y = 230 - knob.center.y; }
}
if(knob.center.x >= 224 && knob.center.x <= 240 && knob.center.y == 230) {
// Third Line
canMoveX = TRUE;
if(translation.x + knob.center.x < 224) { translation.x = knob.center.x - 224; }
else if(translation.x + knob.center.x > 240) { translation.x = 240 - knob.center.x; }
}
if(knob.center.x == 224 && knob.center.y >= 230 && knob.center.y <= 285) {
// Fourth Line
canMoveY = TRUE;
if(translation.y + knob.center.y < 230) { translation.y = knob.center.y - 230; }
else if(translation.y + knob.center.y > 285) { translation.y = 285 - knob.center.y; }
}
if(knob.center.x >= 205 && knob.center.x <= 224 && knob.center.y == 285) {
// Fifth Line
canMoveX = TRUE;
if(translation.x + knob.center.x < 205) { translation.x = knob.center.x - 205; }
else if(translation.x + knob.center.x > 224) { translation.x = 224 - knob.center.x; }
}
if(knob.center.x == 205 && knob.center.y >= 285 && knob.center.y <= 337) {
// Sixth Line
canMoveY = TRUE;
if(translation.y + knob.center.y < 285) { translation.y = knob.center.y - 285; }
else if(translation.y + knob.center.y > 337) { translation.y = 337 - knob.center.y; }
}
if(knob.center.x >= 133 && knob.center.x <= 205 && knob.center.y == 337) {
// Seventh Line
canMoveX = TRUE;
if(translation.x + knob.center.x < 133) { translation.x = knob.center.x - 133; }
else if(translation.x + knob.center.x > 205) { translation.x = 205 - knob.center.x; }
}
if(canMoveX) {
[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y)];
[gesture setTranslation:CGPointZero inView:[piece superview]];
}
if(canMoveY) {
[piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
[gesture setTranslation:CGPointZero inView:[piece superview]];
}