update: 2012/03/26
reference: I touchs: Filter4Cam 學習之 Scroll Horizontally Tables: Part 2
修正與效能調整之一
A. 修正濾鏡標題標籤之問題
1. 說明:
a. 當點選某個濾鏡的標題標籤(title label)時, 背景色不見了.(全透明)
b. 為什麼 cell 被點選時, UILabel 會變成透明的? 它是藉由內部的運作來改變
背景色的. 為了防止此種情況, 我們必須去建立 UILabel 的子類別, 並且覆寫
二個方法來讓一切正常.
Xcode > File > New > File...
> iOS > Cocoa Touch > Objective-C class > Next
Class: FilterTitleLabel
Subclass of: UILabel> Next > Create
3. 開啓 FilterTitleLabel.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
@interface FilterTitleLabel : UILabel
{
}
//@add: set a persistent background color
- (void)setPersistentBackgroundColor:(UIColor *)color;
@end
4. 開啓 FilterTitleLabel.m 檔案, 修改如下:
#import "FilterTitleLabel.h"
@implementation FilterTitleLabel
//@add: set the UILabel's background color
- (void)setPersistentBackgroundColor:(UIColor *)color
{
super.backgroundColor = color;
}
//@add: overide
//
// when it tries calling it's own setBackgroundColor method we will not do anything,
// thus preventing the color from changing when selecting our cell.
- (void)setBackgroundColor:(UIColor *)color
{
// do nothing - background color never changes
}
//@add: overide
//
// Let the text inside the label can have a little bit of padding on the left and right side
- (void)drawTextInRect:(CGRect)rect
{
CGFloat newWidth = rect.size.width - kFilterTitleLabelPadding;
CGFloat newHeight = rect.size.height;
CGRect newRect = CGRectMake(kFilterTitleLabelPadding * 0.5, 0, newWidth, newHeight);
[super drawTextInRect:newRect];
}
//@update: comment it
/*
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
*/
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
5. 開啓 FilterCell.h 檔案, 修改如下:
#import <UIKit/UIKit.h>
//@add
#import "ConstantDefined.h"
#import "FilterTitleLabel.h"
//@add
@class FilterTitleLabel;
@interface FilterCell : UITableViewCell
{
//@add
// hold the sample picture for our filter
UIImageView *sampleImage; // 濾鏡效果的圖例
// the title of our filter
//UILabel *titleLabel;
//@update
FilterTitleLabel *titleLabel;
}
//@add
@property (nonatomic, strong) UIImageView *sampleImage;
//@property (nonatomic, strong) UILabel *titleLabel;
//@update
@property (nonatomic, strong) FilterTitleLabel *titleLabel;
@end
6. 開啓 FilterCell.m 檔案, 修改如下:
....
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
....
//self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
//@update: use FilterTitleLabel
self.titleLabel = [[FilterTitleLabel alloc] initWithFrame:CGRectMake(0, self.sampleImage.frame.size.height * 0.632, self.sampleImage.frame.size.width, self.sampleImage.frame.size.height * 0.37)];
....
//self.titleLabel.backgroundColor = kFilterCellTitleLabelBackgroundColor;
//@update: use setPersistentBackgroundColor
[self.titleLabel setPersistentBackgroundColor:kFilterCellTitleLabelBackgroundColor];
....
}
return self;
}
....
7. 編譯並執行:
------------------------------------------------------------------------------------------------------
B. 修正濾鏡清單上下捲動時的問題
1. 說明:
a. 在此專案濾鏡只有一列, 不會遇到此問題, 但仍然先將功能調好, 以後或許會用到.
b. 當濾鏡清單有多列時, 上下捲動可能會看到重覆出現的例圖, 並且分類下的圖也
不正確.
2. 開啓 ViewController.h 檔案, 修改如下:
....
@interface ViewController : GLKViewController <AVCaptureVideoDataOutputSampleBufferDelegate, UITableViewDelegate, UITableViewDataSource>
{
....
//@add for reusable Cells
//
// When we first load our app, create an array of horizontalTableCell cells
// This will store our horizontalTableCell instances for reuse.
NSMutableArray *reusableCells;
....
}
....
//@add for reusable Cells
@property (nonatomic, strong) NSMutableArray *reusableCells;
....
//@add for initialize reusableCells
- (void)initReusableCells;
....
3. 開啓 ViewController.m 檔案, 修改如下:
....
//@add for reusable Cells
@synthesize reusableCells = _reusableCells;
....
- (void)viewDidUnload
{
[super viewDidUnload];
//@add
if ([EAGLContext currentContext] == self.glContext) {
[EAGLContext setCurrentContext:nil];
}
self.glContext = nil;
self.filterDictionary = nil;
self.reusableCells = nil;
}
....
//@add for initialize reusableCells
- (void)initReusableCells
{
if (!self.reusableCells)
{
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:nil ascending:YES selector:@selector(localizedCompare:)];
// sort our categories and store all of the sorted dictionary keys in an array.
NSArray *sortedCategories = [self.filterDictionary.allKeys sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSString *categoryName;
NSArray *currentCategory;
self.reusableCells = [NSMutableArray array];
for (int i = 0; i < [self.filterDictionary.allKeys count]; i++)
{
// create a reusable cell for each
//
// we will overrode cell's initWithFrame method later, and it will pass
// in the proper frame size
HorizontalTableCell *cell = [[HorizontalTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
categoryName = [sortedCategories objectAtIndex:i];
// get the category array for this corresponding cell
currentCategory = [self.filterDictionary objectForKey:categoryName];
// store it within it's filters property
cell.filters = [NSArray arrayWithArray:currentCategory];
// add the cell we just created to our reusable cells array
//
// reusableCells array contains cells already sorted in the same order
// as our categories
//
// That way we just get the object at the necessary index
[self.reusableCells addObject:cell];
}
}
}
....
- (void)viewDidLoad
{
....
//@add for initialize reusableCells
[self initReusableCells];
}
....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
....
*/
//@update: use reusableCells
//
// we already created a cache of reusable cells, all we need to do is
// fetch the appropriate one.
HorizontalTableCell *cell = [self.reusableCells objectAtIndex:indexPath.section];
return cell;
}
....
4. 開啓 HorizontalTableCell.m 檔案, 修改如下:
....
//@update: comment it
// Since we are no longer using the provided method for reusable cells,
// we don't need an identifier for them.
/*
- (NSString *)reuseIdentifier
{
return @"HorizontalCell";
}
*/
....
5. 編譯並執行
此專案在 UI 外觀上並不會有所改變.
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。