0

こんにちは、描画四角形で作成した円の周りでのみ UIButton をドラッグできるようにしたいと考えています。円の形ではなく円のパスを作成する必要があるかもしれませんが、私はこれに慣れていないため、アプローチについてよくわかりません。これが私が持っているコードです。ボタンをその円または目に見えないパスの周りにドラッグするだけです。ヘルプや例は非常に役立ちます。ありがとう!

編集:以下の回答の一部を使用しましたが、これを正しく機能させる方法がわかりません。作成した円と同じサイズにしたいので、円パスだけを作成して、作成した円の上に配置できます。

//Button Code
timeSetButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [timeSetButton addTarget:self
                         action:nil
               forControlEvents:UIControlEventTouchDown];
        [timeSetButton setImage:[UIImage imageNamed:@"testButton.png"] forState: UIControlStateNormal];
        timeSetButton.frame = CGRectMake(0,415,50,50);
        [self.view addSubview:timeSetButton];

        //Button Listener
        [timeSetButton addTarget:self action:@selector(wasDragged:withEvent:)
         forControlEvents:UIControlEventTouchDragInside];


//Draggable Button Code
// get the touch
UITouch *touch = [[event touchesForView:timeSetButton] anyObject];

// get delta
CGPoint previousLocation = [touch previousLocationInView:timeSetButton];
CGPoint location = [touch locationInView:timeSetButton];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;

// move button
timeSetButton.center = CGPointMake(timeSetButton.center.x + delta_x,
                            timeSetButton.center.y + delta_y);

// enforce constraint on locations, by...

// working out the distance from the centre of the circle
CGPoint vectorFromCentreOfCircle =
CGPointMake(150,150);

CGFloat distanceFromCentreOfCircle = hypotf(vectorFromCentreOfCircle.x, vectorFromCentreOfCircle.y);

// working out what you'd need to multiply that distance by in order
// to get the specified radius
CGFloat correctionMultiplier = 20 / distanceFromCentreOfCircle;

// adjust vector from centre of circle
vectorFromCentreOfCircle.x *= correctionMultiplier;
vectorFromCentreOfCircle.y *= correctionMultiplier;

// move button one more time
timeSetButton.center = CGPointMake(
                    200 + vectorFromCentreOfCircle.x,
                    200 + vectorFromCentreOfCircle.y);

サークルシェイプはこちら

circleView = [[CircleView alloc] initWithFrame:CGRectMake(55, 100, 260, 260)];
        circleView.backgroundColor = [UIColor clearColor];
        [self.view addSubview:circleView];
        [self.view sendSubviewToBack:circleView];
4

3 に答える 3

1

このような場合は、ボタンを移動した後に簡単なコードを追加して、ボタンが円上になければならないという制約を適用するだけで十分でしょう。例えば

// move button
timeSetButton.center = CGPointMake(button.center.x + delta_x,
                            button.center.y + delta_y);

// enforce constraint on locations, by...

// working out the distance from the centre of the circle
CGPoint vectorFromCentreOfCircle =
     CGPointMake(timeSetButton.center.x - centreOfCircle.x,
                 timeSetButton.center.x - centreOfCircle.y);

CGFloat distanceFromCentreOfCircle = 
     hypotf(vectorFromCentreOfCircle.x, vectorFromCentreOfCircle.y);

// working out what you'd need to multiply that distance by in order
// to get the specified radius
CGFloat correctionMultiplier = radiusOfCircle / distanceFromCentreOfCircle;

// adjust vector from centre of circle
vectorFromCentreOfCircle.x *= correctionMultiplier;
vectorFromCentreOfCircle.y *= correctionMultiplier;

// move button one more time
timeSetButton.center = CGPointMake(
          centreOfCircle.x + vectorFromCentreOfCircle.x,
          centreOfCircle.y + vectorFromCentreOfCircle.y);
于 2013-08-14T04:10:18.743 に答える
0

これは、ビューの中心から touchPoint までの距離を知ることで簡単に実行できます。

コードに次の変更を加えます。これが機能するかどうかを確認してください

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
    // get the touch
    UITouch *touch = [[event touchesForView:button] anyObject];

    // get delta
    CGPoint previousLocation = [touch previousLocationInView:button];
    CGPoint location = [touch locationInView:button];

    //calculate center of button in superView
    CGPoint buttonCenter = CGPointMake(button.frame.origin.x+(button.frame.size.width/2.0f),
                                       button.frame.origin.y+(button.frame.size.height/2.0f));
    //calculate the distance of current touchPoint from buttonCenter
    CGFloat diffx = location.x - buttonCenter.x;
    CGFloat diffy = location.y - buttonCenter.y;
    CGFloat distance = sqrtf((diffx*diffx)+(diffy*diffy));

    //check if the distance is within the radius of circle.
    //assuming that your button is always a square to make 
    //perfect circle within it.
    CGFloat radius = button.frame.size.width/2.0f;

    if(radius >= distance)//this makes a circular check.
    {
      CGFloat delta_x = location.x - previousLocation.x;
      CGFloat delta_y = location.y - previousLocation.y;

      // move button
      timeSetButton.center = CGPointMake(button.center.x + delta_x,
                                button.center.y + delta_y);
    }

}
于 2013-08-14T04:11:53.703 に答える