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

  • SEARCH
  • Home
  • 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 7641041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:49:24+00:00 2026-05-31T08:49:24+00:00

I’m trying to achieve a Drag and Drop menu affect. I’m not sure how

  • 0

I’m trying to achieve a Drag and Drop menu affect. I’m not sure how to go about this, perhaps someone has experience with this exact effect.

Quite simply, when a user touches down on a menu item, I want a graphic to appear at their touch location. Their touch will now control the panning of the graphic. Upon releasing the touch, the graphic will sit in its place and assume full alpha.

I’m already familiar with creating pan gestures and instantiating a graphic. So far, I can create the graphic where the menu item is touched. The biggest issue is how I “pass over” the touch gesture so it is a single and continuous motion.

Also, should the menu item be UIButton or UIImageView?

Any help appreciated. Thanks
enter image description here

  • 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-31T08:49:26+00:00Added an answer on May 31, 2026 at 8:49 am

    I had some fun with this one. The following code will grab the image from the button when touched, drag that image at alpha=0.5, and drop it wherever your touches end at alpha=1.0. It will continue to be draggable thereafter.

    After importing QuartzCore, create a new file. The .h should read:

    #import <Foundation/Foundation.h>
    #import <QuartzCore/CAGradientLayer.h>
    #import <QuartzCore/CALayer.h>
    
    @interface DraggableImage : CAGradientLayer
    
    - (void)draw:(UIImage *)image;
    
    - (void)moveToFront;
    
    - (void)appearDraggable;
    
    - (void)appearNormal;
    
    @end
    

    and the .m should read:

    #import "DraggableImage.h"
    
    @implementation DraggableImage
    
    - (void)draw:(UIImage *)image{
        CGRect buttonFrame = self.bounds;
        int buttonWidth = buttonFrame.size.width;
        int buttonHeight = buttonFrame.size.height;
        UIGraphicsBeginImageContext( CGSizeMake(buttonWidth, buttonHeight) );
        [image drawInRect:self.bounds];
        UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        [newImage drawInRect:self.bounds];
    }
    
    - (void)moveToFront {
        CALayer *superlayer = self.superlayer;
        [self removeFromSuperlayer];
        [superlayer addSublayer:self];
    }
    
    - (void)appearDraggable {
        self.opacity = 0.5;
    }
    
    
    - (void)appearNormal {
        self.opacity = 1.0;
    }
    
    @end
    

    Now in your main view controller, add:

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    #import "DraggableImage.h"
    
    @interface YourViewController : UIViewController{
        DraggableImage *heldImage;
        DraggableImage *imageForFrame[5]; // or however many
        UIButton *buttonPressed;
        int imageCount;
    }
    @property (weak, nonatomic) IBOutlet UIButton *imageButton;
    -(IBAction)buildImageLayerForButton:(UIButton *)sender;
    - (void)moveHeldImageToPoint:(CGPoint)location;
    - (CALayer *)layerForTouch:(UITouch *)touch;
    

    The imageButton in this case would be your apple Button. Now in your .m file, add this:

    @synthesize imageButton;
    
    #pragma - mark Touches
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        CALayer *hitLayer = [self layerForTouch:[touches anyObject]];
        if ([hitLayer isKindOfClass:[DraggableImage class]]) {
            DraggableImage *image = (DraggableImage *)hitLayer;
    
            heldImage = image;
            [heldImage moveToFront];
        }
        hitLayer = nil;
        [super touchesBegan:touches withEvent:event];
    }
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
            if (heldImage) 
            {
                UITouch *touch = [touches anyObject];
                UIView *view = self.view;
                CGPoint location = [touch locationInView:view];
                [self moveHeldImageToPoint:location];
            }
    
    }
    
    - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        if (heldImage) {
            [heldImage appearNormal];
            heldImage = nil;
        }
    }
    
    - (void)dragBegan:(UIControl *)c withEvent:ev {
    }
    - (void)dragMoving:(UIControl *)c withEvent:ev {
        UITouch *touch = [[ev allTouches] anyObject];
        CGPoint touchPoint = [touch locationInView:self.view];
        [self moveHeldImageToPoint:touchPoint];
    }
    
    - (void)dragEnded:(UIControl *)c withEvent:ev {
        UITouch *touch = [[ev allTouches] anyObject];
        CGPoint touchPoint = [touch locationInView:self.view];
        [self moveHeldImageToPoint:touchPoint];
        [heldImage appearNormal];
        heldImage = nil;
    }
    
    
    
    -(IBAction)buildImageLayerForButton:(UIButton *)sender{
    
    
            DraggableImage *image = [[DraggableImage alloc] init];
            buttonPressed = sender;
            CGRect buttonFrame = sender.bounds;
            int buttonWidth = buttonFrame.size.width;
            int buttonHeight = buttonFrame.size.height;
    
            image.frame = CGRectMake(120, 24, buttonWidth*3, buttonHeight*3);
            image.backgroundColor = [UIColor lightGrayColor].CGColor;
            image.delegate = self;
            imageForFrame[imageCount] = image;
            [self.view.layer addSublayer:image];
            [image setNeedsDisplay];
            [image moveToFront];
            [image appearDraggable];
            heldImage = image;
            [self moveHeldImageToPoint:sender.center];
    
            imageCount++;
    
    }
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
        UIGraphicsPushContext(ctx);
    
        DraggableImage *image = (DraggableImage *)layer;
        [image draw:[buttonPressed imageForState:UIControlStateNormal]];
    
        UIGraphicsPopContext();
    }
    
    - (void)moveHeldImageToPoint:(CGPoint)location 
    {
        float dx = location.x;
        float dy = location.y;
        CGPoint newPosition = CGPointMake(dx, dy);
    
        [CATransaction begin];
        [CATransaction setDisableActions:TRUE];
        heldImage.position = newPosition;
        [CATransaction commit];
    }
    
    - (CALayer *)layerForTouch:(UITouch *)touch 
    {
        UIView *view = self.view;
    
        CGPoint location = [touch locationInView:view];
        location = [view convertPoint:location toView:nil];
    
        CALayer *hitPresentationLayer = [view.layer.presentationLayer hitTest:location];
        if (hitPresentationLayer) 
        {
            return hitPresentationLayer.modelLayer;
        }
    
        return nil;
    }
    
    -(void)viewDidLoad{
        [imageButton addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown];
        [imageButton addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside | UIControlEventTouchDragOutside];
        [imageButton addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
        [super viewDidLoad];
    }
    
    - (void)viewDidUnload {
        [self setImageButton:nil];
        [super viewDidUnload];
    }
    

    Et voila! Connect your button, set its image, and throw copies all over the screen. 🙂

    Note: I didn’t comment much, but would be happy to answer any questions.

    Cheers!

    EDIT: fixed the -(void)draw:(UIImage *)image{} so that it would resize the image properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.