0
내 응용 프로그램에서는 내부에 UIButton
세 개를 만들고 있는데,이 버튼을 그 사이에 스왑해야합니다. (버튼 1)을 드래그 앤 드롭하여 (버튼 2) 놓으면 (버튼 2)이 (버튼 1)의 위치에서 바뀌고 (버튼 3)에서도 바뀌어야합니다. UIButton
을 끌어서 놓는 방법을 찾았지만 교체 할 수 없습니다. 이 같은iOS/iPhone에서 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);
}
...........
}