在我的应用程序中,我在UIButton
里面创建了三个,我希望这些按钮应该在其中交换。(即)如果我拖放(按钮 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);
}
...........
}