自定义UIView Drawrect方法在方向改变时花费时间

时间:2017-02-20 12:28:22

标签: ios uiview swift3

我遇到了核心文字框架的问题。我的目标是在 UIView 中显示多列。在纵向模式中,我想显示3列和横向模式,我想要显示4列。所以一切都很好。 当它改变方向需要时间来重绘自定义视图。 在我的自定义视图中,我使用核心文本类做了一些事情,之后我的方向受到了打击。

在我的视图控制器

override func viewWillLayoutSubviews() {
        self.rotated()
    }

func rotated() {

        let orientation: UIInterfaceOrientation = UIApplication.shared.statusBarOrientation

        if orientation.isLandscape {
            if  self.multiPageTextView.text != nil {
            self.view.layoutIfNeeded()
            self.multiPageTextView.columnCount = 4

            }
        }else{

             if self.multiPageTextView.text != nil {
                 self.multiPageTextView.columnCount = 3
                  self.multiPageTextView .setNeedsDisplay()
                self.multiPageTextView.layoutIfNeeded()
            }

        }

    }

我的 customview drawrect 方法

- (void)drawRect:(CGRect)rect 
{

    self.headlineLabel = [[UILabel alloc]init];
    self.headlineLabel.frame = CGRectMake(10, 0, self.frame.size.width - 20, 25);

    self.abstractLabel = [[UILabel alloc]init];
    self.abstractLabel.frame = CGRectMake(10, 0, self.frame.size.width - 20, 25);


    [self.pages makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.pages removeAllObjects];


    CGRect pageControlFrame = CGRectMake(0.0, self.frame.size.height - 20.0, self.frame.size.width, 20.0);
    self.pageControl.frame = pageControlFrame;
    self.headlineLabel.numberOfLines = 0;
    self.headlineLabel.lineBreakMode = NSLineBreakByWordWrapping;
    UIFont *titleFont = [UIFont fontWithName:@"TAUN_Elango_Abirami" size:self.titleFontSize];
    self.headlineLabel.font = titleFont;
//    self.headlineLabel.backgroundColor = [UIColor greenColor];
    self.headlineLabel.text =  _titleText;
    CGRect headLineLbleFrame =   CGRectMake(10, 0, self.frame.size.width - 20.0, [self getLabelHeight:self.headlineLabel]);
    self.headlineLabel.frame = headLineLbleFrame;

    self.abstractLabel.numberOfLines = 0;
    self.abstractLabel.lineBreakMode = NSLineBreakByWordWrapping;
    UIFont *abstractFont = [UIFont fontWithName:@"TAUN_Elango_Abirami" size:self.abstractFontSize];
    self.abstractLabel.font =abstractFont;
    self.abstractLabel.text =  _abstractText;
    CGRect abstractLbleFrame =   CGRectMake(10, headLineLbleFrame.size.height + 5, self.frame.size.width - 20, [self getLabelHeight:self.abstractLabel]);
    self.abstractLabel.frame = abstractLbleFrame;



    NSInteger currentPosition = 0;
    NSInteger iteration = 0;
    CGFloat contentSizeX = 0;
    CGFloat contentSizeY = 0;
    BOOL moreTextAvailable = YES;

    do 
    {
        CGRect currentFrame = CGRectOffset(self.scrollView.frame, self.scrollView.frame.size.width * iteration, 0.0);
        AKOMultiColumnTextView *view = [[AKOMultiColumnTextView alloc] initWithFrame:currentFrame];
        [view setPage:iteration];
        view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

        if (iteration == 0) {
            [view addSubview:self.headlineLabel];
            [view addSubview:self.abstractLabel];
        }
        view.startingPoint = self.headlineLabel.frame.size.height + self.abstractLabel.frame.size.height + 10;
        view.columnCount = self.columnCount;
        view.startIndex = currentPosition;
        view.fontSize = self.fontSize;

        view.text = self.text;
       view.font = self.font;
        view.color = self.color;
        view.backgroundColor = self.backgroundColor;

        // set the properties
        view.lineBreakMode = _lineBreakMode;
        view.textAlignment = _textAlignment;
        view.firstLineHeadIndent = _firstLineHeadIndent;
        view.spacing = _spacing;
        view.topSpacing = _topSpacing;
        view.lineSpacing = _lineSpacing;
        view.columnInset = _columnInset;

        view.dataSource = self.dataSource;
        [self.pages addObject:view];
        [self.scrollView addSubview:view];

        currentPosition = view.finalIndex;
        iteration += 1;
        view.scrollView = self.scrollView;
        contentSizeX = currentFrame.size.width * iteration;
        contentSizeY = currentFrame.size.height;
        moreTextAvailable = view.moreTextAvailable;
       // self.pageControl.currentPage = 0;

    } 
    while (moreTextAvailable);

    self.pageControl.numberOfPages = iteration;
    if(iteration == self.pages.count){
        if ([self.dataSource respondsToSelector:@selector(stopLoading:)]){
            [self.dataSource stopLoading:111];
        }

        if ([self.dataSource respondsToSelector:@selector(getTotalPage:)]){
            [self.dataSource getTotalPage:iteration];
        }

    }

    if (self.columnText == nil || self.columnText.length == 0 || [self.columnText isEqualToString:@""]){
        self.columnText = @"0";
    }
    NSInteger tempInt = [self.columnText integerValue];
    self.pageControl.currentPage = tempInt;
    if (isSwipedEnd) {
        [self.scrollView setContentOffset:CGPointMake(0, 0)];
        self.scrollView.contentSize = CGSizeMake(contentSizeX + 5, contentSizeY);
    }else{

    lastXvalue = self.frame.size.width * tempInt;
    CGPoint point = CGPointMake(lastXvalue, 0);
    lastXvalue = 0;
    self.scrollView.contentSize = CGSizeMake(contentSizeX + 5, contentSizeY);
    self.scrollView.contentOffset = point;
    }
}

在这里我如何实现任务跟随任务 1.我怎么知道方向已经完成 2.重绘自定义视图的正确方法是什么?

绘制列的源代码 https://github.com/akosma/CoreTextWrapper

我正在使用桥接标头来访问objective-c类文件 在此先感谢!!

0 个答案:

没有答案