2011-08-26 82 views
0

我正在爲iPad的孩子圖書應用程序工作。它有一個UIView加載UIImageView顯示UIImages(JPEG的),用戶可以在圖像上滑動瀏覽頁面 - 一切正常。現在我想添加一些交互性的一些頁面通過添加另一個UIImageView將加載一個PNG文件,並在我想要動畫他們的點擊手勢...下面是代碼片段...UIImageView標記和點擊手勢問題

我添加了一個Tap手勢到UIView裏面viewDidLoad。 viewDidLoad調用loadPage和loadPage內部我編程式地添加一個包含PNG文件的UIImageView(imageAnimation),併爲其分配一個標籤,以便我可以根據handleTap例程中的標籤播放動畫。由於某些原因,handleTap中的switch語句只針對case 1執行,對於其他情況,handleTap例程從不被調用。我在做什麼錯?

#import "KidsViewController.h" 

@implementation KidsViewController 
@synthesize imageAnimation; 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
    if ([touch.view isKindOfClass:[UISlider class]] || [touch.view isKindOfClass:[UIButton class]]) 
    { 
     return NO; 
    } 
    return YES; 
} 

- (void)handleTap:(UITapGestureRecognizer *)recognizer { 

    NSLog(@"KidsViewController ==> handleTap."); 

    switch (((UIGestureRecognizer *)recognizer).view.tag)  
    { 
     case 1: 
      //... 
      NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 1); 
      break; 
     case 2: 
      //... 
      NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 2); 
      break; 
     case 3: 
      //... 
      NSLog(@"KidsViewController ==> handleTap. Switch Case: %d", 3); 
      break;    
     default: 
      NSLog(@"KidsViewController ==> handleTap. Switch Case: DEFAULT"); 
      break; 
    } 

} 

- (void)viewDidLoad { 

    pageCount=12; 
    pageNum=1; 

    //put imageviews in place 
    imageNext.frame=CGRectMake(0,0-crop,screenwidth,screenheight+(crop*2)); 
    imageCurrent.frame=CGRectMake(0,0-crop,screenwidth,screenheight+(crop*2)); 

    [self loadPage]; 

    imageCurrent.image = [UIImage imageWithContentsOfFile:[self filePathForLanguage:language pageNumber:pageNum fileType:@"jpg"]]; 

    //TAP GESTURE 
    UITapGestureRecognizer *tapRecognizer; 
    tapRecognizer=[[UITapGestureRecognizer alloc] 
        initWithTarget:self 
        action:@selector(handleTap:)]; 
    tapRecognizer.numberOfTapsRequired=1; 
    tapRecognizer.numberOfTouchesRequired=1; 
    [self.imageAnimation addGestureRecognizer:tapRecognizer]; 
    tapRecognizer.delegate = self; 
    [tapRecognizer release]; 
} 

-(void)loadPage{ 

    imageNext.image = [UIImage imageWithContentsOfFile:[self filePathForLanguage:language pageNumber:pageNum fileType:@"jpg"]]; //[UIImage imageWithContentsOfFile:pathFilename]; 

    switch (pageNum)  
    { 
     case 1: 
      //... 
      NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum); 
      UIImage *image = [UIImage imageNamed:@"P3-stilts_00000.png"]; 
      CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height); 
      imageAnimation = [[UIImageView alloc] initWithFrame:frame]; 
      imageAnimation.userInteractionEnabled = YES; 
      imageAnimation.image = image; 
      imageAnimation.tag = pageNum; 
      [self.view addSubview:imageAnimation]; 
      [image release]; 
      break; 
     case 2: 
      //... 
      NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum); 
      imageAnimation.image = nil; 
      [imageAnimation setCenter:CGPointMake(-100,-100)]; 
      break; 
     case 3: 
      //... 
      NSLog(@"KidsViewController ==> loadPage. Switch Case: %d", pageNum); 
      UIImage *image3 = [UIImage imageNamed:@"bug.png"]; 
      CGRect bugFrame = CGRectMake(0, 0, image3.size.width, image3.size.height); 
      imageAnimation = [[UIImageView alloc] initWithFrame:bugFrame]; 
      imageAnimation.userInteractionEnabled = YES; 
      imageAnimation.image = image3; 
      imageAnimation.tag = pageNum; 
      [self.view addSubview:imageAnimation]; 
      [image3 release]; 
      break;    
     default: 
      NSLog(@"KidsViewController ==> loadPage. Switch Case: DEFAULT"); 
      [imageAnimation setCenter:CGPointMake(-100,-100)]; 
      break; 
    } 
} 

- (void)dealloc { 
    [setupViewController release]; 
    [imageCurrent release]; 
    [imageNext release]; 
    [imageShadow release]; 
    [imageMenuBar release]; 
    [imageAnimation release]; 
    [super dealloc]; 
} 

@end 

回答

2

你總是讓你self.view的標籤。這是標籤默認爲。因此,切換跳轉到默認選項。

你可以添加你的識別器到imageAnimation它會正常工作。

+0

添加手勢識別r到imageview(imageAnimation)直接聽起來是輕鬆的路線。你的交換機將工作,你不必找出它來自哪裏(除了使用tapRecognizer.view)。 – Sam

+0

Thx Nekto&Sam,我嘗試將手勢識別器直接添加到imageview(imageAnimation)中,在這種情況下,它僅適用於一頁,但不適用於其他頁面。那可能是因爲我沒有正確地發佈和添加回來?我想重複使用不同PNG的imageAnimation,並處理許多頁面上的不同動畫。 – Alex

+0

您是否爲每個頁面分配了唯一標籤(對於每個** imageAnimation.tag **)?如果是的話,你能發佈你的代碼的更新版本嗎?或者,請給我們更多的細節。 – Nekto

1

你的問題可能出在這裏:

[self.view addGestureRecognizer:tapRecognizer]; 

如果你是:

switch (((UIGestureRecognizer *)recognizer).view.tag) 

當你註冊後該手勢識別要添加到您的視圖控制器的觀點,並沒有給ImageView的你試圖獲得被點擊的視圖,請考慮使用類似

CGPoint point = [tapRecognizer locationInView:tapRecognizer.view]; 
UIView *viewThatWasTouched = [tapRecognizer.view hitTest:point withEvent:nil];