Azerue
Azerue

Reputation: 368

finding locations of subview in superview

Hello i have superview in which i have many UIImageViews.I have added UIPanGesture to the SuperView which is 'View.m' in my case. I need to get the reference or in others words object of that UIImageView(subview) when user drags over it in superview.Also if this can be done with 'hittest' then let me know how to use it in gestureHandler and as hittest returns UIView , how i can convert UiView to UIImageView. Here is my code

//
//  View.m
//  PuzzleGame
//
//  Created by Noman Khan on 8/29/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "View.h"
#define  noOfRows 5
#define  noOfCols 4
@implementation View
@synthesize imageArray;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }

    return self;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

- (void)initImageView {
    int x = 1;
    int y = 1;
//    int width = 190;
//    int height = 198;
    int width = sizeOfRows;
    int height = sizeOfCols;

    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(x,y,width-10,height+5)]; 
    imgView.image=[UIImage imageNamed:@"iceage_01.png"];
    [self addSubview:imgView];

    x=x+(width-9);
    y=y+(sizeOfCols+9);

    UIImageView *imgView1=[[UIImageView alloc]initWithFrame:CGRectMake(x,y,width-10,height+5)]; 
    imgView1.image=[UIImage imageNamed:@"iceage_02.png"];
    [self addSubview:imgView1];





- (void)drawRect:(CGRect)rect
{
    imageArray=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"iceage_01.png"],[UIImage imageNamed:@"iceage_02.png"],[UIImage imageNamed:@"iceage_03.png"],[UIImage imageNamed:@"iceage_04.png"],[UIImage imageNamed:@"iceage_05.png"],[UIImage imageNamed:@"iceage_06.png"],[UIImage imageNamed:@"iceage_07.png"],[UIImage imageNamed:@"iceage_08.png"],[UIImage imageNamed:@"iceage_09.png"],[UIImage imageNamed:@"iceage_10.png"],[UIImage imageNamed:@"iceage_11.png"],[UIImage imageNamed:@"iceage_12.png"],[UIImage imageNamed:@"iceage_13.png"],[UIImage imageNamed:@"iceage_14.png"],[UIImage imageNamed:@"iceage_15.png"],[UIImage imageNamed:@"iceage_16.png"],[UIImage imageNamed:@"iceage_17.png"],[UIImage imageNamed:@"iceage_18.png"],[UIImage imageNamed:@"iceage_19.png"],[UIImage imageNamed:@"iceage_20.png"], nil];
    CGContextRef content=UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(content, 2);
    CGContextSetStrokeColorWithColor(content, [UIColor whiteColor].CGColor);

    int totalWidth=self.frame.size.width;
    int totalHeight=self.frame.size.height;

     sizeOfRows=totalHeight/noOfRows;
     sizeOfCols=totalWidth/noOfCols;
    //making rows
    int x = 0,y = 0;

    for (int i=0; i<noOfRows+1; i++) {
        CGContextMoveToPoint(content, x, y);
        CGContextAddLineToPoint(content, 1000, y);
        CGContextStrokePath(content);
        //y=y+200;
        y=y+sizeOfRows;
    }
    //making colums
    x=0,y=0;
    for (int i=0; i<noOfCols+1; i++) {
        CGContextMoveToPoint(content, x, y);
        CGContextAddLineToPoint(content, x, 1000);
        CGContextStrokePath(content);
        //x=x+192;
        x=x+sizeOfCols;
    }

    [self initImageView];

    UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panHandler:)];
    [self addGestureRecognizer:panGesture];
    //    CGContextMoveToPoint(content, 20, 20);
    //    CGåContextAddLineToPoint(content, 50, 50);
    //    
    //    CGContextStrokePath(content);
}



- (void)panHandler:(UIGestureRecognizer *)sender{

    CGPoint point=[sender locationInView:self];

    if (sender.state == UIGestureRecognizerStateBegan) {
        NSLog(@"BEgin");
        UIView *v=[[UIView alloc]initWithFrame:CGRectMake(75, 75, 100, 100)];
     v   = [self hitTest:point withEvent:nil];
      //  UIImageView *imv=(UIImageView*)v;   
        [self addSubview:v];
    }
    if (sender.state == UIGestureRecognizerStateChanged) {
        NSLog(@"moved");
    }
    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"end");
    }
   // NSLog(@"In Pan Handler");

}

@end

Upvotes: 2

Views: 478

Answers (1)

Ehsan
Ehsan

Reputation: 416

Yes you can use hittest. Hittest works in recursive fashion, below are some important points about hittest. 1. It calls pointInside:withEvent: of self 2. If it ,hitTest:withEvent:, returns nil. your view hierarchy is end. you dont have further views. 3. If it returns YES, the message is passed to subviews, it starts from the top-level subview, and continues to other views until a subview returns a non-nil object or all subviews receive the message. 4. If no nil object is returned nil, the self is returned

UIImageView is the subclass of UIView so you can cast your hittest returned view to UIImageView:
UIImageView *imageView = (UIImageView *)[[UIView alloc]initWithFrame:CGRectMake(75, 75, 100, 100)];

You can use Tag property of view for much better handling.

Upvotes: 2

Related Questions