UIScrollView message handler
- by cs221313
Hi, all,
I want to create a scroll view and put thumbnail view in that scroll view. but I can not get the touchBegan message in my program. My source code is like following.
- (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 class is like following.
//
// 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
I can not catch the touchesBegan message in my program. Can anyone help me? I stucked by this problem by couple days.
Thanks so much.