iPhone手把手教你做CoverFlow

邵君植
2023-12-01
原文地址:iPhone手把手教你做CoverFlow!!
[url]http://www.aisidechina.com/forum/viewthread.php?tid=799[/url]
大家好,今天来给大家讲讲iPhone里面的CoverFlow技术。


CoverFlow 并未加入的苹果的官方SDK,但是它仍然是标准的UIKit,因此我么可以提取出UICoverFlowLayer.h文件。那我们怎么生成CoverFlow效果呢?下面一步教你怎么实现简单的CoverFlow效果:首先建立一个View based Application。
[img]http://www.aisidachina.com/forum/attachments/month_1001/1001051722b627afa482dcd686.png[/img]
名字随便起就行 这里我命名为CoverFlow如图:
[img]http://www.aisidachina.com/forum/attachments/month_1001/1001051722ffa40dc3f8846dd5.png[/img]
接下来新建一个空文件,然后将 UICoverFlowLayer.h 里面的代码拷贝进去,
命名为 UICoverFlowLayer.h[code]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
[/code]首先介绍一下CFLayer,iPhone开发过程中,我们直接控制的是UIView,而不是
Layer,UIView 相当于Layer的一个子类,因此我们要想显示我们的CoverFlowLayer,就必须
建立一个UIView,来充当我们Layer的容器。

新建一个UIView的子类,并选中also create .h
这里我命名为CoverFlowView 接下来我们将在CoverFlowView 里面初始化我们的Layer[code]- (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
[/code]然后回到我们的 CoverFlowViewController


在ViewDidLoad方法里面我们加上这段代码[code][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];
}
[/code]OK 程序写到这里已经进行的差不多了。
让我们运行下看看效果如何?
[img]http://www.aisidachina.com/forum/attachments/month_1001/100105172215c2e2a1bfc28f3d.png[/img]
很炫吧!!赶紧去实现你的CoverFlow吧 祝你学的开心!!:lol
 类似资料: