2011-03-10 3 views
1

스크롤보기를 만들고 해당 스크롤보기에 축소판보기를 넣고 싶습니다. 내 프로그램에서 touchesBegan: 메시지를 가져올 수 없습니다. 내 소스 코드는 다음과 같습니다.UIScrollView 메시지 처리기

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSError* error; 
    NSString *bundleRoot = [[NSBundle mainBundle] bundlePath]; 

    dirContents = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundleRoot error: &error] copy]; 

    scrollView = [[UIScrollView alloc] initWithFrame:[[self view] bounds]]; 
    scrollView.pagingEnabled = YES; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.userInteractionEnabled = YES; 
    scrollView.clipsToBounds = YES;  // default is NO, we want to restrict drawing within our scrollview 
    scrollView.scrollEnabled = YES; 
    [scrollView setDelegate:self]; 
    [[self view] addSubview:scrollView]; 

    DLog(@"scroll frame top = %d, left = %d, width = %d, height = %d", scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height); 
    int i, j; 

    UIView* onePageView = [[UIView alloc] init]; 
    int pageNumber = 0; 
    int iconNumber = 0; 

    for (NSString *tString in dirContents) 
    { 
     if ([tString hasSuffix:@"_chess.png"]) 
     { 
      if(iconNumber == 9) 
      { 
       onePageView.tag = pageNumber + 1; 
       [scrollView addSubview: onePageView]; 
       onePageView = [[UIView alloc] init]; 
       pageNumber++; 
       iconNumber = 0; 
      } 
      j = iconNumber % 3; 
      i = iconNumber/3; 

      const float WIDTH = 150.0; 
      const float HEIGHT = 150.0; 
      CGRect imageRect = CGRectMake(j * 200 + 50.0, i * 200 + 50.0, WIDTH, HEIGHT); 
      //remove the charactors of "_chess.png". 
      NSString* sgfName = [tString substringToIndex: tString.length - 10]; 
      sgfName = [sgfName stringByAppendingString: @".sgf"]; 
      ThumbnailView *thumbnailImage = [[ThumbnailView alloc] initWithFilename: sgfName frame: imageRect]; 
      thumbnailImage.delegate = self; 

      [thumbnailImage setImage:[UIImage imageNamed: tString]]; 
      thumbnailImage.opaque = YES; // explicitly opaque for performance 
      //[self.view addSubview:thumbnailImage]; 
      [onePageView addSubview:thumbnailImage]; 
      [thumbnailImage release]; 
      iconNumber++;    
     } 
    } 

    pageControl.numberOfPages = pageNumber + 1; 
    pageControl.currentPage = 0; 
    onePageView.tag = pageNumber + 1; 
    [scrollView addSubview: onePageView]; 
    [self layoutScrollPages]; 
} 

ThumbnailView 클래스는 다음과 같습니다.

// 
// ThumbnailView.m 
// go 
// 
// Created by David Li on 2/18/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "common.h" 
#import "ThumbnailView.h" 
#import "ipad_goViewController.h" 

@implementation ThumbnailView 

@synthesize delegate; 
@synthesize sgfName; 

-(id) initWithFilename: (NSString*)filename frame: (CGRect)frame 
{ 
    sgfName = [[NSString alloc] initWithString: filename]; 

    return [self initWithFrame: frame]; 
} 

- (id)initWithFrame:(CGRect)frame { 

    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code. 
     self.userInteractionEnabled = YES; 
    } 

    return self; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    [self becomeFirstResponder]; 
} 

/* refer to http://stackoverflow.com/questions/855095/how-can-i-detect-the-touch-event-of-an-uiimageview */ 
-(BOOL)canBecomeFirstResponder 
{ 
    return YES; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    DLog(@"touched"); 
    [[self delegate] loadGame: sgfName]; 
} 

/* 
// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
    // Drawing code. 
} 
*/ 

- (void)dealloc { 
    [super dealloc]; 
} 


@end 

내 프로그램에서 touchesBegan: 메시지를 수신 할 수 없습니다. 누구든지 나를 도울 수 있습니까? 나는이 문제로 며칠 갇혔다.

답변

0

나는 onePageView의 프레임을 설정하지 않았다고 생각합니다 ... 그 이유는 당신이 손도 쓰지 않는 이유입니다 ... 프레임을 설정하면 효과가 있습니다.

참고 : onePageView.backgroundColor = [UIColor redColor]를 설정해보십시오. 디버깅 목적으로 쉽게 확인할 수 있습니다.

+0

네 말이 맞아. 나는 그 문제를 해결했다. 정말 고마워!!! – cs221313