J'ai quatre UIViews sur un UIScrollView (écran divisé en quartiles)objet le plus proche de CGPoint
Sur les quartiles, j'ai quelques objets (UIImageViews), sur chaque quartile.
Lorsque l'utilisateur appuie sur l'écran, je veux trouver l'objet le plus proche du CGPoint donné?
Des idées? J'ai le CGPoint et l'image (CGRect) des objets dans chaque quartile.
MISE À JOUR:
hRuler http://img.skitch.com/20100824-c333thq3rjcak9f5q5djjugjhw.preview.jpgPins rouges sont 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]);
}
Console de débogage
est ici le problème. Chaque tuile est 256x256. Le CGPoint du premier UIImageView converti au système de coordonnées UIScrollView (53.25, 399.36) devrait être mort avec le tapPoint (30,331). Pourquoi la différence ?? L'autre point à la droite du point exploité est le calcul plus proche (distance sage) ??
<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??????
Cela va essentiellement d'exiger un tas de mathématiques. Vos UIImageViews sont-ils de la même taille? –
Oui, ils le sont. CGRect 26.624 71.68 32 39. Tous de la même taille. – Jordan
Ajout de la mise à jour avec Image et du code de Kalle. Il se passe quelque chose d'étrange quand je convertis le CGPoint de UIImageView en un système de coordonnées UIScrollViews qui rejette les coordonnées et le code de Kalle. Quelqu'un peut-il voir ce que je manque? – Jordan