それでは、あなたはドラッグ操作の途中にいるとしましょう。どのようなジェスチャーが動きを引き起こしていても、その中心をその中心に設定することで、ボタンインスタンスを移動しています。
ジェスチャの中心をテストし、中心の値を好きでない場合はリセットして制限を加えることができます。以下では、すべてのTouch Dragイベントのアクションにボタンが接続されていることを前提としていますが、ジェスチャ認識機能やtouchesBegan:や友だちを使用している場合でも原則が適用されます。
- (IBAction)handleDrag:(UIButton *)sender forEvent:(UIEvent *)event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
if (point.y > 200)
{
point.y = 200; //No dragging this button lower than 200px from the origin!
}
sender.center = point;
}
あなただけの1つの軸上のスライドボタンをしたい場合は、それは十分に簡単です:
- (IBAction)handleDrag:(UIButton *)sender forEvent:(UIEvent *)event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
point.y = sender.center.y; //Always stick to the same y value
sender.center = point;
}
または多分あなただけの特定のビューの領域内のボタンをドラッグします。境界が複雑な場合は、これを定義する方が簡単かもしれません。
- (IBAction)handleDrag:(UIButton *)sender forEvent:(UIEvent *)event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.someView];
if ([self.someView pointInside:point withEvent:nil])
{
sender.center = point;
//Only if the gesture center is inside the specified view will the button be moved
}
}
絶対に華麗で、治療を受けました。 –