UIScrollView消息处理程序

时间:2011-03-10 01:46:49

标签: iphone objective-c uiscrollview

我想创建一个滚动视图,并在该滚动视图中放置缩略图视图。但我无法在我的程序中收到touchesBegan:消息。我的源代码如下:

- (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:消息。谁能帮我?几天后我一直坚持这个问题。

1 个答案:

答案 0 :(得分:0)

我认为你还没有设置onePageView的框架......那就是为什么你没有接触到... 尝试设置它将起作用的框架。

仅供参考:尝试设置onePageView.backgroundColor = [UIColor redColor];用于调试目的,以便您轻松验证。