Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6596289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:03:22+00:00 2026-05-25T18:03:22+00:00

When creating a Draggable UIButton , the dragging events work as they should, however,

  • 0

When creating a Draggable UIButton, the dragging events work as they should, however, I am unable to figure out why the UIControlEvents do not respond when the button is simply pressed and not dragged.

Creating the Button

DraggableButton * camera = [DraggableButton buttonWithType:UIButtonTypeCustom];
[camera setFrame:CGRectMake(160,5,149,40)];
[camera setImage: [UIImage imageWithContentsOfFile:cameraMode] forState:UIControlStateNormal];
[camera setTitle: @"Camera" forState:UIControlStateNormal];  // never gets called.
[camera addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: camera];

Draggable Button

@interface DraggableButon : UIButton
{
@private float deltaX;
@private float deltaY;
    CGPoint startLocation;
    BOOL    dragging;
}
@end
@implementation DraggableButon
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesMoved:touches withEvent:event];

    if ( ! draggable ) return;

    dragging = YES;
    camera.enabled = NO;
    flash.enabled = NO;

    // Calculate offset
    CGPoint pt = [[[event allTouches] anyObject] locationInView:buttonView];
    deltaX = pt.x - startLocation.x;
    deltaY = pt.y - startLocation.y;

    float xx;
    if ( IPAD ) { xx = buttonView.center.x + deltaX; } else { xx = 160; }
    CGPoint newcenter = CGPointMake(xx, buttonView.center.y + deltaY);

    // Set new location
    buttonView.center = newcenter;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    [super touchesBegan:touches withEvent:event];

    if ( ! draggable ) return;

    // Calculate and store offset, and pop view into front if needed
    CGPoint pt = [[[event allTouches] anyObject] locationInView:buttonView];
    startLocation = pt;
    [[self superview] bringSubviewToFront:buttonView];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{        
    [super touchesEnded:touches withEvent:event];

    dragging = YES;
    if ( dragging )
    {
        camera.enabled = YES;
        flash.enabled = YES;

        //NSLog(@"touchesEnded: buttonView.frame.origin.x: %3.2f, buttonView.frame.origin.y: %3.2f",buttonView.frame.origin.x,buttonView.frame.origin.y);
        rectFromString = [NSString stringWithFormat:@"{{%3.2f,%3.2f},{320,50}}",buttonView.frame.origin.x,buttonView.frame.origin.y]; 
        NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: SETTINGS_FILE];
        [dict setObject:rectFromString forKey:@"kCGRectFromString"];
        [dict writeToFile: SETTINGS_FILE atomically:YES];
    }else{
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    [super touchesCancelled:touches withEvent:event];
}
@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:03:23+00:00Added an answer on May 25, 2026 at 6:03 pm

    the action is not called because you overwrite the touch events and you’re not sending them further to super. You can add a new variable (let’s call it dragged). Set this to NO at touchesBegan:, to YES at touchesMoved: and at touchesEnded:, if it’s set to NO call [self sendActionsForControlEvents:UIControlEventTouchUpInside];

    @interface DraggableButon : UIButton
    {
    @private float deltaX;
    @private float deltaY;
        CGPoint startLocation;
        BOOL    dragging;
    }
    @end
    @implementation DraggableButon
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        dragging=NO;
    
        if ( ! draggable ) return;
    
        // Calculate and store offset, and pop view into front if needed
        CGPoint pt = [[[event allTouches] anyObject] locationInView:buttonView];
        startLocation = pt;
        [[self superview] bringSubviewToFront:buttonView];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        dragging=YES;
    
        if ( ! draggable ) return;
    
        camera.enabled = NO;
        flash.enabled = NO;
    
        // Calculate offset
        CGPoint pt = [[[event allTouches] anyObject] locationInView:buttonView];
        deltaX = pt.x - startLocation.x;
        deltaY = pt.y - startLocation.y;
    
        float xx;
        if ( IPAD ) { xx = buttonView.center.x + deltaX; } else { xx = 160; }
        CGPoint newcenter = CGPointMake(xx, buttonView.center.y + deltaY);
    
        // Set new location
        buttonView.center = newcenter;
    }
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    {        
        if(!dragging){  // or if(!dragging||!draggable)
            [self sendActionsForControlEvents:UIControlEventTouchUpInside];
            return;
        }
    
        camera.enabled = YES;
        flash.enabled = YES;
    
        //NSLog(@"touchesEnded: buttonView.frame.origin.x: %3.2f, buttonView.frame.origin.y: %3.2f",buttonView.frame.origin.x,buttonView.frame.origin.y);
        rectFromString = [NSString stringWithFormat:@"{{%3.2f,%3.2f},{320,50}}",buttonView.frame.origin.x,buttonView.frame.origin.y]; 
        NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile: SETTINGS_FILE];
        [dict setObject:rectFromString forKey:@"kCGRectFromString"];
        [dict writeToFile: SETTINGS_FILE atomically:YES];
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The app I am currently developing uses draggable UIButtons created programmatically. They work very
jQuery UI Sortable + Draggable 1.6rc5 WHAT I AM DOING: Creating a calendar with
I'm creating a custom drag helper (in jQuery): $('.dragme', element).draggable({ appendTo: 'body', helper :
After creating a div on the fly with this markup: $('.circuit').prepend(<div class='component' draggable='true'>TRANSISTOR</div>); It
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
I'm creating a draggable/resizable menu with all the neccesary links to control the website,
I'm creating a day planner using jquery, draggable and droppable. When a job is
I'm planning on creating an app which will utilise Phonegap, however the app is
I am creating a widget similar to Google search widget for android. How're they
I'm creating custom window title. I did it with requestWindowFeature and setFeatureInt but not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.