私はリーダーアプリに取り組んでいます。雑誌を1冊読むと、最初の5ページが表示され、残りのページが1つずつダウンロードされます。ページのサムネイル画像を表示するためのスクロールビューがあります。最初に、ページをダウンロードする必要がある場合、対応するサムネイルビューのアルファ値は0.5に設定されます(サムネイル画像はファイルにあり、ダウンロードする必要はありません)。ページがダウンロードされると、サムネイルビューの値が1.0に更新されます。1つの操作を使用してページをダウンロードし、1つをダウンロードしたら、デリゲートを使用してサムネイルビューのアルファを設定します。しかし、サムネイルビューのアルファ値を更新しても、最初と同じです。アルファは効果がないようです。私のコードに何か問題があるのだろうか?いくつかのスニペットは次のとおりです。
PageViewController.m内
- (void)loadView
{
[super loadView];
//...
[self createSlideUpViewIfNecessary];
[self downloadPages];
}
- (void)createSlideUpViewIfNecessary {
if (!slideUpView) {
[self createThumbScrollViewIfNecessary];
// create container view that will hold scroll view and label
CGRect frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMaxY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(thumbScrollView.frame));
slideUpView = [[UIView alloc] initWithFrame:frame];
[slideUpView setBackgroundColor:[UIColor blackColor]];
[slideUpView setOpaque:NO];
[slideUpView setAlpha:0.75];
[[self view] addSubview:slideUpView];
// add subviews to container view
[slideUpView addSubview:thumbScrollView];
}
}
- (void)createThumbScrollViewIfNecessary {
if (!thumbScrollView) {
float scrollViewHeight = THUMB_HEIGHT + THUMB_V_PADDING;
float scrollViewWidth = CGRectGetWidth(self.view.bounds);
thumbScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
[thumbScrollView setCanCancelContentTouches:NO];
[thumbScrollView setClipsToBounds:NO];
// now place all the thumb views as subviews of the scroll view
// and in the course of doing so calculate the content width
float xPosition = THUMB_H_PADDING;
for (int i = 0; i < magazine.pageNum ; i++) {
Page *page = [magazine.pages objectAtIndex:i];
NSString *name = page.pageName;
NSString *mfjName =[name stringByReplacingOccurrencesOfString:@".mfj" withString:@"Small.mfj"];
UIImage *thumbImage = nil;
if([mfjName hasSuffix:@".mfj"])
thumbImage = [Reader loadMfjFromSprOrCache:magazine MFJ:mfjName];
ThumbImageView *thumbView;
if (thumbImage) {// sometimes mfjname is 0 which means white page in normal and black thumbnail in thumbnail scrollview.
if (!mThumbnailSizeUpdated) {
mThumbnailWidth = thumbImage.size.width;
mThumbnailHeight = thumbImage.size.height;
mThumbnailSizeUpdated = YES;
}
thumbView = [[ThumbImageView alloc] initWithImage:thumbImage];
} else {
CGRect thumbFrame;
if (mThumbnailSizeUpdated) {
thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
} else {
mThumbnailWidth = 80;
mThumbnailHeight = 100;
thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
}
thumbView = [[ThumbImageView alloc] initWithFrame:thumbFrame];
}
NSString *mfjPath= [[magazine getDownloadPath] stringByAppendingPathComponent:name];
if (![magazine getFileInfo:name]&&![[NSFileManager defaultManager] fileExistsAtPath:mfjPath]) {
thumbView.alpha = 0.5;
}
[thumbView setBackgroundColor:[UIColor blackColor]];
[thumbView setTag:THUMBVIEW_OFFSET+i];
[thumbView setDelegate:self];
[thumbView setImageName:name];
CGRect frame = [thumbView frame];
frame.origin.y = THUMB_V_PADDING;
frame.origin.x = xPosition;
frame.size.width = frame.size.width+30;
frame.size.height = frame.size.height+40;
[thumbView setFrame:frame];
[thumbScrollView addSubview:thumbView];
UILabel *pageIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, frame.origin.y+frame.size.height-THUMB_LABEL_HEIGHT, frame.size.width, THUMB_LABEL_HEIGHT)];
[pageIndexLabel setBackgroundColor:[UIColor clearColor]];
[pageIndexLabel setText:[NSString stringWithFormat:@"%d",(i+1)]];
[pageIndexLabel setTextColor:[UIColor whiteColor]];
[pageIndexLabel setTextAlignment:UITextAlignmentCenter];
[thumbScrollView addSubview:pageIndexLabel];
xPosition += (frame.size.width + THUMB_H_PADDING);
}
thumbScrollView.showsHorizontalScrollIndicator = NO;
[thumbScrollView setContentSize:CGSizeMake(xPosition, scrollViewHeight)];
}
}
- (void)downloadPages
{
DownloadOperation *op = [[DownloadOperation alloc] initWithMagazine:magazine];
op.delegate = self;
[[(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:op];
}
- (void)downloadOperation:(DownloadOperation *)operation finishedAtIndex:(NSUInteger)index
{
if (thumbScrollView){
[thumbScrollView viewWithTag:THUMBVIEW_OFFSET+index].alpha = 1.0;
}
}
DownloadOperation.mで
- (void)main
{
// ...
NSUInteger index = 0;
for (Page *page in mMagazine.pages)
{
if (/*page doesn't exist*/){
// download the page;
if ([delegate respondsToSelector:@selector(downloadOperation:finishedAtIndex:)]) {
[delegate downloadOperation:self finishedAtIndex:index];
}
}
index++;
}
}