1

サブビューのみで uibutton をドラッグ アンド ドロップする必要があります。実際、私はメインビューでサブビューを取っています。UIbutton はサブビューに追加されます。ボタンをドラッグすると、両方のビューで移動しますが、サブビューでのみボタンをドラッグする必要があります。誰でも私を助けてください。前もって感謝します。

- (void) drag
{
  //subview
  UIView *viewscramble = [[UIView alloc] initWithFrame:CGRectMake(0, 90, 320, 50)];
  [viewscramble setBackgroundColor:[UIColor redColor]];
  [self.view addSubview:viewscramble];

   //button
   UIButton  *btnscramble = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, 50, 50)];
   btnscramble.titleLabel.text = @"A";
   [btnscramble setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   [btnscramble addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
   [btnscramble setBackgroundImage:[UIImage imageNamed:@"box.png"] forState:UIControlStateNormal];

   //add button`     
   [viewscramble addSubview:btnscramble];
   [btnscramble release];
}

- (IBAction)imageMoved:(id)sender withEvent:(UIEvent *) event
{
  CGPoint point = [[[event allTouches]anyObject] locationInView:viewscramble];
  UIControl *control = sender;
  control.center = point;
}
4

5 に答える 5

3

私はちょうどあなたの質問の解決策を以下から見つけました:-http://www.cocoanetics.com/2010/11/draggable-buttons-labels/

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // create a new button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Drag me!" forState:UIControlStateNormal];

    // add drag listener
    [button addTarget:self action:@selector(wasDragged:withEvent:) 
        forControlEvents:UIControlEventTouchDragInside];

    // center and size
    button.frame = CGRectMake((self.view.bounds.size.width - 100)/2.0,
            (self.view.bounds.size.height - 50)/2.0,
            100, 50);

    // add it, centered
    [self.view addSubview:button];
}


- (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];
    CGFloat delta_x = location.x - previousLocation.x;
    CGFloat delta_y = location.y - previousLocation.y;

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

お役に立てば幸いです:)

于 2012-12-03T05:30:54.863 に答える
0
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{

    UITouch *tap = [touches anyObject];
    CGPoint pointToMove = [tap locationInView:yourSubView]; // just assign subview where you want to move the Button
}
于 2012-12-03T05:31:07.030 に答える
0
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if([touch view] == YOUR_BUTTON_OBJECT){
       CGPoint point = [touch locationInView:YOUR_PARENT_VIEW];
       //YOUR_X_LIMIT is your subview end in x direction.
       //YOUR_Y_LIMIT is your subview end in y direction.
       if(point.x < YOUR_X_LIMIT && point.y < YOUR_Y_LIMIT){
          button.center = point;
       }else{
         // do nothing since touch is outside your limit.
       }
    }
}
于 2012-12-03T05:31:29.023 に答える
0

UIButton の UIControlEventTouchDragInside および UIControlEventTouchUpInside イベントを使用します。

// add drag listener
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
// add tap listener
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside];

hbdraggablebuttonコントロールを使用できます。

于 2013-10-11T14:10:18.993 に答える