---
title: " iPhone手把手教你做CoverFlow"
author: "lovebirdegg"
date: "2010-01-05T22:38:38Z"
tags: ["EXT", "Objective-C", "PHP"]
categories: ["移动开发"]
slug: "iphone-coverflow"
draft: false
lastmod: "2010-01-07T17:01:52Z"
id: 563317
url: "https://lovebirdegg.iteye.com/blog/563317"
---

原文地址：iPhone手把手教你做CoverFlow！！
<http://www.aisidechina.com/forum/viewthread.php?tid=799>
大家好，今天来给大家讲讲iPhone里面的CoverFlow技术。



      CoverFlow 并未加入的苹果的官方SDK，但是它仍然是标准的UIKit，因此我么可以提取出UICoverFlowLayer.h文件。那我们怎么生成CoverFlow效果呢？下面一步教你怎么实现简单的CoverFlow效果：首先建立一个View based Application。
![](http://www.aisidachina.com/forum/attachments/month_1001/1001051722b627afa482dcd686.png)
名字随便起就行 这里我命名为CoverFlow如图：
![](http://www.aisidachina.com/forum/attachments/month_1001/1001051722ffa40dc3f8846dd5.png)
接下来新建一个空文件，然后将 UICoverFlowLayer.h 里面的代码拷贝进去，
命名为 UICoverFlowLayer.h
```
UICoverFlowLayer.h
*/
@interface UICoverFlowLayer : NSObject // CALayer
{
    void *_private;
}

- (id)initWithFrame:(struct CGRect)fp8 numberOfCovers:(unsigned int)fp24 numberOfPlaceholders:(unsigned int)fp28;
- (unsigned int)numberOfCovers;
- (unsigned int)numberOfPlaceholders;
- (void)dealloc;
- (void)setDelegate:(id)fp8;
- (void)setPlaceholderImage:(void *)fp8 atPlaceholderIndex:(unsigned int)fp12;
- (void)setPlaceholderIndicesForCovers:(unsigned int *)fp8;
- (void)_prefetch:(unsigned int)fp8 atIndex:(unsigned int)fp12;
- (void)_requestBatch;
- (void)_requestImageAtIndex:(int)fp8 quality:(unsigned int)fp12;
- (void)_requestImageAtIndex:(int)fp8;
- (void)_notifySelectionDidChange;
- (void)transitionIn:(float)fp8;
- (void)transitionOut:(float)fp8;
- (void)transition:(unsigned int)fp8 withCoverFrame:(struct CGRect)fp12;
- (void)transitionIn:(float)fp8 fromFrame:(struct CGRect)fp12;
- (void)transitionOut:(float)fp8 toFrame:(struct CGRect)fp12;
- (void)setDisplayedOrientation:(int)fp8 animate:(BOOL)fp12;
- (void)setInfoLayer:(id)fp8;
- (void)setImage:(void *)fp8 atIndex:(unsigned int)fp12 type:(unsigned int)fp16;
- (void)setImage:(void *)fp8 atIndex:(unsigned int)fp12 type:(unsigned int)fp16 imageSubRect:(struct CGRect)fp20;
- (void)setImage:(void *)fp8 atIndex:(unsigned int)fp12;
- (unsigned int)indexOfSelectedCover;
- (unsigned int)_coverAtScreenPosition:(struct CGPoint)fp8;
- (void)_recycleLayer:(int)fp8 to:(int)fp12;
- (void)_setNewSelectedIndex:(int)fp8;
- (void)_updateTick;
- (void)displayTick;
- (void)dragFlow:(unsigned int)fp8 atPoint:(struct CGPoint)fp12;
- (void)selectCoverAtIndex:(unsigned int)fp8;
- (void)selectCoverAtOffset:(int)fp8;
- (unsigned int)coverIndexAtPosition:(float)fp8;
- (void)_setupFlippedCoverLayer:(id)fp8;
- (void)flipSelectedCover;
- (int)benchmarkTick;
- (void)benchmarkHeartbeatLongScrub;
- (void)benchmarkHeartbeatShortScrub;
- (void)benchmarkHeartbeatScrubAndWait;
- (void)benchmarkTightLoop;
- (void)benchmarkTightLoopScrub;
- (BOOL)benchmarkLoadScrub;
- (BOOL)benchmarkImageManager:(void *)fp8;
- (void)benchmarkSetEnv;
- (void)benchmarkMode:(int)fp8;
- (void)benchmarkTickMode:(int)fp8;
- (void)benchmarkImageMode:(int)fp8;
- (void)benchmarkPerformanceLog:(BOOL)fp8;
- (void)benchmarkTightLoopTime:(unsigned int)fp8;
- (void)benchmarkLongScrubSpeed:(float)fp8;
- (void)benchmarkSkipImageLoad:(BOOL)fp8;

@end

```
首先介绍一下CFLayer，iPhone开发过程中，我们直接控制的是UIView，而不是
Layer，UIView 相当于Layer的一个子类，因此我们要想显示我们的CoverFlowLayer，就必须
建立一个UIView，来充当我们Layer的容器。

新建一个UIView的子类，并选中also create .h 
这里我命名为CoverFlowView 接下来我们将在CoverFlowView 里面初始化我们的Layer
```
- (CoverFlowView*)initWithFrame:(CGRect)aRect addCount:(NSInteger)count {
	self = [super initwithframe:arect];
	
	self.cfLayer = [[uicoverflowlayer alloc] initWithFrame:CGRectMake(0, 0, 320, 480) 
											numberOfCovers:12
									  numberOfPlaceholders:1];
	[[self layer] addSublayer:(CALayer *)self.cfLayer];
	
	self.phimg = [[uiimageview alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
	[self.cflayer setplaceholderimage: [self.phimg layer] atPlaceholderIndex:0];
	
	unsigned int *pharray = malloc(count * sizeof(int));
	for (int i = 0; i < 12; i++) pharray[i] = 0;;
	[cflayer setplaceholderindicesforcovers:pharray];
	[self setuserinteractionenabled:yes];
	return self;
}   //init


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self.cflayer dragflow:0 atpoint:[[touches anyobject] locationInView:self]];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
	[self.cflayer dragflow:1 atpoint:[[touches anyobject] locationInView:self]];
}
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{	
	[self.cflayer dragflow:2 atpoint:[[touches anyobject] locationInView:self]];
}
- (void) flipSelectedCover         //翻转我们的cover
{
	[self.cflayer flipselectedcover];
}
//touch event

```
然后回到我们的 CoverFlowViewController


在ViewDidLoad方法里面我们加上这段代码
```
[self.view setuserinteractionenabled:yes];
	 self.cfView = [[coverflowview alloc] initWithFrame:CGRectMake(0, 0, 320, 480) addCount:1];
	[self.cfview.cflayer setdelegate:self];  // 设置代理
	[self.cfview.cflayer selectcoveratindex:1];
	self.view = self.cfView;


#pragme datasource method

(void) coverFlow:(id)coverFlow requestImageAtIndex: (int)index quality: (int)quality
{
 UIImage *whichImg = [uiimage imagenamed:[nsstring stringwithformat:@"00%d.jpg",index]];
	[coverflow setimage:[whichimg cgimage]  atIndex:index type:quality];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self.cfview flipselectedcover];
}

```
OK 程序写到这里已经进行的差不多了。
让我们运行下看看效果如何？
![](http://www.aisidachina.com/forum/attachments/month_1001/100105172215c2e2a1bfc28f3d.png)
很炫吧！！赶紧去实现你的CoverFlow吧 祝你学的开心！！:lol