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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:27:39+00:00 2026-05-16T12:27:39+00:00

I have four UIViews on a UIScrollView (screen divided into quartiles) On the quartiles,

  • 0

I have four UIViews on a UIScrollView (screen divided into quartiles)

On the quartiles, I have a few objects (UIImageViews), on each quartile.

When the user taps the screen, I want to find the closest object to the given CGPoint?

Any ideas?

I have the CGPoint and frame (CGRect) of the objects within each quartile.

UPDATE:

hRuler
(source: skitch.com)

Red Pins are UIImageViews.

    // UIScrollView
    NSLog(@" UIScrollView: %@", self);

    // Here's the tap on the Window in UIScrollView's coordinates
    NSLog(@"TapPoint: %3.2f, %3.2f", tapLocation.x, tapLocation.y);

    // Find Distance between tap and objects
    NSArray *arrayOfCGRrectObjects = [self subviews];
    NSEnumerator *enumerator = [arrayOfCGRrectObjects objectEnumerator];

    for (UIView *tilesOnScrollView in enumerator) {

        // each tile may have 0 or more images
        for ( UIView *subview in tilesOnScrollView.subviews ) {

            // Is this an UIImageView?
            if ( [NSStringFromClass([subview class]) isEqualToString:@"UIImageView"]) {

                // Yes, here are the UIImageView details (subView)
                NSLog(@"%@", subview);

                // Convert CGPoint of UIImageView to CGPoint of UIScrollView for comparison...
                // First, Convert CGPoint from UIScrollView to UIImageView's coordinate system for reference
                CGPoint found =  [subview convertPoint:tapLocation fromView:self];
                NSLog(@"Converted Point from ScrollView: %3.2f, %3.2f", found.x, found.y);

                // Second, Convert CGPoint from UIScrollView to Window's coordinate system for reference
                found =  [subview convertPoint:subview.frame.origin toView:nil];
                NSLog(@"Converted Point in Window: %3.2f, %3.2f", found.x, found.y);

                // Finally, use the object's CGPoint in UIScrollView's coordinates for comparison
                found =  [subview convertPoint:subview.frame.origin toView:self]; // self is UIScrollView (see above)
                NSLog(@"Converted Point: %3.2f, %3.2f", found.x, found.y);

                // Determine tap CGPoint in UIImageView's coordinate system
                CGPoint localPoint = [touch locationInView:subview];
                NSLog(@"LocateInView: %3.2f, %3.2f",localPoint.x, localPoint.y );

               //Kalle's code
                    CGRect newRect = CGRectMake(found.x, found.y, 32, 39);
                    NSLog(@"Kalle's Distance: %3.2f",[self distanceBetweenRect:newRect andPoint:tapLocation]);

            }

Debug Console

Here’s the problem. Each Tile is 256×256. The first UIImageView’s CGPoint converted to the
UIScrollView’s coordinate system (53.25, 399.36) should be dead on with the tapPoint (30,331). Why the difference?? The other point to the right of the tapped point is calculating closer (distance wise)??

<CALayer: 0x706a690>>
[207] TapPoint: 30.00, 331.00
[207] <UIImageView: 0x7073db0; frame = (26.624 71.68; 32 39); opaque = NO; userInteractionEnabled = NO; tag = 55; layer = <CALayer: 0x70747d0>>
[207] Converted Point from ScrollView: 3.38, 3.32
[207] Converted Point in Window: 53.25, 463.36
[207] Converted Point: 53.25, 399.36 *** Looks way off!
[207] LocateInView: 3.38, 3.32
[207] Kalle's Distance: 72.20 **** THIS IS THE TAPPED POINT
[207] <UIImageView: 0x7074fb0; frame = (41.984 43.008; 32 39); opaque = NO; userInteractionEnabled = NO; tag = 55; layer = <CALayer: 0x7074fe0>>
[207] Converted Point from ScrollView: -11.98, 31.99
[207] Converted Point in Window: 83.97, 406.02
[207] Converted Point: 83.97, 342.02
[207] LocateInView: -11.98, 31.99
207] Kalle's Distance: 55.08 ***** BUT THIS ONE's CLOSER??????
  • 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-16T12:27:39+00:00Added an answer on May 16, 2026 at 12:27 pm

    The following method should do the trick. If you spot anything weird in it feel free to point it out.

    - (CGFloat)distanceBetweenRect:(CGRect)rect andPoint:(CGPoint)point
    {
        // first of all, we check if point is inside rect. If it is, distance is zero
        if (CGRectContainsPoint(rect, point)) return 0.f;
    
        // next we see which point in rect is closest to point
        CGPoint closest = rect.origin;
        if (rect.origin.x + rect.size.width < point.x)
            closest.x += rect.size.width; // point is far right of us
        else if (point.x > rect.origin.x) 
            closest.x = point.x; // point above or below us
        if (rect.origin.y + rect.size.height < point.y) 
            closest.y += rect.size.height; // point is far below us
        else if (point.y > rect.origin.y)
            closest.y = point.y; // point is straight left or right
    
        // we've got a closest point; now pythagorean theorem
        // distance^2 = [closest.x,y - closest.x,point.y]^2 + [closest.x,point.y - point.x,y]^2
        // i.e. [closest.y-point.y]^2 + [closest.x-point.x]^2
        CGFloat a = powf(closest.y-point.y, 2.f);
        CGFloat b = powf(closest.x-point.x, 2.f);
        return sqrtf(a + b);
    }
    

    Example output:

    CGPoint p = CGPointMake(12,12);
    
    CGRect a = CGRectMake(5,5,10,10);
    CGRect b = CGRectMake(13,11,10,10);
    CGRect c = CGRectMake(50,1,10,10);
    NSLog(@"distance p->a: %f", [self distanceBetweenRect:a andPoint:p]);
    // 2010-08-24 13:36:39.506 app[4388:207] distance p->a: 0.000000
    NSLog(@"distance p->b: %f", [self distanceBetweenRect:b andPoint:p]);
    // 2010-08-24 13:38:03.149 app[4388:207] distance p->b: 1.000000
    NSLog(@"distance p->c: %f", [self distanceBetweenRect:c andPoint:p]);
    // 2010-08-24 13:39:52.148 app[4388:207] distance p->c: 38.013157
    

    There might be more optimized versions out there, so might be worth digging more.

    The following method determines the distance between two CGPoints.

    - (CGFloat)distanceBetweenPoint:(CGPoint)a andPoint:(CGPoint)b
    {
        CGFloat a2 = powf(a.x-b.x, 2.f);
        CGFloat b2 = powf(a.y-b.y, 2.f);
        return sqrtf(a2 + b2)
    }
    

    Update: removed fabsf(); -x^2 is the same as x^2, so it’s unnecessary.

    Update 2: added distanceBetweenPoint:andPoint: method too, for completeness.

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

Sidebar

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.