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 4539010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:53:15+00:00 2026-05-21T14:53:15+00:00

@interface UIDraggableImageView : UIImageView { } .m file – (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { //

  • 0
@interface UIDraggableImageView : UIImageView {

}

.m file

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
// Retrieve the touch point
CGPoint point = [[touches anyObject] locationInView:self];
startLocation = point;
[[self superview] bringSubviewToFront:self];
}

 - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
CGPoint point = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += point.x - startLocation.x;
frame.origin.y += point.y - startLocation.y;
[self setFrame:frame];
}

took this code off the web for the draggable image,

Problem: image is irregular shape with transparent areas, clicking on transparent areas drags it as well.

Required Solution: How to make the transparent areas non interactive/non draggable?

Any suggestions, I will be trying to mask the image as an attempt and will post the results, but any workarounds/advise.

Further to suggestions by MiRAGe: Trying to incorporate the code in one class file, since image property is available in UIImageView and it would be easier to plug and play with any UIImageView in interface builder, but still having problems, transparent areas are movable, hitTest method gets called several times on a single click, any advise?

#import "UIImageViewDraggable.h"

@implementation UIImageViewDraggable

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Retrieve the touch point
CGPoint point = [[touches anyObject] locationInView:self];
startLocation = point;
[[self superview] bringSubviewToFront:self];
}

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
// Move relative to the original touch point
CGPoint point = [[touches anyObject] locationInView:self];
CGRect frame = [self frame];
frame.origin.x += point.x - startLocation.x;
frame.origin.y += point.y - startLocation.y;
[self setFrame:frame];
}

- (NSData *)alphaData {
CGContextRef    cgctx = NULL;
void *          bitmapData;
int             bitmapByteCount;

size_t pixelsWide = CGImageGetWidth(self.image.CGImage);
size_t pixelsHigh = CGImageGetHeight(self.image.CGImage);

bitmapByteCount     = (pixelsWide * pixelsHigh);

bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL) 
    return nil;

cgctx = CGBitmapContextCreate (bitmapData,
                               pixelsWide,
                               pixelsHigh,
                               8,
                               pixelsWide,
                               NULL,
                               kCGImageAlphaOnly);
if (cgctx == NULL) {
    free (bitmapData);
    fprintf (stderr, "Context not created!");

    return nil;
}

CGRect rect = {{0,0},{pixelsWide,pixelsHigh}}; 
CGContextDrawImage(cgctx, rect, self.image.CGImage); 

unsigned char *data = CGBitmapContextGetData(cgctx);

CGContextRelease(cgctx);

if (!data) {
    free(bitmapData);
    return nil;
}

size_t dataSize = pixelsWide * pixelsHigh;

NSData *alphaData = [NSData dataWithBytes:data length:dataSize];

free(bitmapData);
return alphaData;
}    

- (BOOL)isTransparentLocation:(CGPoint)point withData:(NSData *)data {   
if (data == nil)
    NSLog(@"data was nil");

NSUInteger index = point.x + (point.y * [self.image size].width);
unsigned char *rawDataBytes = (unsigned char *)[data bytes];

return (rawDataBytes[index] == 0);
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
NSLog(@"test");
NSAutoreleasePool *pool = [NSAutoreleasePool new];

// view responding to the hit test. note that self may respond too.
UIView *anyViewResponding = [super hitTest:point withEvent:event];  
if( anyViewResponding == nil || anyViewResponding == self ) {
    // convert the point in the image, to a global point.
    CGPoint framePoint = [self.superview convertPoint:point fromView:self];
    // if the point is in the image frame, and there is an image, see if we need to let the touch through or not
    if(self.image != nil && CGRectContainsPoint([self frame], framePoint)) {
        NSData *imageData = [self alphaData];         

        // check if the point touched is transparent in the image
        if( imageData != nil && [self isTransparentLocation:point withData:imageData]) {               
            // return nil, so the touch will not arrive at this view
            anyViewResponding = nil;
        }
    }
}

[pool drain];
return anyViewResponding;
}
  • 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-21T14:53:16+00:00Added an answer on May 21, 2026 at 2:53 pm

    You can easily detect alpha area’s and make them non draggable. Here is some code that will allow you to detect alpha area’s. It might be some overhead for you, but it’s the best I could do.

    I have subclassed UIImage and put this code in the implementation file.

    #import <CoreGraphics/CoreGraphics.h>
    
    - (NSData *)alphaData
    {
        CGContextRef    cgctx = NULL;
        void *          bitmapData;
        int             bitmapByteCount;
    
        size_t pixelsWide = CGImageGetWidth(self.CGImage);
        size_t pixelsHigh = CGImageGetHeight(self.CGImage);
    
        bitmapByteCount     = (pixelsWide * pixelsHigh);
    
        bitmapData = malloc( bitmapByteCount );
        if (bitmapData == NULL) 
            return nil;
    
        cgctx = CGBitmapContextCreate (bitmapData,
                                       pixelsWide,
                                       pixelsHigh,
                                       8,
                                       pixelsWide,
                                       NULL,
                                       kCGImageAlphaOnly);
        if (cgctx == NULL)
        {
            free (bitmapData);
            fprintf (stderr, "Context not created!");
    
            return nil;
        }
    
        CGRect rect = {{0,0},{pixelsWide,pixelsHigh}}; 
        CGContextDrawImage(cgctx, rect, self.CGImage); 
    
        unsigned char *data = CGBitmapContextGetData(cgctx);
    
        CGContextRelease(cgctx);
    
        if (!data)
        {
            free(bitmapData);
            return nil;
        }
    
        size_t dataSize = pixelsWide * pixelsHigh;
    
        NSData *alphaData = [NSData dataWithBytes:data length:dataSize];
    
        free(bitmapData);
        return alphaData;
    }    
    
    - (BOOL)isTransparentLocation:(CGPoint)point withData:(NSData *)data
    {   
        if (data == nil)
            NSLog(@"data was nil");
    
        NSUInteger index = point.x + (point.y * [self size].width);
        unsigned char *rawDataBytes = (unsigned char *)[data bytes];
    
        return (rawDataBytes[index] == 0);
    }
    

    Now in a subclass of UIImageView (I use the hitTest function to allow detection, but you could easily change this into something that works for you, this is just an example) I put this code to detect if the point hit was transparent or not. If it is transparent, we pass the touch onto the view below, otherwise we keep the touch to ourselves.

    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
    {
        NSAutoreleasePool *pool = [NSAutoreleasePool new];
    
        // view responding to the hit test. note that self may respond too.
        UIView *anyViewResponding = [super hitTest:point withEvent:event];  
        if( anyViewResponding == nil || anyViewResponding == self )
        {
            // convert the point in the image, to a global point.
            CGPoint framePoint = [self.superview convertPoint:point fromView:self];
            // if the point is in the image frame, and there is an image, see if we need to let the touch through or not
            if( self.image != nil && CGRectContainsPoint([self frame], framePoint) )
            {
                NSData *imageData = [self.image alphaData];         
    
                // check if the point touched is transparent in the image
                if( imageData != nil && [self.image isTransparentLocation:point imageData] )
                {               
                    // return nil, so the touch will not arrive at this view
                    anyViewResponding = nil;
                }
            }
        }
    
        [pool drain];
        return anyViewResponding;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Interface Builder can be used for basic dependency injection in a Cocoa app, but
The Interface Segregation Principle (ISP) says that many client specific interfaces are better than
An interface is a 100% abstract class, so we can use an interface for
In interface builder i've created a UITextfield within a subclass of a UITableViewCell. The
public interface IBar {} public interface IFoo : IBar {} typeof(IFoo).BaseType == null How
If I have interface IFoo, and have several classes that implement it, what is
I have an Interface called IStep that can do some computation (See Execution in
I see many user interface control libraries for .NET, but where can I get
I have a user interface in .net which needs to receive data from a
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {

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.