论坛首页 移动开发技术论坛

iPhone手把手教你做CoverFlow

浏览 18891 次
该帖已经被评为良好帖
作者 正文
   发表时间:2010-01-05   最后修改:2010-01-07
iOS
原文地址: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
   发表时间:2010-01-06  
lovebirdegg 写道
原文地址:iPhone手把手教你做CoverFlow!!
http://www.aisidachina.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

学习了,哥们,谢谢共享
0 请登录后投票
   发表时间:2010-01-07  
这是什么代码,object-c?
0 请登录后投票
   发表时间:2010-01-07  
NumbCoder 写道
这是什么代码,object-c?

对,objective-c
0 请登录后投票
   发表时间:2010-01-07  
效果很棒啊,我还以为这种效果是opengl写出来的,这几天正琢磨
0 请登录后投票
   发表时间:2010-01-08   最后修改:2010-01-08
error: request for member 'cfLayer' in something not a structure or union
error: request for member 'phimg' in something not a structure or union

编译出了这种错误
楼主帮解!




0 请登录后投票
   发表时间:2010-01-08  
#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]; 


楼主,这段代码是怎么实现的??
实现的哪个协议吗???
0 请登录后投票
   发表时间:2010-01-08  
guying1028 写道
#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]; 


楼主,这段代码是怎么实现的??
实现的哪个协议吗???



编译错误需要引入 QuartzCore.framework 并用import  QuartzCore.h

上边这段代码不需要实现什么协议的
0 请登录后投票
   发表时间:2010-01-12  
有个问题噢, 就是用private framework不能保证将来可以运行啊
0 请登录后投票
   发表时间:2010-01-19  
双击 这样的事件 怎么处理?
代码里只有touch Event
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics