私のアプリケーションでは、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);
}
...........
}