如何绘制在两个单独的图层iOS,核心 [英] How to draw on two separate layers iOS, coregraphics

查看:94
本文介绍了如何绘制在两个单独的图层iOS,核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助绘制这样的东西:

I need help with drawing something like this:

我被告知,灰色背景栏和紫色条应该在单独的图层上绘制。然后,那些表示书的章节(这个滑块是关于)的点将在这两个的顶部的层上。

I have been told that the gray background bar and the purple bar should be drawn on separate layers. And then the dots there that signifies the chapters of a book (which this slider is about) will be on a layer on top of those two.

我已完成在活动栏上创建渐变的任务,并绘制如下:

I have accomplished the task of creating the gradient on the active bar and drawing it like so:

- (void)drawRect:(CGRect)rect{
    self.opaque=NO;

    CGRect viewRect = self.bounds;
    //NSLog(@"innerRect width is: %f", innerRect.size.width);
    CGFloat perPageWidth =  viewRect.size.width/[self.model.book.totalPages floatValue];
    NSLog(@"perpage width is: %f", perPageWidth);
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIBezierPath *beizerPathForSegment= [UIBezierPath bezierPath];

    NSArray *arrayFromReadingSessionsSet =[self.model.readingSessions allObjects];
    NSArray *arrayFromAssesmentSet = [self.model.studentAssessments allObjects];
    NSLog(@"array is : %@", self.model.readingSessions);

    CGGradientRef gradient = [self gradient];



    for (int i=0;i<[arrayFromReadingSessionsSet count]; i++) {

        ReadingSession *tempRSObj= [arrayFromReadingSessionsSet objectAtIndex:i];
        CGFloat pageDifference = [tempRSObj.endPage floatValue]-[tempRSObj.startPage floatValue];
        NSLog(@"startpage is: %@, end page is: %@, total pages are: %@", tempRSObj.startPage, tempRSObj.endPage, self.model.book.totalPages) ;


        CGRect ProgressIndicator = CGRectMake(perPageWidth*[tempRSObj.startPage floatValue], viewRect.origin.y, perPageWidth*pageDifference, viewRect.size.height);


       [beizerPathForSegment appendPath:[UIBezierPath bezierPathWithRoundedRect:ProgressIndicator cornerRadius:13.0]];
}
[beizerPathForSegment addClip];

    CGContextDrawLinearGradient(context, gradient, CGPointMake(CGRectGetMidX([beizerPathForSegment bounds]), CGRectGetMaxY([beizerPathForSegment bounds])),CGPointMake(CGRectGetMidX([beizerPathForSegment bounds]), 0), (CGGradientDrawingOptions)NULL);
}

如何将其移动到图层上,然后创建另一个图层和另一个图层然后将它们放在一起?

How do I shift it onto a layer and then create another layer and another layer and then put them over one another?

TIA

推荐答案

'm猜你说话的人是指 CALayer 。在iOS中,每个视图都有一个CALayer支持它。不要在视图中实现 -drawRect:,请执行以下操作:

I’m guessing the person you spoke with was referring to CALayer. In iOS, every view has a CALayer backing it. Instead of implementing -drawRect: in your view, do this:


  1. 与QuartzCore的链接

  2. #import< QuartzCore / QuartzCore。 documentation / uikit / reference / uiview_class / uiview / uiview.html#// apple_ref / occ / instp / UIView / layerrel =nofollow> layer 属性。

  1. link with QuartzCore
  2. #import <QuartzCore/QuartzCore.h> anywhere you want to use this.
  3. Use to your view’s layer property.

图层行为与视图很相似,因为您可以有子图层和超图层,图层具有背景颜色,并且他们可以动画。可能对您的目的有用的几个子类是 CAGradientLayer CAShapeLayer 。有关如何使用图层的详情,请参阅核心动画编程指南

Layers behave a lot like views, in that you can have sublayers and superlayers, and layers have properties for things like background color, and they can be animated. A couple of subclasses that will probably be useful for your purposes are CAGradientLayer and CAShapeLayer. For more on how to use layers, refer to the Core Animation Programming Guide.

这篇关于如何绘制在两个单独的图层iOS,核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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