2012-10-30 2 views
0

배열의 이미지가 포함 된 사진 슬라이드 쇼 앱이 하나 만들었습니다. 스크롤 뷰에 표시됩니다. 터치 이벤트를 추가했습니다. 터치하면 UIimageView 에서 터치 한 이미지의 세부보기가됩니다.하지만 마우스 클릭으로 (나는 시뮬레이터에서 실행 중입니다.) 알지 못하지만 alt + 마우스 클릭 - 그 때지도에서 확대/축소와 마찬가지로 두 가지 점이 있습니다. 올바른 방법이 있다는 것을 알고 있습니다. 그렇다면 마우스 클릭 한 번으로 적절한 접촉을 얻으려면 어떻게해야합니까?이미지보기에서 터치 이벤트로 상세보기로 이동하는 방법?

touchesBegan: 방법의 모든 히트 테스트를 직접 수행하는

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    scrollView.delegate = self; 
    scrollView.scrollEnabled = YES; 
    int scrollWidth = 120; 
    scrollView.contentSize = CGSizeMake(scrollWidth,80); 

    int xOffset = 0; 
    imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]]; 

    for(int index=0; index < [imagesName count]; index++) 
    { 
     UIImageView *img = [[UIImageView alloc] init]; 
     img.bounds = CGRectMake(10, 10, 50, 50); 
     img.frame = CGRectMake(5+xOffset, 0, 160, 110); 
     NSLog(@"image: %@",[imagesName objectAtIndex:index]); 
     img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]]; 
     [images insertObject:img atIndex:index]; 



     scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); 
     [scrollView addSubview:[images objectAtIndex:index]]; 

     xOffset += 170; 
    } 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [self.nextResponder touchesBegan:touches withEvent:event]; 

    UITouch * touch = [[event allTouches] anyObject]; 

    for(int index=0;index<[images count];index++) 
    { 
     UIImageView *imgView = [images objectAtIndex:index]; 


     NSLog(@"x=%f,y=%f,width=%f,height=%f",  
    imgView.frame.origin.x,imgView.frame.origin.y, 
    imgView.frame.size.width,imgView.frame.size.height); 
    NSLog(@"x= %f,y=%f",[touch locationInView:self.view].x,[touch  
    locationInView:self.view].y) ; 


     if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollView])) 
     { 
      [self ShowDetailView:imgView]; 
      break; 
     } 
    } 
} 

-(void)ShowDetailView:(UIImageView *)imgView 
{ 
    imageView.image = imgView.image; 
} 
+0

코드를 더 일관성있게 포맷하십시오. 읽기가 어렵습니다. –

답변

0

대신에 코드를 추가, 나는 매우UITapGestureRecognizer를 사용하는 recomment 것이다. 다음과 같이 코드를 수정하십시오 :

- (void)viewDidLoad 
{ 
    //... 

    for(int index=0; index < [imagesName count]; index++) 
    { 
     UIImageView *img = [[UIImageView alloc] init]; 
     img.bounds = CGRectMake(10, 10, 50, 50); 
     img.frame = CGRectMake(5+xOffset, 0, 160, 110); 
     NSLog(@"image: %@",[imagesName objectAtIndex:index]); 
     img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]]; 
     [images insertObject:img atIndex:index]; 

     UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)] 

     [img addGestureRecognizer:tapGR]; 
     img.userInteractionEnabled = YES; 

     /... 

    } 
} 

- (void)handleTap:(UIGestureRecognizer *)sender 
{ 
    UIImageView *iv = sender.view; 
    [self ShowDetailView:iv]; 
} 
+0

"img.userInteractionEnabled = YES;"를 추가 할 수 있습니다. 귀하의 코드에 – Oscar

+0

좋은 지적입니다. 끝났어. – Tobi

관련 문제