【发布时间】:2011-03-23 07:29:27
【问题描述】:
我想知道如何将此代码转换为页面转换。我想用这种翻页效果切换 Xibs。谢谢!
.h:
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface PageOpenAnimationTutorialViewController : UIViewController
{
IBOutlet UIButton *openPageButton;
IBOutlet UIImageView *pageImage;
}
- (IBAction) openPage:(id)sender;
- (void) pageOpenView:(UIView *)viewToOpen duration:(NSTimeInterval)duration;
@end
.m:
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
- (IBAction) openPage:(id)sender {
[self pageOpenView:pageImage duration:1.0f];
}
- (void) pageOpenView:(UIView *)viewToOpen duration:(NSTimeInterval)duration
{
// Remove existing animations before stating new animation
[viewToOpen.layer removeAllAnimations];
// Make sure view is visible
viewToOpen.hidden = NO;
// disable the view so it’s not doing anythign while animating
viewToOpen.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 (viewToOpen.layer.anchorPoint.x != 0.0f) {
viewToOpen.layer.anchorPoint = CGPointMake(0.0f, 0.5f);
viewToOpen.center = CGPointMake(viewToOpen.center.x - viewToOpen.bounds.size.width/2.0f, viewToOpen.center.y);
}
// create an animation to hold the page turning
CABasicAnimation *transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
transformAnimation.removedOnCompletion = NO;
transformAnimation.duration = duration;
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 = duration;
// CAAnimation-objects support arbitrary Key-Value pairs, we add the UIView tag
// to identify the animation later when it finishes
[theGroup setValue:[NSNumber numberWithInt:viewToOpen.tag] forKey:@"viewToOpenTag"];
// 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
[viewToOpen.layer addAnimation:theGroup forKey:@"flipViewOpen"];
}
- (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:@"viewToOpenTag"];
// Find the UIView with the tag and do what you want
// This only searches the first level subviews
for (UIView *subview in self.view.subviews) {
if (subview.tag == [tag intValue]) {
// Code for what's needed to happen after
// the animation finishes goes here.
if (flag) {
// Now we just hide the animated view since
// animation.removedOnCompletion is not working
// in animation groups. Hiding the view prevents it
// from returning to the original state and showing.
subview.hidden = YES;
}
}
}
}
@end
【问题讨论】:
-
到目前为止你尝试过什么?你明白什么,你不明白什么?还是您的第一个想法是“让我们让 SO 上的某个人为我做这件事”?
-
我是 iPhone SDK 的新手。所以我厌倦了看一些教程,我什至试着把代码拆开,看看每个元素的作用。但这太令人困惑了。到目前为止,我的 Xib 已经随着这个过渡而改变: .modalTransitionStyle = UIModalTransitionStyleCrossDissolve;我想知道是否有办法让我的 Xib 像一本书一样翻转。不是卷页。只是一个简单的翻转。谢谢!
-
@zachdrago:好的,那么答案是这个问题太大了。这几乎就像您要求其他人为您编写程序一样。尝试将问题分解为更小的步骤,并尝试为每个小问题找到答案。几乎所有这些都可能已经在 SO 上得到了回答。
-
@zachdrago:令人困惑的是你问“如何将这段代码变成页面转换”,但似乎代码已经创建了页面转换。所以更多的是你不知道如何使用代码?或者如何使它与 Interface Builder 一起工作?再次尝试提出许多小问题,而不是一个大问题。
标签: iphone objective-c transition xib flip