openFlow实现CoverFlow效果

来源:互联网 发布:诸葛亮 知乎 编辑:程序博客网 时间:2024/05/16 14:46

本文多为转载,我只是把各个大神的资料搜集一下,请各位关注原作者!如果原作者不希望转载,请发邮件留言至我的博客,我会在24小时内删除相关内容!

实现CoverFlow效果有两种方法:

1、CoverFlow。为私有方法,程序中不允许使用。这是苹果官方的限制。这里不再讲解;

2、OpenFlow实现CoverFlow效果,下面仔细讲一下这个。


1、我的代码,已经过本人修改过:这里我在建立View的时候直接将其父类建立成AFOpenFlowView

@interface VideoOpenFlowView : AFOpenFlowView

<AFOpenFlowViewDelegate, AFOpenFlowViewDataSource>

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization codecoverImageData = [NSArray arrayWithObjects:[UIImage imageNamed:@"index0.jpg"],

        

        coverImageData = [NSArray arrayWithObjects:[UIImage imageNamed:@"index0.jpg"],

                          

                          [UIImage imageNamed:@"index1.jpg"],

                          

                          [UIImage imageNamed:@"index2.jpg"],

                          

                          [UIImage imageNamed:@"index3.jpg"],

                          

                          [UIImage imageNamed:@"index4.jpg"],

                          

                          [UIImage imageNamed:@"index5.jpg"],

                          

                          

                          nil];

        

        // Set the Images in OpenFlow View and set the count of images.

        

        for (int i=0; i < [coverImageData count]; i++) {

            

            [self setImage:[coverImageData objectAtIndex:i] forIndex:i];

            

        }

        

        [self setNumberOfImages:[coverImageData count]];

 

        self.viewDelegate=self  

        self.dataSource=self

    }

    return self;

}


- (void) imageDidLoad:(NSArray *)arguments{

    

    UIImage *loadedImage = (UIImage *) [arguments objectAtIndex:0];

    

    NSNumber *imageIndex = (NSNumber *) [arguments objectAtIndex:1];

    

    [self setImage:loadedImage forIndex:[imageIndex intValue]];

}


- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index{  

    

    NSLog(@"%d is selected",index);  

    

}  



// setting the image 1 as the default pic  

- (UIImage *)defaultImage{  

    return [UIImage imageNamed:@"index2.jpg"];  

}  



- (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex:(int)index

{

    NSLog(@"index = %d",index);

}


- (void)openFlowView:(AFOpenFlowView *)openFlowView singleTaped:(int)index

{

    这里面放点击图片的动作;

}

2、本文多为转载,我只是把各个大神的资料搜集一下,请各位关注原作者!如果原作者不希望转载,请发邮件留言至我的博客,我会在24小时内删除相关内容!

转发自:http://blog.csdn.net/archavon/article/details/6529313

压缩包里面有示例项目,可以拿来运行了解一下。

编写自己的代码,需要一些准备工作:

项目中要增加Quartz 2d和CFnetwork框架支持:

 

 

然后,要把open flow的源代码加入到自己项目中:

 

再然后,一般要把自己的Controller类增加open flow的两个protocol

 

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h>   
  2. #import "AFOpenFlowView.h"  
  3. @interface MyFlowViewController UIViewController <AFOpenFlowViewDataSourceAFOpenFlowViewDelegate>{   
  4.     NSOperationQueue *loadImagesOperationQueue  
  5.  
 

 

 

 

可以看到代码中还增加了相应的头文件、还有一个成员用于队列处理。

写了个特别简单的实现,比它官方示例还简单,取消了循环:

 

[cpp] view plaincopy
  1.  (void)loadView   
  2.     [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];   
  3.     self.view=[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];   
  4.     self.view.backgroundColor [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.jpg"]];   
  5.       
  6.     loadImagesOperationQueue [[NSOperationQueue alloc] init];   
  7.     AFOpenFlowView *flowView=[[AFOpenFlowView alloc] initWithFrame:CGRectMake(10, 200, 748, 1080)];   
  8.     [flowView setImage:[UIImage imageNamed:@"6.jpg"forIndex:0];   
  9.     [flowView setImage:[UIImage imageNamed:@"7.jpg"forIndex:1];   
  10.     [flowView setImage:[UIImage imageNamed:@"9.jpg"forIndex:2];   
  11.     [flowView setImage:[UIImage imageNamed:@"11.jpg"forIndex:3];   
  12.     [flowView setImage:[UIImage imageNamed:@"12.jpg"forIndex:4];   
  13.     [flowView setImage:[UIImage imageNamed:@"14.jpg"forIndex:5];   
  14.     flowView.viewDelegate=self;   
  15.     flowView.dataSource=self;   
  16.       
  17.       
  18.     [flowView setNumberOfImages:6];   
  19.     [self.view addSubview:flowView];   
  20.        
  21.  
 

 

 

另外,别忘记实现protocol中的方法,在controller实现类中:

 

[cpp] view plaincopy
  1. (void)openFlowView:(AFOpenFlowView *)openFlowView   
  2.   selectionDidChange:(int)index{   
  3.     NSLog(@"%d is selected",index);   
  4.       
  5.  
  6. // setting the image as the default pic   
  7. (UIImage *)defaultImage{      
  8.     return [UIImage imageNamed:@"9.jpg"];   
  9.  
  10. (void)openFlowView:(AFOpenFlowView *)openFlowView requestImageForIndex:(int)index{   
  11.     NSLog(@">>request index %i",index);   
  12.  
 

 

 

这里会发现,每次点击(选择)图片的时候,都会调用

 

[cpp] view plaincopy
  1. – (void)openFlowView:(AFOpenFlowView *)openFlowView   
  2.   selectionDidChange:(int)index  
 

3、转载自:http://www.cnblogs.com/lovecode/articles/2314110.html


方法。这个方法可以用于切换视图。

另外,本文中没有实现实质性的datasource部分。最简单的示例嘛。


利用OpenFlow开源库开发CoverFlow图片展示效果比较方便,谁让苹果不让我们用其私有API!OpenFlow开源库导入到具体工程项目中的基本方法是:

1.首先这个还是基于view-base项目模板;
2.添加OpenFlow类库文件到类里,以及添加QuartzCore.framework和CoreGraphics.framework;
3.在视图controller类里添加AFOpenFlowView.h引用,还要遵循两个协议<AFOpenFlowViewDataSource,AFOpenFlowViewDelegate>。
4.更改XIB文件里的UIView父类为AFOpenFlowView;
5.连接DataSoure,delegate并写好相应的方法;
6.添加图片,显示示图。
相关注意要点:
1. OpenFlow是要放在一个UIView上的。
2. 若要占部分屏幕:要在上面再放一个View来显示那个CoverFlow,这样位置和大小就能定。
3. 可[self.view addSubview:mAFOpenFlowView];添加CoverFlow视图,而控件还可再添加到它上面。
4. 某些情形下,必须显式设置delegate和datasource属性。

一些修改扩展:
修改AFOpenFlowView类实现点击图片触发事件

// AFOpenFlowView.h
@protocol AFOpenFlowViewDelegate <NSObject>
@optional
- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index;
@end

// 修改为以下代码
@protocol AFOpenFlowViewDelegate <NSObject>
@optional
- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index;
- (void)openFlowView:(AFOpenFlowView *)openFlowView singleTaped:(int)index;
@end
复制代码
// AFOpenFlowView.m内的touchesEnded方法内以下代码
if (targetCover && (targetCover.number != selectedCoverView.number)) [self setSelectedCover:targetCover.number];

//修改为
if (targetCover) { if(targetCover.number != selectedCoverView.number) { [self setSelectedCover:targetCover.number]; } else { if([self.viewDelegate respondsToSelector:@selector(openFlowView:singleTaped:)]) { [self.viewDelegate openFlowView:self singleTaped:selectedCoverView.number]; return; } } }
复制代码