ITeye博客

iPhone手把手教你做CoverFlow

lovebirdegg · 2010-01-05 22:38 · iphone · 移动开发 · PHP · Objective-C · EXT · Markdown

原文地址: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

评论(13)

  1. #13 楼 · marshluca · 2010-03-26 12:08

    joycodes writes:

    写写玩玩还行,苹果是不会让包含私有api的程序上线运行的。。肯定被拒绝的。

    已经有上线的app了。感谢楼主分享,正好需要。

  2. #12 楼 · joycodes · 2010-01-29 11:06

    写写玩玩还行,苹果是不会让包含私有api的程序上线运行的。。肯定被拒绝的。

  3. #11 楼 · lovebirdegg · 2010-01-19 16:13

    ext writes:

    双击 这样的事件 怎么处理? 代码里只有touch Event

    用UITouch *touch = ;获取UITouch对象, UITouch又一个属性tapCount可以取得点击的次数

  4. #10 楼 · ext · 2010-01-19 14:04

    双击 这样的事件 怎么处理? 代码里只有touch Event

  5. #9 楼 · huqing · 2010-01-12 17:40

    C 呀 不懂

  6. #8 楼 · lordhong · 2010-01-12 02:44

    有个问题噢, 就是用private framework不能保证将来可以运行啊

  7. #7 楼 · lovebirdegg · 2010-01-08 12:47

    guying1028 writes:

    #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

    上边这段代码不需要实现什么协议的

  8. #6 楼 · guying1028 · 2010-01-08 12:00

    #pragme datasource method

    (void) coverFlow:(id)coverFlow requestImageAtIndex: (int)index quality: (int)quality
    {
    UIImage *whichImg = ];
    atIndex:index type:quality];
    }

    • (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
      {
      ;
      }

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

  9. #5 楼 · guying1028 · 2010-01-08 10:42

    error: request for member 'cfLayer' in something not a structure or union error: request for member 'phimg' in something not a structure or union

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

  10. #4 楼 · moderating · 2010-01-07 16:59

    效果很棒啊,我还以为这种效果是opengl写出来的,这几天正琢磨

  11. #3 楼 · lovebirdegg · 2010-01-07 16:55

    NumbCoder writes:

    这是什么代码,object-c?

    对,objective-c

  12. #2 楼 · NumbCoder · 2010-01-07 16:44

    这是什么代码,object-c?

  13. #1 楼 · liuzongan · 2010-01-06 10:03

    lovebirdegg writes:

    原文地址:iPhone手把手教你做CoverFlow!! [url]http://www.aisidachina.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

    学习了,哥们,谢谢共享

评论功能维护中,当前仅展示历史评论。