0

私のアプリケーションでは、UIButton内部に 3 つのボタンを作成しています。これらのボタンをその中で交換する必要があります。(ie) (ボタン 1) をドラッグ アンド ドロップして (ボタン 2) に配置すると、(ボタン 2) は (ボタン 1) の位置に置き換えられ、(ボタン 3) の位置も同じになります。をドラッグアンドドロップする方法を見つけましたが、UIButton交換できません。

ボタンの作成とドラッグアンドドロップのコードは次のとおりです。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setTitle:@"Drag One!" 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 - 10)/2.0,
                              (self.view.bounds.size.height - 50)/2.0,
                              100, 50);
    button.tag=0;
    [self.view addSubview:button];
   .........


  -(void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{

 if(button.tag==0){


        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);
    }
    ...........
}
4

1 に答える 1

1

次のようなコードを使用します。

[UIView animateWithDuration:0.5
                              delay:0.1
                            options: UIViewAnimationCurveEaseOut
                         animations:^
         {
             CGSize s = [[CCDirector sharedDirector] winSize];

             CGRect frame = mButton.frame;
             frame.origin.y = location.y ;
             frame.origin.x = location.x ;
             mButton.frame = frame;
         } 
                         completion:^(BOOL finished)
         {


         }];
于 2012-08-17T08:04:05.653 に答える