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:
消息。誰能幫我?我被這個問題困擾了幾天。
你說得對。我解決了這個問題。非常感謝!!! – cs221313 2011-03-11 05:28:12