如何裁剪UITextView [英] How to crop an UITextView

查看:140
本文介绍了如何裁剪UITextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有两个UITextViews,一个在后台,一个在前面。有没有可能裁剪50%的一个在前台,以便你可以看到一个在后台的50%?我不想在前面重新调整UITextView的大小,但只是隐藏一半。

I would like to have two UITextViews, one in the background and one in the front. Is there any possibility to crop 50% of the one in the foreground so that you can see 50% of the one in the background? I do not want to re-size the UITextView in the front, but merely to hide half of it.

我想有一个插图,因为这可能听起来很混乱:

I think an illustration is in place as this might sound rather confusing:

>

我想我使用两个视图控制器,一个隐藏,一个可见:

I thought I do this with two view controllers, one hidden, one visible:

// 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控制器的隐藏 hiddenViewController在后台显示(就像一个滑动的门 - 从右边滑动)。这是我到目前为止,但我不能想到任何转换/种植技术,将做:

Ideally, I would like to animate the 'hiding' of the visibleView Controller, so that the hiddenViewController unveils in the background (like a sliding door - sliding in from the right). This is what I've come up so far, but I can't think of any transformation / cropping technique that will do:

[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];

我想这是相当基本的,但我还是一个初学者,建议如何完成这个。

I guess this is quite basic, but I'm still a beginner and would be very happy over any suggestions of how to accomplish this.

推荐答案

我刚刚创建了一个新的基于视图的应用程序项目,并将此代码放在viewDidLoad viewController获取屏幕显示。它显示了你需要做什么的理论。要注意的要点是clipsToBounds = true和textFrameRight的负x位置。

I just created a new View-Based Application project and put this code in the viewDidLoad of the viewController to get the screen shown. It shows the theory of what you need to do. The main points to note are the 'clipsToBounds = true' and the negative x-position of textFrameRight.

- (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想要这个动画;这里是上述方法的修订版本。在这个版本中有更多的硬编码值;但它作为一个例子。

Realising the OP wanted this to animate; here is a revised version of the above method which does such. There are more hard-coded values in this version; but it serves as an example.

- (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];
}

这篇关于如何裁剪UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆