IPhone Drag Button Grid

I am looking to create a simple color palette and customize the grid of buttons in the interface builder.

I would like the user to be able to interact with the palette (buttons) by selecting any button, then dragging a finger between the buttons (activating events when entering and exiting the button and changing the selected color with each event) Is this possible?

For example, the user touches a blue button (the color is updated), then drags a finger to the green button (the color is updated).

Events "Touch Drag Enter" and "Touch Drag Exit" are displayed only if the original user selection was sent (does not allow dragging between two buttons). Thank!

+3
source share
3 answers

I created a simple application that does what you want. You can find the source at:

http://github.com/st3fan/iphone-experiments/tree/master/Miscellaneous/ColorPalette/

This is just a container view ( PaletteView) with a bunch of subzones that have specific colors. When it PaletteViewreceives a touch, it finds the view under the touch and then informs the delegate ( PaletteViewDelegate) that the color has changed.

Tested on iPhone 4 with 4.0.2.

+7
source

You can use the touchhesBegan + touchesMoved + touchesEnded methods to create a list of buttons affected by the user, from start to finish.

I wrote this on the fly and did not test it, but it could look something like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    // interactions is the NSMutableArray where you store the pressed/dragged buttons
    // we need to clear it when we start a new checkForInteractions
    [interactions removeAllObjects];

    UITouch *touch = [touches anyObject];
    [self checkForInteractions:touch];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [self checkForInteractions:touch];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    [self checkForInteractions:touch];
    // here you'd call a function that'd use the buttons on the array,
    // to do whatever you wanted with them
    // it can also be copied to touchesMoved, to trigger the interactions
    // as the user finger is moving, rather than only when it stops
    [self useInteractions];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{
    // if the touch was cancelled, clear the interactions
    [interactions removeAllObjects];
}
- (void) checkForInteractions : (UITouch *) touch
{
    if ([touch view] == myButton1 || [touch view] == myButton2)
    {
        // check if the object had already been added to the array
        if ( ![interactions containsObject:[touch view]] )
        {
            [interactions addObject:[touch view]];
        }
    }
}

, :)

+1

I think you might be lucky with UIGestureRecognizer

+1
source

Source: https://habr.com/ru/post/1761851/


All Articles