DP
DP

Reputation:

How do you swap UIButton positions by swiping and/or dragging?

I'm currently writing a program where I have UIButtons arranged in a square. in a NIB. I swap the buttons by taping one one then another using "Touch Up Inside" events.

I want to make them swap by swiping one in the direction of another and by dragging one on top of another. I haven't been able to figure out how to do this. Ive been looking in the Apple documentation for weeks but I really don't know how to make sense of it.

Can someone tell me what I need to dd to my code to do this?

Here is my code:

#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];      
}

Upvotes: 1

Views: 557

Answers (1)

zpasternack
zpasternack

Reputation: 17898

Gestures like swipe and drag aren't handled for you; you pretty much have to do the work yourself. In my app, I've made a UIView subclass to do this. You'll want to override the following:

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

A few random tutorials on the subject here and here.

Upvotes: 1

Related Questions