【问题标题】:Using CAGradientLayer on UITextView in UITableViewCell在 UITableViewCell 中的 UITextView 上使用 CAGradientLayer
【发布时间】:2014-06-30 21:08:39
【问题描述】:

我在可折叠的TableViewCell 中有一个UITextView。 在UITextView 我想要一个CAGradientLayer 淡出上面的底部文本。

我创建了一个UIView,其中包含UITextView。这一切都在单元格中

我不知道如何在单元格或视图控制器中设置代码。

我的测试代码来自 Github:Fun with Mask by Ewan Davids.

[self createGradientMask];

我应该把(void) 方法放在哪里?在TableViewCellViewController?

    - (void)createGradientMask
        {
            //creating our gradient mask
        CAGradientLayer *maskLayer = [CAGradientLayer layer];

        //this is the anchor point for our gradient, in our case top left. setting it in the middle (.5, .5) will produce a radial gradient. our startPoint and endPoints are based off the anchorPoint
        maskLayer.anchorPo = CGPointZero;

        //The line between these two points is the line our gradient uses as a guide
        //starts in bottom left
        maskLayer.startPoint = CGPointMake(0.0f, 1.0f);

        //ends in top right
        maskLayer.endPoint = CGPointMake(1.f, 0.0f);

        //setting our colors - since this is a mask the color itself is irrelevant - all that matters is the alpha. A clear color will completely hide the layer we're masking, an alpha of 1.0 will completely show the masked view.
        UIColor *outerColor = [UIColor colorWithWhite:1.0 alpha:0.0];
        UIColor *innerColor = [UIColor colorWithWhite:1.0 alpha:1.0];

        //an array of colors that dictatates the gradient(s)
        maskLayer.colors = @[(id)outerColor.CGColor, (id)outerColor.CGColor, (id)innerColor.CGColor, (id)innerColor.CGColor];

        //these are percentage points along the line defined by our startPoint and endPoint and correspond to our colors array. The gradient will shift between the colors between these percentage points.
        maskLayer.locations = @[@0.0, @0.15, @0.5, @1.0f];

        maskLayer.bounds = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 

    CGRectGetHeight(self.view.bounds));

            self.underTextView.layer.mask = maskLayer;
        }
  • TableViewCell 中,我无法识别view 属性。
  • ViewController我不认识UITextView property

已添加 QuartzCore.framework。

【问题讨论】:

    标签: ios objective-c uitableview uiview cagradientlayer


    【解决方案1】:

    将渐变代码放在tableview数据源方法里面:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // code to customize cell here
    }
    

    此时有机会自定义单元格视图,比如颜色、文字、图层和子视图等等...

    【讨论】:

    • 谢谢!干了! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多