2012-08-30 3 views
2

안녕하세요 저는 많은 UIImageViews.I가있는 Superview를 가지고 있습니다. 내보기에는 'View.m'인 SuperView에 UIPanGesture를 추가했습니다. 나는 참조를 얻거나 다른 사람이 그 UIImageView (subview)의 개체를 superview.It에서 그것을 끌어다 놓을 때 'hittest'로 할 수 있다면 gestureHandler에서 사용하는 방법을 알려주고 hittest가 UIView를 반환 할 때이를 알릴 필요가있다. , 어떻게 UiView로 UiView로 변환 할 수 있습니다. 여기 내 코드는수퍼 뷰에서 서브 뷰의 위치를 ​​찾는 것

// 
// 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 

답변

2

예. hittest를 사용할 수 있습니다. Hittest는 재귀 적 방식으로 작동하며 아래는 적중 테스트에 대한 몇 가지 중요한 사항입니다. 1. pointInside : withEvent : of self를 이라고합니다. 2. hitTest : withEvent :가 null이면 nil을 반환합니다. 뷰 계층 구조가 끝났습니다. 당신은 더 이상의 견해가 없다. 3. YES를 반환하면 메시지는 하위보기로 전달되고 최상위 하위보기에서 시작되며 하위보기가 0이 아닌 개체를 반환하거나 모든 하위보기가 메시지를받을 때까지 다른보기로 계속 진행됩니다.
있는 UIImageView * 이미지 뷰 = (있는 UIImageView *) [[UIView의 ALLOC : 더 전무 객체가 nil을 반환하지 않으면, 자기가

있는 UIImageView를 반환 4. 당신이있는 UIImageView에 그러나 hitTest 돌아보기를 캐스팅 수있는 UIView의 서브 클래스 ] initWithFrame : CGRectMake (75, 75, 100, 100)];

더 나은 처리를 위해보기의 태그 속성을 사용할 수 있습니다.

+0

K 그리고 제스처 처리기를 사용하고 있다면 hittest가 hittest 객체를 필요로하므로 UIEvent의 get 객체를 얻습니다. – Azerue

+0

UITouches와 관련된 다음 메서드를 재정 의하여 UIEvent 객체를 가져올 수 있습니다. - (void) touchesBegan : (NSSet *) withEvent : (UIEvent *) 이벤트; - (void) touchesMoved : (NSSet *)은 withEvent : (UIEvent *) 이벤트를 터치합니다. - (void) touchesEnded : (NSSet *)은 withEvent : (UIEvent *) 이벤트를 터치합니다. - (void) touchesCancelled : (NSSet *)은 withEvent : (UIEvent *) 이벤트를 터치하고이 이벤트를 hitTest에 전달합니다. hit는 nil도 hitTest에 전달할 수 있습니다. – Ehsan

관련 문제