2009-08-17 2 views
1

저는 현재 UIButtons가 정사각형으로 배열 된 프로그램을 작성하고 있습니다. NIB에서. 나는 "Touch Up Inside"이벤트를 사용하여 하나를 테이핑하고 다른 하나를 테이핑하여 버튼을 교환합니다.UIButton 위치를 스 와이프하거나 드래그하여 어떻게 바꿉니 까?

나는 다른 사람의 방향으로 사람을 스 와이프하고 다른 사람의 아이를 얹어서 스왑하고 싶습니다. 나는 이것을하는 방법을 알아낼 수 없었다. 필자는 수 주일 동안 애플의 문서를보고 있었지만 실제로 그것을 이해하는 방법을 모른다.

누군가 내 코드에이 작업을 수행하기 위해 필요한 것을 말해 줄 수 있습니까? 여기

내 코드입니다 : 당신을 위해 처리되지 슬쩍 끌어와 같은

#import "squareViewController.h" 
static UIButton *matrix[2][2]; 
static int firstButtonRow, firstButtonColumn, secondButtonRow, secondButtonColumn; 
static BOOL isFirstTouch = YES; 
static CGPoint gestureStartPoint; 
@implementation squareViewController 
@synthesize diamond; 
@synthesize heart; 
@synthesize spade; 
@synthesize club; 

- (void)viewDidLoad { 


    matrix[0][0] = [diamond retain]; 
    matrix[0][1] = [heart retain]; 
    matrix[1][0] = [spade retain]; 
    matrix[1][1] = [club retain]; 

} 

- (void)ButtonTapped:(id)sender{ 
    int row = 0, column = 0; 
    for(row = 0; row < 2; row++){ 
     for(column = 0; column < 2; column++){ 
      if (matrix[row][column] == sender){ 
       if (isFirstTouch == YES){ 
        firstButtonRow = row; 
        firstButtonColumn = column; 
        isFirstTouch = NO; 
        return; 

       } 
       else{ 
        secondButtonRow = row; 
        secondButtonColumn = column; 
        [self SwapButtons: firstButtonRow: 
        firstButtonColumn: 
         secondButtonRow: 
        secondButtonColumn]; 
        isFirstTouch = YES;  
        return; 



       } 
      } 

     } 
    } 
} 

- (void)ButtonSwiped:(id)sender { 

    // ????? 

} 

- (void)ButtonDragged:(id)sender { 

    // ????? 

} 

- (void)SwapButtons:(int)firstRow: (int)firstColumn: (int)secondRow: (int)secondColumn{ 
    UIButton *tempButton = [[[UIButton alloc] init] retain]; // allocate memory for tempButton 
    UIButton *tempButtonOrig = tempButton; // mirror tempButton to preserve adderess for release; 
    [UIView beginAnimations:@"main" context:nil]; 
    tempButton.frame = matrix[firstRow][firstColumn].frame; 
    matrix[firstRow][firstColumn].frame 
    = matrix[secondRow][secondColumn].frame; 
    matrix[secondRow][secondColumn].frame = tempButton.frame; 
    tempButton = matrix[firstRow][firstColumn]; 
    matrix[firstRow][firstColumn] 
    = matrix[secondRow][secondColumn]; 
    matrix[secondRow][secondColumn] = tempButton; 
    [tempButtonOrig release];  
    [UIView setAnimationDelegate:self]; 
    [UIView commitAnimations];  
} 

답변

1

제스처; 당신은 꽤 많은 일을 스스로해야합니다. 내 응용 프로그램에서이 작업을 수행하려면 UIView 하위 클래스를 만들었습니다. 다음 오버라이드 (override) 할 수 있습니다 : 피사체 herehere

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 

몇 임의 자습서.

+0

내가 그것을 넘기면 "Touch Up Inside"이벤트가 여전히 자동으로 처리 되나요? 아니면 내가 추적해야합니까? 또한 (ID) 발신인을 테스트 할 수 있습니까? 스 와이프 및 드래그를 할 수 있습니까? –

관련 문제