Post Pic

How to add CoverFlow Effect on your iPhone App – OpenFlow

Hello all,

The main criteria of this post is to help you add a cool effect called the “cover flow/open flow” effect to any of your iphone apps. This is cool in two ways actually. One it adds animation kind of effect to your app and the other, its very easy to build too.

I got to learn about this effect when I was working on my “pianos” app where in i’ll have bunch of animals to select which would be displayed as a menu using this “cover flow” effect. Once a particular animal is selected your piano view for that animal comes up. My piano app will be out soon and you can check that out. The source for this post is the link displayed below.

“http://fajkowski.com/blog/2009/08/02/openflow-a-coverflow-api-replacement-for-the-iphone/”

Based on his post I have simplified things further. He uses “flicker API” for the images in his “cover flow”  but in my version I would just use my own library of images so that this post would target the beginners. To begin with we should work with Photoshop a bit to generate your images. If your not acquainted with photoshop, never mind not a problem at all. This particular task with the photoshop just wants you to scale the  images you use in your library to size “225 * 225″ applying “Free Transformation”. That’s all what you got to do. Once you got your images then your ready to go.

Creating the project

Firstly Create a new “view based”  project with project name like “CoverFlow”.

Once you create a new project you would arrive at a screen shown below with predefined classes already generated for you.

Screen shot 2010-04-07 at 4.05.07 PM

Add the OpenFlow source code to your project

You could get the “OpenFlow” source code from “http://apparentlogic.com/openflow“. Add the Openflow library to your project.

Adding OpenFlow source code to the project.

Adding OpenFlow source code to the project.

Add QuartzCore and CoreGraphics frameworks to your project.


Adding frameworks

Adding frameworks

Add both the frameworks form the “Existing Frameworks..” .

Implementing the “CoverFlowViewController.h” class.

-Import “AFOpenFlowView.h” into your CoverFlowVieController.h class.

-Then, you will have to implement two protocols AFOpenFlowViewDelegate and AFOpenFlowViewDataSource.

-The only member of .h class would be a queue to hold all the images needed for the cover flow.

Once you are done with all 3 steps, your source code would be as shown below.

//  CoverFlowViewController.h
//  CoverFlow
//
//  Created by Avinash on 4/7/10.
//  Copyright Apple Inc 2010. All rights reserved.
//
 
#import
#import "AFOpenFlowView.h"
 
@interface CoverFlowViewController : UIViewController  {
 
// Queue to hold the cover flow images
 
	NSOperationQueue *loadImagesOperationQueue;
 
}
 
@end

Implementing the “CoverFlowViewController.m” class.

Open the CoverFlowViewController.m class and you will see many methods already generated and commented out.

As we  want this effect to occur as soon as our view is loaded, we start writing our code under the method -(void)viewDidLoad{

}

Just uncomment the method and start writing the code.

Lets see how we implement this class

-Load the images into the queue

- (void)viewDidLoad {
    [super viewDidLoad];
 
	// loading images into the queue
 
	loadImagesOperationQueue = [[NSOperationQueue alloc] init];
 
	NSString *imageName;
	for (int i=0; i < 10; i++) {
		imageName = [[NSString alloc] initWithFormat:@"cover_%d.jpg", i];
		[(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
		[imageName release];
		NSLog(@"%d is the index",i);
 
	}
	[(AFOpenFlowView *)self.view setNumberOfImages:10];
 
}

-We implement two delegate protocols one to set the default image for the cover flow and the other to tell which image has bee selected.

//delegate protocols
 
// delegate protocol to tell which image is selected
- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index{
 
	NSLog(@"%d is selected",index);
 
}
 
// setting the image 1 as the default pic
- (UIImage *)defaultImage{
 
	return [UIImage imageNamed:@"cover_1.jpg"];
}
 
CoverFlow@end

Once both your classes are implemented you need your Image library to display the images in the Cover Flow effect. Just drag and drop your set of images folder under the Resources tab of your project

Adding images library to the project

Adding images library to the project

Once you have implemented both the classes and your images library is ready you will be just left out with final step, modifying the interface builder i.e, the .xib file.

You will find this .xib file under the  Resources tab in the project. Double click this .xib file and it opens up the “interface builder”.

Resources-iphone –> CoverflowViewController.xib

Once you open this file the Interface Builder would present you with  3 to 4 separate windows like Library, Identity Inspector, View, Reveal in document window.

If they do not open up automatically, nothing to worry, you can open it up manually and you find all these windows in the Tools tab of the Interface Builder. Once you open up the CoverFlowViewController.xib window you will find attributes like Files owner, First Responder, Open Flow View.

As you can see the type of this Open Flow View attribute will be set to UIView.

But as we are creating a Open Flow View instead of the regular UIView its identity should be changed to AFOpenFlowView.

To do this select the Open Flow View attribute in the .xib window and in the Identity Inspector window go to the tab “i” (identity) the right corner tab. Change the class UIView to AFOpenFlowView as shown below.

changing the View Identity

changing the View Identity

Once you are done with this final step  the stage is set and you are ready to go. Save and Run this project in the iphone simulator and you must be able to see the Cover Flow effect as shown in the  screen shot below.

Cover Flow Effect

Cover Flow Effect

And there you go, you have just added the cool  Cover FLow Effect to your apps in minutes. But it took me much longer time to complete this post.

Hope you liked this post and feel free to add any comment or any suggestions.

Finally Subscribe US(Object Graph) on FaceBook and Twitter….

Download the complete source code here

Related posts:

  1. [iPhone App] iMustache iMustache adds a mustache on your photos! Turn your friends'...
  2. [iPhone App] iAfro iAfro adds some funkiness on your photos! Turn your friends'...
  3. iPhone Developer Training in NY City ObjectGraph is starting an iPhone developer training program. The first...
  4. Afro Photo – iPhone App Updated! Our funkiest app was just updated with tons of new...
  5. Check out our Retro Phone Application for iPhone [iRetroPhone App] We are glad to announce our Retro Phone Rotary Dial...

Related posts brought to you by Yet Another Related Posts Plugin.

15 Responses

04.09.10

Thank you for this post, it has been really helpful for me, even if my project is little bit different than yours, and I’m stuck at a point and I can’t figure out what is going wrong.

So, if the device is in portrait orientation, i use plain scrollview, to display the images.
After I change the orientation to landscape (and display the images with OpenFlow) and change orientation once again, back to portrait, my scrollview’s frame size is pretty wierd: the width is 160px smaller, that is should be, so I get only half-screen-sized image.

I tried debugging, to see with function is responsible for this change, but I couldn’t find it. Do you have any idea what is wrong with my app?

Any answer would be appriciated.
Thank you so much and excuse my bad english.

[...] an excellent tutorial on how to, [...]

04.09.10

Dear Sir,

when I openFlowView selectionDidChange:(int)index,
it cant display NSLog(@”%d is selected”,index).

thankyou for your help!

04.09.10

Hello,
Thanks a lot for this very useful free coverflow.
Is there a way to change the size of the FlowItems ?
I integrated that coverflow in an Ipad app and i can’t manage to resize photo in it.

Thanks in advance.
Morgan.

04.09.10

G8 tutorial.But in this tutorial, openFlow delegate methods not called by program.How to call this?

04.09.10

you didn’t set the delegate.
add this line.

[(AFOpenFlowView *)self.view setViewDelegate:self];

then, you can.

04.09.10

Hello Patrick, put this [(AFOpenFlowView *)self.view setViewDelegate:self]; after looping for in viewDidLoad method to register delegate.

Sds,

Marcio

04.09.10

Grt tutorial. To make the delegate methods work, open coverFlowViewController.xib and connect dataSource & viewDelegate Outlets of ‘Open Flow View’ to its File’s Owner

04.09.10

Dear Sir,

when I openFlowView selectionDidChange:(int)index,
it cant display NSLog(@”%d is selected”,index).

thankyou for your help

–>

go interface builder
Open Flow View datasource and viewDelegate Files’s Owner control drag

04.09.10

Thank you for this!

BTW, is there anyway to have the flow go vertical rather than horizontal?

Thanks.

04.09.10

I’m trying to add a button to the Open Flow View, below the images where the reflections are shown. The semi transparent reflections are shown over the top of my button. How can I make the button appear on top?

Thanks

04.09.10

Also, how can I adjust the cover flow to display in landscape mode when the iphone is turned on its side?

Thanks

04.09.10

Dear sir,
The tutorial is really nice but while looping through i am getting an exception at line ” [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i]; ” saying unrecognised selector sent .\plzz help me out

[...] How to add CoverFlow Effect on your iPhone App – OpenFlow – ObjectGraph Blog (tags: coverflow) [...]

04.09.10

You initialized the queue but haven’t added objects to it.

Leave Your Response

* Name, Email, Comment are Required