Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2009, 10:43 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 215
Default Creating a UIButton with a page turn animation

Hi,

I just did this code for a UIButton that "page turns" away when pressed, and I thought I'd share it with you in case anybody needs something similar. You could use this for example to make a book with turning pages. Anyway, have fun and if you have any comments, fire away!

Code:
- (IBAction) openButton:(id)sender {
UIButton *button = (UIButton *)sender; // disable the button so it’s not pressed again during the animation ((UIView *)sender).userInteractionEnabled = NO; // Set the CALayer anchorPoint to the left edge and // translate the button to account for the new // anchorPoint. In case you want to reuse the animation // for this button, we only do the translation and // anchor point setting once. if (button.layer.anchorPoint.x != 0.0f) { button.layer.anchorPoint = CGPointMake(0.0f, 0.5f); button.center = CGPointMake(button.center.x – button.bounds.size.width/2.0f, button.center.y); } // create an animation to hold the page turning CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; transformAnimation.removedOnCompletion = NO; transformAnimation.duration = 2.0f; transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // start the animation from the current state transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; // this is the basic rotation by 90 degree along the y-axis CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f, 0.0f, -1.0f, 0.0f); // these values control the 3D projection outlook endTransform.m34 = 0.001f; endTransform.m14 = -0.0015f; transformAnimation.toValue = [NSValue valueWithCATransform3D:endTransform]; // Create an animation group to hold the rotation CAAnimationGroup *theGroup = [CAAnimationGroup animation]; // Set self as the delegate to receive notification when the animation finishes theGroup.delegate = self; theGroup.duration = 2.0; // CAAnimation-objects support arbitrary Key-Value pairs, we add the UIView tag // to identify the animation later when it finishes [theGroup setValue:[NSNumber numberWithInt:button.tag] forKey:@”buttonTag”]; // Here you could add other animations to the array theGroup.animations = [NSArray arrayWithObjects:transformAnimation, nil]; theGroup.removedOnCompletion = NO; // Add the animation group to the layer [button.layer addAnimation:theGroup forKey:@"flipButtonOpen"];
} - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
// Get the tag from the animation, we use it to find the // animated UIView NSNumber *tag = [theAnimation valueForKey:@"buttonTag"]; // Find the UIView with the tag and do what you want for (UIView *subview in contents.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
if (subview.tag == [tag intValue])
subview.hidden = YES;
}
}
}
fiftysixty is offline   Reply With Quote
Old 11-04-2009, 01:56 AM   #2 (permalink)
Registered Member
 
LiquidChaz's Avatar
 
Join Date: Sep 2009
Location: New York City
Posts: 53
Default

Wouldn't it be a whole lot easier and faster to put the button in it's unique UIView in IB and then use one of the predefined view transition animations?
__________________
Charles Scalesse

Latest Release:
Mad Bomber - A contemporary twist on the Atari 2600 classic, Kaboom!

LiquidChaz is offline   Reply With Quote
Old 11-04-2009, 07:15 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 215
Default

First, there's no need to put the button in a UIView because UIButton is a subclass of UIView, you can use the predefined view transitions straight out of the box. Second, none of the predefined transitions do this kind of "book page turn" transition: page flip from right or left turns the page along the center vertical axis, and page curl is more akin to tearing a page off a calendar. If you want to go beyond the predefined transitions, you have to code things yourself, and that's what I wanted and did.
fiftysixty is offline   Reply With Quote
Old 11-04-2009, 07:21 AM   #4 (permalink)
indie dev
 
rocotilos's Avatar
 
Join Date: Oct 2009
Posts: 2,754
Default

Quote:
Originally Posted by fiftysixty View Post
First, there's no need to put the button in a UIView because UIButton is a subclass of UIView, you can use the predefined view transitions straight out of the box. Second, none of the predefined transitions do this kind of "book page turn" transition: page flip from right or left turns the page along the center vertical axis, and page curl is more akin to tearing a page off a calendar. If you want to go beyond the predefined transitions, you have to code things yourself, and that's what I wanted and did.
Thank you fiftysixty. This is nice to share. Perhaps the forum moderator could move this thread to "Tutorial" section.
rocotilos is offline   Reply With Quote
Old 11-05-2009, 08:59 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 1
Default

Could you post a sample XCode project utilizing this code?
dmsher is offline   Reply With Quote
Old 11-13-2009, 08:59 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 22
Default I would also love a sample project

I would also love a sample project please


Quote:
Originally Posted by fiftysixty View Post
Hi,

I just did this code for a UIButton that "page turns" away when pressed, and I thought I'd share it with you in case anybody needs something similar. You could use this for example to make a book with turning pages. Anyway, have fun and if you have any comments, fire away!

Code:
- (IBAction) openButton:(id)sender {
UIButton *button = (UIButton *)sender; // disable the button so it’s not pressed again during the animation ((UIView *)sender).userInteractionEnabled = NO; // Set the CALayer anchorPoint to the left edge and // translate the button to account for the new // anchorPoint. In case you want to reuse the animation // for this button, we only do the translation and // anchor point setting once. if (button.layer.anchorPoint.x != 0.0f) { button.layer.anchorPoint = CGPointMake(0.0f, 0.5f); button.center = CGPointMake(button.center.x – button.bounds.size.width/2.0f, button.center.y); } // create an animation to hold the page turning CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"]; transformAnimation.removedOnCompletion = NO; transformAnimation.duration = 2.0f; transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // start the animation from the current state transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; // this is the basic rotation by 90 degree along the y-axis CATransform3D endTransform = CATransform3DMakeRotation(3.141f/2.0f, 0.0f, -1.0f, 0.0f); // these values control the 3D projection outlook endTransform.m34 = 0.001f; endTransform.m14 = -0.0015f; transformAnimation.toValue = [NSValue valueWithCATransform3D:endTransform]; // Create an animation group to hold the rotation CAAnimationGroup *theGroup = [CAAnimationGroup animation]; // Set self as the delegate to receive notification when the animation finishes theGroup.delegate = self; theGroup.duration = 2.0; // CAAnimation-objects support arbitrary Key-Value pairs, we add the UIView tag // to identify the animation later when it finishes [theGroup setValue:[NSNumber numberWithInt:button.tag] forKey:@”buttonTag”]; // Here you could add other animations to the array theGroup.animations = [NSArray arrayWithObjects:transformAnimation, nil]; theGroup.removedOnCompletion = NO; // Add the animation group to the layer [button.layer addAnimation:theGroup forKey:@"flipButtonOpen"];
} - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {
// Get the tag from the animation, we use it to find the // animated UIView NSNumber *tag = [theAnimation valueForKey:@"buttonTag"]; // Find the UIView with the tag and do what you want for (UIView *subview in contents.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
if (subview.tag == [tag intValue])
subview.hidden = YES;
}
}
}
appkov is offline   Reply With Quote
Old 11-20-2009, 06:26 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 215
Default

Hi,

I just wanted to let you know that I posted a sample XCode project using this code:

fiftysixtysoftware.com Blog Archive UIView PageOpen sample project

Feel free to download the project and use it in your Apps as you wish.
fiftysixty is offline   Reply With Quote
Old 04-15-2010, 10:53 AM   #8 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 2
Default

Quote:
Originally Posted by fiftysixty View Post
Hi,

I just wanted to let you know that I posted a sample XCode project using this code:

fiftysixtysoftware.com Blog Archive UIView PageOpen sample project

Feel free to download the project and use it in your Apps as you wish.
Mikko,
Hello and thanks for your code! I am trying to rotate the button counterclockwise along the y-axis but it doesn't seem to be working... Any idea why? I understand that I should just need to change the sign of the y-axis vector, but it still has not changed direction. Here is my code:

//set the anchor point to the right side of the button(334 is the width)
if (button.layer.anchorPoint.x != 0.0f) {
button.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
button.center = CGPointMake(334.0f, button.center.y);
}


// create an animation to hold the page turning
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.removedOnCompletion = NO;
transformAnimation.duration = 1.5f;
transformAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseO ut];
// start the animation from the current state
transformAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
// this is the basic rotation by 90 degree along the y-axis
CATransform3D endTransform = CATransform3DMakeRotation(3.14, 0.0f, 1.0f, 0.0f);
ttefabbob is offline   Reply With Quote
Old 05-05-2010, 08:45 AM   #9 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Madurai
Posts: 111
Send a message via MSN to lokidil Send a message via Yahoo to lokidil Send a message via Skype™ to lokidil
Default

Hi I need animation of 180ş of rotation, that is after the animation the view should be of mirror like appearance. I had change the degree to 180ş in your code but the animation stops in the middle say like 120ş of angle. How to rotation it to 180ş
plz help me.

Thanks for helps and suggestions,
Regards,
lokidil is offline   Reply With Quote
Old 05-06-2010, 01:13 PM   #10 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 175
Default

Anyone know the values to "reverse" the flipping? I have been playing around with it for a couple days and just cannot get it to reverse!

Any help is appreciated.
RockStrongo is offline   Reply With Quote
Old 05-07-2010, 07:24 AM   #11 (permalink)
Registered Member
 
Join Date: Apr 2009
Location: Madurai
Posts: 111
Send a message via MSN to lokidil Send a message via Yahoo to lokidil Send a message via Skype™ to lokidil
Default

Yeah that could be another issue with that animation, I too tried a hard to reverse the animation but I can't, Did you find any solution???

Thanks for helps and suggestions,
Regards,
Quote:
Originally Posted by RockStrongo View Post
Anyone know the values to "reverse" the flipping? I have been playing around with it for a couple days and just cannot get it to reverse!

Any help is appreciated.
lokidil is offline   Reply With Quote
Old 05-07-2010, 09:25 AM   #12 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 175
Default

Nope, didnt figure it out. Also, on the device, I am getting a "flicker" when I remove the subview. I have tried numerous things and it just will not go away.
RockStrongo is offline   Reply With Quote
Old 05-20-2010, 04:51 PM   #13 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 175
Default

Anyone have problem with this animation flickering? I cant seem to figure out why it is doing it.
RockStrongo is offline   Reply With Quote
Old 05-27-2010, 10:55 AM   #14 (permalink)
Registered Member
 
TheFabolusBensun's Avatar
 
Join Date: Jan 2010
Posts: 62
Default

yes it flicker and me and my mate have tryed to work it out for days please someone tell us
TheFabolusBensun is offline   Reply With Quote
Old 05-28-2010, 09:29 AM   #15 (permalink)
Registered Member
 
TheFabolusBensun's Avatar
 
Join Date: Jan 2010
Posts: 62
Default PROBLEM SOLVED

NO MORE FLICKER:

theGroup.fillMode = kCAFillModeBoth;

theGroup not transformAnimation
TheFabolusBensun is offline   Reply With Quote
Old 05-28-2010, 09:39 AM   #16 (permalink)
Registered Member
 
Join Date: Aug 2008
Posts: 175
Default

Quote:
Originally Posted by TheFabolusBensun View Post
NO MORE FLICKER:

theGroup.fillMode = kCAFillModeBoth;

theGroup not transformAnimation
very nice....you the man!
RockStrongo is offline   Reply With Quote
Old 05-29-2010, 05:46 PM   #17 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 6
Default

How would you go about running the animation with the splash page as the entire image and making it flip after a specified sleep period? sleep(2);

I've tried a few things but I can't get it to work!
eyesteal is offline   Reply With Quote
Old 05-29-2010, 06:30 PM   #18 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Default

I don't know whether you can act on the splash screen like that. I don't think you can.

One work-around might be to have a view controller that shows nothing else than your splash screen PNG (i.e. looks exactly like your splash screen) load first. Then, after the wanted interval, you can move from that fake view controller to your real first view controller and have that transmission animated. Does that make sense?

Hope this helps.

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 07-09-2010, 11:15 PM   #19 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 54
Default

To reverse the animation, just reverse the from and to parameters.

Last edited by EPage_Ed; 07-09-2010 at 11:17 PM. Reason: Out of context.
EPage_Ed is offline   Reply With Quote
Old 10-16-2010, 11:57 AM   #20 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 33
Default

Anyone have the value for the reverse fliP?
__SUPERMAN__ is offline   Reply With Quote
Old 02-13-2011, 03:16 PM   #21 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 1
Default Forward and reverse flip

Quote:
Originally Posted by __SUPERMAN__ View Post
Anyone have the value for the reverse fliP?
Here you can find a very easy page curl effect.

YouTube - Iphone Ebook Page Curl Effect

i used it very easy to integrate.
pravs is offline   Reply With Quote
Reply

Bookmarks

Tags
animation, button, uibutton

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 236
16 members and 220 guests
ADY, Alsahir, cacao, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, HemiMG, jansan, M@realobjects, MarkC, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0