我想有两个UITextViews,一个在后台,一个在前面。是否有可能裁剪前景中50%的一个,以便您可以在背景中看到50%的一个?我不想在前面重新调整UITextView的大小,只是隐藏了一半。
我认为插图已经到位,因为这可能听起来很混乱:
我以为我使用两个视图控制器执行此操作,一个隐藏,一个可见:
// Visible and Hidden View
VisibleView *visibleController = [[VisibleView alloc] initWithNibName:@"VisibleView" bundle:nil];
self.visibleView = visibleController;
[visibleController release];
HiddenView *hiddenController = [[HiddenView alloc] initWithNibName:@"HiddenView" bundle:nil];
self.hiddenView = hiddenController;
[hiddenController release];
[self.view insertSubview:visibleView.view atIndex:0]; // show visibleView
理想情况下,我想为visibleView Controller的“隐藏”设置动画,以便hiddenViewController在后台显示(如滑动门 - 从右侧滑入)。这是我到目前为止所提出的,但我想不出任何转换/裁剪技术会做:
[UIView beginAnimations:@"Hide VisibleView" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: ??
forView: self.view
cache: YES];
[visibleView.view removeFromSuperview];
[self.view insertSubview:hiddenView.view atIndex:0];
[UIView commitAnimations];
我想这是非常基本的,但我仍然是初学者,对于如何实现这一点的任何建议都会非常高兴。
答案 0 :(得分:4)
我刚刚创建了一个新的基于视图的应用程序项目,并将此代码放在viewController的viewDidLoad中以显示所显示的屏幕。它显示了你需要做什么的理论。需要注意的要点是'clipsToBounds = true'和textFrameRight的负x位置。
- (void)viewDidLoad { [super viewDidLoad]; NSString* text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; UIFont* font = [UIFont fontWithName:@"Helvetica" size:20.0f]; CGRect textFrameLeft = CGRectMake(0, 0, 320, 480); UITextView* textLeft = [[UITextView alloc] initWithFrame:textFrameLeft]; textLeft.text = text; textLeft.font = font; textLeft.textColor = [UIColor blackColor]; CGRect textFrameRight = textFrameLeft; textFrameRight.origin.x -= textFrameRight.size.width/2; UITextView* textRight = [[UITextView alloc] initWithFrame:textFrameRight]; textRight.text = text; textRight.font = font; textRight.textColor = [UIColor redColor]; CGRect leftFrame = self.view.frame; leftFrame.size.width /= 2; UIView* leftView = [[UIView alloc] initWithFrame:leftFrame]; leftView.clipsToBounds = true; CGRect rightFrame = self.view.frame; rightFrame.size.width -= leftFrame.size.width; rightFrame.origin.x += leftFrame.size.width; UIView* rightView = [[UIView alloc] initWithFrame:rightFrame]; rightView.clipsToBounds = true; [self.view addSubview:leftView]; [leftView addSubview:textLeft]; [self.view addSubview:rightView]; [rightView addSubview:textRight]; [leftView release]; [textLeft release]; [rightView release]; [textRight release]; }
==================================
意识到OP希望它能够生成动画;这是上述方法的修订版本。此版本中有更多硬编码值;但它就是一个例子。
- (void)viewDidLoad { [super viewDidLoad]; NSString* text = @"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; UIFont* font = [UIFont fontWithName:@"Helvetica" size:20.0f]; CGRect leftFrame = CGRectMake(0, 0, 0, 480); CGRect textFrameLeft = CGRectMake(0, 0, 0, 480); CGRect rightFrame = CGRectMake(0, 0, 320, 480); CGRect textFrameRight = CGRectMake(0, 0, 320, 480); UITextView* textLeft = [[UITextView alloc] initWithFrame:textFrameLeft]; textLeft.text = text; textLeft.font = font; textLeft.textColor = [UIColor redColor]; UITextView* textRight = [[UITextView alloc] initWithFrame:textFrameRight]; textRight.text = text; textRight.font = font; textRight.textColor = [UIColor blackColor]; UIView* leftView = [[UIView alloc] initWithFrame:leftFrame]; leftView.clipsToBounds = true; UIView* rightView = [[UIView alloc] initWithFrame:rightFrame]; rightView.clipsToBounds = true; [self.view addSubview:leftView]; [leftView addSubview:textLeft]; [self.view addSubview:rightView]; [rightView addSubview:textRight]; [UIView beginAnimations:@"Hide VisibleView" context:nil]; [UIView setAnimationDuration:3.0]; rightView.frame = CGRectMake(320, 0, 0, 480); textRight.frame = CGRectMake(-320, 0, 320, 480); leftView.frame = CGRectMake(0, 0, 320, 480); textLeft.frame = CGRectMake(0, 0, 320, 480); [UIView commitAnimations]; [leftView release]; [textLeft release]; [rightView release]; [textRight release]; }
答案 1 :(得分:0)
我通常用它来掩盖其他视图的视图。在你的情况下,我会做这样的事情: