2012-10-30 97 views
0

我已經創建了一個照片幻燈片放映應用程序,其中來自數組的圖像顯示在滾動視圖中。 我已經添加觸摸事件it.On觸摸它應該是在UIimageView上觸摸的圖像的詳細視圖 但我沒有通過鼠標點擊(我在模擬器上運行它),但我通過ALT +鼠標點擊 - 當時有兩點就像在地圖上放大一樣 我知道這是正確的方式,所以如何在單擊鼠標時獲得適當的觸摸?如何在圖像視圖上通過觸摸事件進行詳細視圖?

添加代碼

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

請更好地(一致地)格式化你的代碼,這很難閱讀。 – 2012-10-30 13:57:51

回答

0

而是自己在touchesBegan:方法執行所有命中的測試,我會高度電子書籍使用UITapGestureRecognizer。像這樣修改你的代碼:

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

好點。 thx,完成。 – Tobi

相關問題