0

ビューがあり、その中にUIImage. 画像は静的ではなく、指をドラッグすると (ドラッグ イベントを使用して) 移動できます。問題は、画像がUIViewフレームの外に移動する場合があることです。親フレームの境界内に保持する適切な方法は何ですか?

--UIViewA

--------UIViewB

--------------UIImage

--------------UIボタン

UIViewB 内に UIImage を保持したい

- (IBAction)myButtonSingleTap:(UIButton *)sender {
    imDragging = YES;
    [_myButton addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];

}

- (IBAction)myButtonDragInside:(UIButton *)sender
{
    [_myButton addTarget:self action:@selector(draging:withEvent:) forControlEvents: UIControlEventTouchDragInside];

}
- (void)dragBegan:(UIControl *)c withEvent:ev {

    UITouch *touch = [[ev allTouches] anyObject];
    startingTouchPoint = [touch locationInView:self.view];

}
- (void)draging:(UIControl *)c withEvent:ev {
    UITouch *touch = [[ev allTouches] anyObject];
    currentTouchPoint = [touch locationInView:self.view];
    _movingPic.frame = CGRectMake(currentTouchPoint.x, currentTouchPoint.y, 28, 23);
}
4

3 に答える 3

1

ドラッグ中にビューの位置を確認する必要があります。

ある時点で、ユーザーのドラッグ方向などに応じて画像のフレームを設定します...

この間、次のようなロジックチェックが必要です...

If new location x value is less than 0 then set new location x = 0.
If new location x value plus image width is greater than view width then set new location x = view width - image width.

等...

次に、新しい場所をポイントとして使用して、画像を移動します。

于 2013-05-09T11:21:46.183 に答える
0

ビュー全体ではなく、親ビューにタッチ認識機能を追加してみてください

于 2013-05-09T11:05:31.277 に答える
0

新しいフレームを設定する前に、それが移動ビューのスーパービューの境界に含まれていることを確認してください。

- (void)draging:(UIControl *)c withEvent:ev
{
    UITouch *touch = [[ev allTouches] anyObject];
    currentTouchPoint = [touch locationInView:self.view];
    CGRect newFrame = CGRectMake(currentTouchPoint.x, currentTouchPoint.y, 28, 23);
    newFrame.x = MAX(newFrame.x, 0);
    newFrame.y = MAX(newFrame.y, 0);
    newFrame.x = MIN(newFrame.x, _movingPic.superview.bounds.size.width - 28);
    newFrame.y = MIN(newFrame.y, _movingPic.superview.bounds.size.height - 23);
    _movingPic.frame = newFrame;
}
于 2013-05-09T11:24:30.753 に答える