Search code examples
iphoneobjective-ciosuiviewdrawrect

Speed up -drawRect: method for UIView subclass


I've created a custom UIView subclass. Everything works fine on the simulator, but very slow on the device. My -drawRect: method takes about 100 milliseconds to fully redraw the view.

The speed problem arose when I added a pan gesture to this view and, while dragging, the view needed to be redrawn multiple times per second.

I need to optimize the drawRect: method. Here's my current -drawRect: method code:

- (void)loayoutSubviews
{
    for (UIView* subview in self.subviews)
    {
        [subview removeFromSuperview];
    };

    // ...
    // some calculations here... assume that they can not be optimized
    // ...

    for (unsigned int blockCounter=0; blockCounter<blockQuantity; blockCounter++)
    {
        NSNumber *y=[blockTops objectAtIndex:blockCounter];
        NSNumber *height=[blockHeights objectAtIndex:blockCounter];

        if ([height intValue]>2)
        {
            if ([y doubleValue]>self.frame.size.height/2.0)
            {
                double angle= [[self angleValueForBlockHeight:height] doubleValue];

                UIView *bView=[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[blocks objectAtIndex:blockCounter]]];
                [bView setFrame:CGRectMake(0.0, [y doubleValue], self.frame.size.width, blockSize.height)];
                [bView.layer setMasksToBounds:YES];


                double oldHeight=bView.frame.size.height;

                [bView.layer setAnchorPoint:CGPointMake(0.5, 1.0)];
                [bView setFrame:CGRectMake(bView.frame.origin.x, bView.frame.origin.y+bView.frame.size.height/2.0, bView.frame.size.width, bView.frame.size.height)];

                if ([height doubleValue]!=blockSize.height)
                {
                    //preparing transform
                    CATransform3D basicTrans = CATransform3DIdentity;
                    basicTrans.m34 =1.0/-projection;

                    double rangle;
                    rangle=angle/360*(2.0*M_PI);

                    bView.layer.transform = CATransform3DRotate(basicTrans, rangle, 1.0f, 0.0f, 0.0f);

                };



                double newHeight=bView.frame.size.height;

                [bView setCenter:CGPointMake(bView.center.x, bView.center.y-(oldHeight-newHeight))];

                //adding subview
                [self addSubview:bView];
            }
            else
            {                
                double angle= [[self angleValueForBlockHeight:height] doubleValue];

                UIView *bView=[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:[blocks objectAtIndex:blockCounter]]];
                [bView setFrame:CGRectMake(0.0, [y doubleValue], self.frame.size.width, blockSize.height)];
                [bView.layer setMasksToBounds:YES];

                [bView.layer setAnchorPoint:CGPointMake(0.5, 0.0)];
                [bView setFrame:CGRectMake(bView.frame.origin.x, bView.frame.origin.y-bView.frame.size.height/2.0, bView.frame.size.width, bView.frame.size.height)];

                if ([height doubleValue]!=blockSize.height)
                {
                    //preparing transform
                    CATransform3D basicTrans = CATransform3DIdentity;
                    basicTrans.m34 =1.0/-projection;

                    double rangle;
                    rangle=(360.0-angle)/360*(2.0*M_PI);

                    bView.layer.transform = CATransform3DRotate(basicTrans, rangle, 1.0f, 0.0f, 0.0f);

                };

                //adding subview
                [self addSubview:bView];
            };
        }
        else
        {
            //do not need to draw blocks with very low height
        };
    };
}

and here's the pan gesture recognizer code:

-(void)handlePanGesture:(UIPanGestureRecognizer *)sender
{        
    double translatedY = [sender translationInView:self].y;
    double delta;

    if (fabs(translatedY)<fabs(oldTranslatedY))
    {
        [sender setTranslation:CGPointZero inView:self];
        oldTranslatedY=0.0;
        delta=0.0;
    }
    else
    {
        delta=translatedY-oldTranslatedY;
        oldTranslatedY=translatedY;
    };

    double pOffset=delta/((blockQuantity*blockSize.height)-self.frame.size.height);

    self.scrollPosition=self.scrollPosition-pOffset;

    if (self.scrollPosition<0.0)
    {
        self.scrollPosition=0.0;
    }
    else if(self.scrollPosition>1.0)
    {
        self.scrollPosition=1.0;
    };

    [self setNeedsLayout];
}

Please feel free to ask me any questions.

UPDATED: moved code from drawRect: method to layoutSubviews


Solution

  • You can use Instruments to check which method takes most time, but I think it is unarchiving and creating of UIView. Why do you need it? Why not to hold UIView's in memory, if you need them each time.