【问题标题】:How to crop an UITextView如何裁剪 UITextView
【发布时间】:2011-07-25 02:25:19
【问题描述】:

我想要两个 UITextView,一个在后台,一个在前面。有没有可能在前景中裁剪 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];

我想这是非常基本的,但我仍然是一个初学者,如果有任何关于如何完成此操作的建议,我将非常高兴。

【问题讨论】:

    标签: iphone cocoa-touch uiview uitextview


    【解决方案1】:

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

    - (void)viewDidLoad
    {
        [超级视图DidLoad];
    
        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 pariaatur
        UIFont* font = [UIFont fontWithName:@"Helvetica" size:20.0f];
    
        CGRect textFrameLeft = CGRectMake(0, 0, 320, 480);
    
        UITextView* textLeft = [[UITextView alloc] initWithFrame:textFrameLeft];
        textLeft.text = 文本;
        textLeft.font = 字体;
        textLeft.textColor = [UIColor blackColor];
    
        CGRect textFrameRight = textFrameLeft;
        textFrameRight.origin.x -= textFrameRight.size.width/2;
    
        UITextView* textRight = [[UITextView alloc] initWithFrame:textFrameRight];
        textRight.text = 文本;
        textRight.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];
    
        [左视图发布];
        [文本左释放];
        [右视图发布];
        [textRight 释放];
    }

    ===================================

    意识到 OP 想要这个动画;这是上述方法的修订版。这个版本有更多的硬编码值;但它只是一个例子。

    - (void)viewDidLoad
    {
        [超级视图DidLoad];
    
        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 pariaatur
        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 = 文本;
        textLeft.font = 字体;
        textLeft.textColor = [UIColor redColor];
    
        UITextView* textRight = [[UITextView alloc] initWithFrame:textFrameRight];
        textRight.text = 文本;
        textRight.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 提交动画];
    
        [左视图发布];
        [文本左释放];
        [右视图发布];
        [textRight 释放];
    }

    【讨论】:

    • @Wex:哇,我也很感动!感谢这个非常清晰明了的例子。我对框架等感到非常困惑,但是您的代码比 1000 个单词更好地解释了它。完成后,我将尝试对其进行动画处理并发布我的结果。再次感谢!
    • 没问题的家伙:)。我没有意识到你想要它动画。这应该可以通过根据需要操作 leftView.frame 和 rightView.frame 来实现。
    • @Wex:我尝试过让它动画化,但我想我让生活变得太难了。如果我理解这个概念,我必须更改 4 个值才能让这个动画从右到左(即想象红色来自左边,即文本从左边浸透了红色/血液)。我需要在我的动画中更改以下内容: textFrameRight.origin.x = -320 ----> 0; leftFrame.size.width = 320 -----> 0; rightFrame.size.width = 0 -----> 320; rightFrame.origin.x = 320 ------> 0;
    • @Wex:或者有更简单的处理框架的解决方案吗?
    • 用另一个具有动画的代码示例更新了答案。我还应该说:请注意,您必须设置 .frame 属性才能使动画正常工作。
    【解决方案2】:

    我通常用其他视图来掩盖视图。在你的情况下,我会做这样的事情:

    • 两个文本视图都有自己的超级视图,充当掩码
    • 为每个蒙版的框架设置动画以显示/隐藏子视图

    【讨论】:

    • 谢谢,这真的很有帮助!很抱歉问这个问题,但是 textview 是否有 superview 或 viewController(例如,在我的情况下是 visibleController)?在这种情况下如何访问超级视图的掩码?我需要拨打mask superview.visibleController.UITextView1之类的电话吗?换句话说,我如何访问我的 textview1 的父视图的掩码框架?对不起,如果这是基本的,但我真的很挣扎。我查看了 UIView 文档,但不太了解。
    • - (textView) 这是一个 textView - (maskView) 这是一个基本的 uiview (clipToBounds:YES) 并且它有 textView (textView) 作为子视图 然后:在 viewController 我会做这样的事情: [UIView beginAnimation.....] maskView.frame = cgrectmake(.....) [UIView commitAnim....]
    • 我明白了!会试一试,让你知道它是怎么回事。非常感谢您的帮助,感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2013-08-10
    • 2015-09-18
    相关资源
    最近更新 更多