iPhone手把手教你做CoverFlow
原文地址:iPhone手把手教你做CoverFlow!! http://www.aisidechina.com/forum/viewthread.php?tid=799 大家好,今天来给大家讲讲iPhone里面的CoverFlow技术。
CoverFlow 并未加入的苹果的官方SDK,但是它仍然是标准的UIKit,因此我么可以提取出UICoverFlowLayer.h文件。那我们怎么生成CoverFlow效果呢?下面一步教你怎么实现简单的CoverFlow效果:首先建立一个View based Application。
名字随便起就行 这里我命名为CoverFlow如图:
接下来新建一个空文件,然后将 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 程序写到这里已经进行的差不多了。
让我们运行下看看效果如何?
很炫吧!!赶紧去实现你的CoverFlow吧 祝你学的开心!!:lol
已经有上线的app了。感谢楼主分享,正好需要。
写写玩玩还行,苹果是不会让包含私有api的程序上线运行的。。肯定被拒绝的。
用UITouch *touch = ;获取UITouch对象, UITouch又一个属性tapCount可以取得点击的次数
双击 这样的事件 怎么处理? 代码里只有touch Event
C 呀 不懂
有个问题噢, 就是用private framework不能保证将来可以运行啊
编译错误需要引入 QuartzCore.framework 并用import QuartzCore.h
上边这段代码不需要实现什么协议的
#pragme datasource method
(void) coverFlow:(id)coverFlow requestImageAtIndex: (int)index quality: (int)quality
{
UIImage *whichImg = ];
atIndex:index type:quality];
}
{
;
}
楼主,这段代码是怎么实现的?? 实现的哪个协议吗???
error: request for member 'cfLayer' in something not a structure or union error: request for member 'phimg' in something not a structure or union
编译出了这种错误 楼主帮解!
效果很棒啊,我还以为这种效果是opengl写出来的,这几天正琢磨
对,objective-c
这是什么代码,object-c?
学习了,哥们,谢谢共享