我很少看到我正在用这样的页面卷曲制作动画:
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:NO];
效果很好,视图从右侧卷起,但如果我将设备旋转 180 度,则从左侧发生卷曲。
我的应用程序同时支持横向。不允许肖像。
如何解决?
首先,您是否尝试过使用:
[UIView setAnimationBeginsFromCurrentState:NO];
这是你的问题吗:
当您在横向模式下进行“卷曲”时,使用右侧的按钮,页面开始从右上角翻转到左下角(意思是,您用右手翻页,然后向左侧移动这本书。也意味着书的装订在左边)。
当您执行相同操作但左侧有按钮时,即使正确旋转内容,动画也不会旋转,在vc中说“shouldAutorotateToInterfaceOrientation:True”。结果,动画从左下角到右上角,给人的印象是现在书的装订在右边。
一些代码来说明:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
view1 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[view1 setBackgroundColor:[UIColor greenColor]];
[view1 setFont:[UIFont systemFontOfSize:80]];
[view1 setText:@"VIEW 1"];
view2 = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[view2 setBackgroundColor:[UIColor redColor]];
[view2 setFont:[UIFont systemFontOfSize:80]];
[view2 setText:@"VIEW 2"];
}
return self;
}
-(void)didSwipeNext
{
[self performSelector:@selector(swipePrevious) withObject:nil afterDelay:1];
}
-(void)didSwipePrevious
{
[self performSelector:@selector(swipeNext) withObject:nil afterDelay:1];
}
-(void)swipeNext
{
[UIView beginAnimations:@"curl" context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationDidStopSelector:@selector(didSwipeNext)];
[view1 removeFromSuperview];
[self.view addSubview:view2];
[UIView commitAnimations];
}
-(void)swipePrevious
{
[UIView beginAnimations:@"curl" context:NULL];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationBeginsFromCurrentState:NO];
[UIView setAnimationDidStopSelector:@selector(didSwipePrevious)];
[view2 removeFromSuperview];
[self.view addSubview:view1];
[UIView commitAnimations];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:view1];
[self performSelector:@selector(swipeNext) withObject:nil afterDelay:1];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
一种解决方法是为要处理的页面中的所有 UI 元素创建一个包装器(UIView)。我使包装器成为视图控制器视图的唯一子视图以及包装器的所有其他视图子视图。并将目标修改为包装器,如下所示
[UIView transitionWithView:self.wrapperView
duration:1.0
options:UIViewAnimationOptionTransitionCurlUp
animations:^{[imageView setImage:foregroundImage];}
completion:nil];