iPhone SDK开发基础之OpenFlow编程
当用户界面需要按页面显示图片时,使用OpenFlow库提供的功能,将要显示的用户界面图片分页进行显示会使编程工作变得非常快捷。该库提供了与OS X桌面Finder程序相同的视觉效果,如图3-46所示就是一个使用OpenFlow库逐页进行图片显示的程序,用户按下屏幕即可进行左右滚动显示,双击图片即可对当前显示的图片进行选取操作。
▲图3-46 OpenFlow编程实例界面
程序在视图控制器的viewDidAppear()中使用refreshCoverFlow()函数初始化OpenFlow库,通过setNumberOfImages()函数设置图片数量,代码如下。
// RootViewController.m
- (void)refreshCoverFlow{
CGRect bounds = [[UIScreen mainScreen] bounds];
AFOpenFlowView *coverFlowView = (AFOpenFlowView*)[self.view viewWithTag:kTagCoverflow];
if(coverFlowView != nil)
[coverFlowView removeFromSuperview];
coverFlowView = [[AFOpenFlowView alloc] initWithFrame:CGRectMake(0, -30, bounds.size.width, COVERFLOWHEIGHT)];
coverFlowView.dataSource = self;
coverFlowView.viewDelegate = self;
coverFlowView.defaultImage = [self defaultImage];
coverFlowView.tag = kTagCoverflow;
[self.view addSubview:coverFlowView];
NSInteger count = [self numberOfAnimals];
[coverFlowView setNumberOfImages:count];
//...
[coverFlowView release];
}
- (void)refreshCoverFlow{
CGRect bounds = [[UIScreen mainScreen] bounds];
AFOpenFlowView *coverFlowView = (AFOpenFlowView*)[self.view viewWithTag:kTagCoverflow];
if(coverFlowView != nil)
[coverFlowView removeFromSuperview];
coverFlowView = [[AFOpenFlowView alloc] initWithFrame:CGRectMake(0, -30, bounds.size.width, COVERFLOWHEIGHT)];
coverFlowView.dataSource = self;
coverFlowView.viewDelegate = self;
coverFlowView.defaultImage = [self defaultImage];
coverFlowView.tag = kTagCoverflow;
[self.view addSubview:coverFlowView];
NSInteger count = [self numberOfAnimals];
[coverFlowView setNumberOfImages:count];
//...
[coverFlowView release];
}
并在loadView()中初始化图片,将图片从资源中加载并保存在一个NSMutableArray类型的变量imageArray中,代码如下。
- (BOOL)doAddAnimal:(NSString *)name Image:(NSString *)imageName{
UIImage *image = [UIImage imageNamed: imageName];
if(image == nil) return FALSE;
CGSize size = CGSizeMake(179, 208);
[imageArray addObject:[self resizeImage:image scaledToSize:size]];
return TRUE;
}
UIImage *image = [UIImage imageNamed: imageName];
if(image == nil) return FALSE;
CGSize size = CGSizeMake(179, 208);
[imageArray addObject:[self resizeImage:image scaledToSize:size]];
return TRUE;
}
在OpenFlow库的requestImageForIndex delegate方法中直接通过NSMutableArray的索引作为OpenFlow库的图片索引,并通过该索引设置和获取具体图片,代码如下。
// PageViewController.m
- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex: (int)index{
UIImage *image = [imageArray objectAtIndex:index];
[openFlowView setImage:image forIndex:index];
}
- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex: (int)index{
UIImage *image = [imageArray objectAtIndex:index];
[openFlowView setImage:image forIndex:index];
}
笔者在OpenFlow库AFOpenFlowView.m文件的touchesEnded()函数中增加了双击回调接口,以便在用户双击图片时通知库的调用者,代码如下。
//AFOpenFlowView.m
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(((UITouch *)[touches anyObject]).tapCount == 2){
if ([self.viewDelegate respondsToSelector:@selector(openFlowView: coverViewDoubleClick:)])
[self.viewDelegate openFlowView:self coverViewDoubleClick: selectedCoverView.number];
}
[super touchesEnded:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(((UITouch *)[touches anyObject]).tapCount == 2){
if ([self.viewDelegate respondsToSelector:@selector(openFlowView: coverViewDoubleClick:)])
[self.viewDelegate openFlowView:self coverViewDoubleClick: selectedCoverView.number];
}
[super touchesEnded:touches withEvent:event];
}
库的调用者RootViewController类通过接口函数coverViewDoubleClick()即可处理用户双击事件,代码如下。
- (void)openFlowView:(AFOpenFlowView *)openFlowView coverViewDoubleClick:(int)index{
NSLog(@"coverViewDoubleClick called!");
[self showPaintingViewController];
}
NSLog(@"coverViewDoubleClick called!");
[self showPaintingViewController];
}