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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:59:52+00:00 2026-05-27T00:59:52+00:00

I have around 16 UIImage views, can be moved by touch. I can drop

  • 0

I have around 16 UIImage views, can be moved by touch. I can drop over UIImage over another UIImage and then the UIImages would exchange their position. Being newcomer to ObjectiveC programming, I am struggling to figure out the UIImageViews and exchange their positons. Till now I am trying to implement in touchesEnded method

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
[self.superview exchangeSubviewAtIndex:[[self.superview subviews] indexOfObject:self] withSubviewAtIndex:[[self.superview subviews] indexOfObject:[touch view]]];
}

But this does not work. Any help would be appreciated.

UIImages are created by following code
TouchImageView *touchImageView = [[TouchImageView alloc]initWithImage:displayImage];
        touchImageView.identy = [NSString stringWithFormat:@"Image ID %d",i]; 

So each touch image is associated some string to describe itself.

In touch end, I have just added following code to know ID of image views

UITouch *touch = [touches anyObject];
    TouchImageView *dragImage = (TouchImageView*)[touch view];
    NSLog(@"Ended  %a%a",[dragImage identy],[self identy]);

But the o/p I got is totally different

2011-11-21 11:50:34.404 OrganizeMe[882:f803] Ended  0x1.6d96006a6d96p-9170x1.807p-1022

FInal code

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

    __block int tag = -1;
    __block float distance = 100000.0;

    [[self.superview subviews] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger index, BOOL *stop) {
        BOOL interSect = CGRectIntersectsRect([self frame], [view frame]);

        if(interSect && ([self tag]!=[view tag])){
            [self.image CGImage];
            CGPoint currPoint = [[touches anyObject]locationInView:[self superview]];
            CGPoint underPoint = view.center;

            if(distance >= [self distanceBetweenPoint1:currPoint Point2:underPoint]){
                distance =  [self distanceBetweenPoint1:currPoint Point2:underPoint] ;
                tag = [view tag];
            }

        }
    }];
    NSLog(@"Tag and Distance %d,%f",tag,distance);
    TouchImageView* imageView1 = (TouchImageView*)[self.superview viewWithTag:tag];
    CGRect point1 = [imageView1 frame];
    CGRect point2 = [self frame];

    if(tag != -1){
        [imageView1 setFrame:point2];
        [self setFrame:point1];
    }else{
        CGPoint lastTouch = [[touches anyObject]previousLocationInView:[self superview]];
        self.center = lastTouch;
    }
    [self.superview setNeedsLayout];

}
  • 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-27T00:59:52+00:00Added an answer on May 27, 2026 at 12:59 am

    The index is just the UIView’s position in the subview array. It does not set the location within the superview.

    Relayout the superview based on the subview’s index.

    In your above method, also call setNeedsLayout in order to relayout the superview.

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [touches anyObject];
        [self.superview exchangeSubviewAtIndex:[[self.superview subviews] indexOfObject:self] withSubviewAtIndex:[[self.superview subviews] indexOfObject:[touch view]]];
        [self.superview setNeedsLayout]; // Should call the method below
    }
    

    Implement the following method for the superview:

    - (void)layoutSubviews
    {
        // Iterate over all subviews
        [self.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger index, BOOL *stop) {
            // This block will be executed for each subview. You have the subview's index.
            // Now set the subview's position accordingly to the index.
            // I don't know your layout logic, but let's assume you have 14 subviews spread over 4 columns, this will be the subviews' position:
            /*  0  1  2  3
                4  5  6  7
                8  9 10 11
               12 13         */
            int col = index % 4; // Column from 0 to 3
            int row = (int)(index / 4); // Row starting from 0
            int w = view.frame.size.width;
            int h = view.frame.size.height;
            CGPoint p = CGPointMake(col * w + w / 2, row * h + h / 2);
            [view setCenter:p];
        }];
    }
    

    It’s been a long time I last used Cocoa touch, so I can’t guarantee that the code is working.

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

Sidebar

Related Questions

I have around 3500 flood control facilities that I would like to represent as
I have a custom UIImageView, I can drag it around screen by making a
I currently have a UIImageView which I can move/drag around the view. How does
I have several uiimageviews on my view which the user can drag around. I
I have around 1000 pdf filesand I need to convert them to 300 dpi
I have around 25 worksheets in my workbook (Excel spreadsheet). Is there a way
I have around 8-9 parameters to pass in a function which returns an array.
I have around ~40 custom modules and a custom skin on a DotNetNuke site
I have around 400 GB Live mysql Databases on one server and I like
I have around 100 rows of text that I want to tokenize, which are

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.