iOS-UIView中的drawRect:平滑动画的建议

iOS-UIView中的drawRect:平滑动画的建议,ios,animation,Ios,Animation,下面是我的代码,它工作正常,只是直线中移动直线的“动画”(guidelineOn)不平滑。drawRect每0.01秒调用一次 if (guideLineOn) { [self drawGuidanceLineAtPoint:CGPointMake((65+guideLineOffset)*scalingF, 98*scalingF) withAlpha:paramAlpha]; } 它有点滞后,有时会干扰用户输入。我如何解决这个问题?新的,所以开放的建议 提前感谢你的帮助 码头 - (voi

下面是我的代码,它工作正常,只是直线中移动直线的“动画”(guidelineOn)不平滑。drawRect每0.01秒调用一次

if (guideLineOn)
{
[self drawGuidanceLineAtPoint:CGPointMake((65+guideLineOffset)*scalingF, 98*scalingF) withAlpha:paramAlpha];
}
它有点滞后,有时会干扰用户输入。我如何解决这个问题?新的,所以开放的建议

提前感谢你的帮助

码头

- (void)drawRect:(CGRect)rect
{
scalingF = 1.0; // default : iPhone

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    scalingF = IPAD_SCALING_FACTOR;
}

if (guideLineOn)
{
    [self drawGuidanceLineAtPoint:CGPointMake((65+guideLineOffset)*scalingF, 98*scalingF) withAlpha:paramAlpha];
}

// draw staff line
[self drawStaffLineFrom:CGPointMake(65*scalingF, 98*scalingF) toPoint:CGPointMake(420*scalingF, 98*scalingF)];
[self drawStaffLineFrom:CGPointMake(420*scalingF,108*scalingF) toPoint:CGPointMake(420*scalingF, 50*scalingF)];
[self drawStaffLineFrom:CGPointMake(415*scalingF,108*scalingF) toPoint:CGPointMake(415*scalingF, 50*scalingF)];

// cycle through all the static images to draw tbd
float offSet = 0.0;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    offSet += 50.0; // to adjust the metronome down
}

if (metronomeImage)
{

    [metronomeImage drawInRect:CGRectMake(410.0f*scalingF, 195.0f*scalingF+offSet, metronomeImage.size.width*scalingF, metronomeImage.size.height*scalingF)];
}

if (circlesImage)
{
    [circlesImage drawInRect:CGRectMake(400.0f*scalingF, 255.0f*scalingF+offSet, circlesImage.size.width*scalingF, circlesImage.size.height*scalingF)];
}

for (Note * v in notesArray)
{
    UIImage * noteImage = [UIImage imageNamed: v.imageName];
    [noteImage drawInRect:CGRectMake(v.notePlacement.x*scalingF, v.notePlacement.y*scalingF, noteImage.size.width*scalingF, noteImage.size.height*scalingF)];
}

if (!clearDisplay) 
{
    // draw the arrows
    for (int i=0; i < arrowsToDrawArray.count; i++)
    {
        Arrow * arrowObj = [arrowsToDrawArray objectAtIndex:i];
        UIColor * colourOfArrow = [arrowObj colourOfArrow]; // colour determines whether right or wrong
        CGPoint p = [arrowObj arrowPlacement];
        p.x = p.x*scalingF;
        p.y = p.y*scalingF;

    //    CGPoint p = [val CGPointValue]; 
        [self drawRooftopAtTopPointof:p colour:colourOfArrow lineJoin:kCGLineJoinMiter];
    }

    // draw the ties
    for (int j=0; j < tiePointsArray.count; j++)
    {
        CGPointPair * tiePairToDraw = [tiePointsArray objectAtIndex:j];
        CGPoint firstPt = [tiePairToDraw firstPoint];
        firstPt.x = firstPt.x *scalingF;
        firstPt.y = firstPt.y *scalingF;

        CGPoint secondPt = [tiePairToDraw secondPoint];
        secondPt.x = secondPt.x *scalingF;
        secondPt.y = secondPt.y *scalingF;

        [self drawPseudoCurveFromPoint:firstPt toPoint:secondPt];
    }

    // bool perfect = true;
    // int noOfNotesHit = 0;

    // draw the tick/cross/arrow
    for (int k=0; k < answerObjArray.count; k++)
    {
        Answer * ansObj = [answerObjArray objectAtIndex:k];

        if (ansObj.hit)
        {
           // noOfNotesHit++;

            if (ansObj.earlyOrLate == -1) // early, draw right pointing arrow
            {
                UIImage * arrowImage = [UIImage imageNamed: @"arrowright.png"];
                [arrowImage drawInRect:CGRectMake((ansObj.xPosition-(0.5*arrowImage.size.width))*scalingF, 125*scalingF, arrowImage.size.width*scalingF, arrowImage.size.height*scalingF)];
               // perfect = false;
            }
            else if (ansObj.earlyOrLate == 1) // late, draw left pointing arrow
            {
                UIImage * arrowImage = [UIImage imageNamed: @"arrowleft.png"];
                [arrowImage drawInRect:CGRectMake((ansObj.xPosition-(0.5*arrowImage.size.width))*scalingF, 125*scalingF, arrowImage.size.width*scalingF, arrowImage.size.height*scalingF)];
                //perfect = false;
            }
            else // perfect!
            {
                // draw a tick
                UIImage * tickImage = [UIImage imageNamed: @"tick.png"];
                [tickImage drawInRect:CGRectMake((ansObj.xPosition-(0.5*tickImage.size.width))*scalingF, 125*scalingF, tickImage.size.width*scalingF, tickImage.size.height*scalingF)];
            }

        }
        else
        {
            // draw a cross
            UIImage * crossImage = [UIImage imageNamed: @"cross.png"];
            [crossImage drawInRect:CGRectMake((ansObj.xPosition-(0.5*crossImage.size.width))*scalingF, 125*scalingF, crossImage.size.width*scalingF, crossImage.size.height*scalingF)];
            //perfect = false;
        }

    }

}
else 
{
    // draw nothing
    clearDisplay = false; 
}

}
-(void)drawRect:(CGRect)rect
{
scalingF=1.0;//默认值:iPhone
if(UI\u USER\u INTERFACE\u IDIOM()==UIUserInterfaceIdiomPad)
{
scalingF=IPAD\u缩放因子;
}
国际单项体育联合会(指南)
{
[self-DrawGuidanceLineEatPoint:CGPointMake((65+导轨偏移)*缩放F,98*缩放F)带Alpha:paramAlpha];
}
//划一条界线
[self-drawStaffLineFrom:CGPointMake(65*scalingF,98*scalingF)toPoint:CGPointMake(420*scalingF,98*scalingF)];
[self-drawStaffLineFrom:CGPointMake(420*scalingF,108*scalingF)拓扑点:CGPointMake(420*scalingF,50*scalingF)];
[self-drawStaffLineFrom:CGPointMake(415*scalingF,108*scalingF)拓扑点:CGPointMake(415*scalingF,50*scalingF)];
//循环浏览所有静态图像以绘制tbd
浮动偏移=0.0;
if(UI\u USER\u INTERFACE\u IDIOM()==UIUserInterfaceIdiomPad)
{
偏移量+=50.0;//向下调整节拍器
}
if(节拍器图像)
{
[metronomeImage drawInRect:CGRectMake(410.0f*缩放F,195.0f*缩放F+偏移,metronomeImage.size.width*缩放F,metronomeImage.size.height*缩放F)];
}
if(圆形图像)
{
[circlesImage drawInRect:CGRectMake(400.0f*缩放F,255.0f*缩放F+偏移,circlesImage.size.width*缩放F,circlesImage.size.height*缩放F)];
}
用于(notesArray中的Note*v)
{
UIImage*noteImage=[UIImage imageName:v.imageName];
[noteImage drawInRect:CGRectMake(v.notePlacement.x*scalingF,v.notePlacement.y*scalingF,noteImage.size.width*scalingF,noteImage.size.height*scalingF)];
}
如果(!clearDisplay)
{
//画箭头
对于(int i=0;i
以下是我为感兴趣的人提供的核心动画解决方案

- (void) animateRedLine: (float) alphaParam
{
[self.view addSubview:self.redlineImageView];
/* Start from top left corner */
[self.redlineImageView setFrame:CGRectMake(65.0f*scalingF,
                                          50.0f*scalingF,
                                          self.redlineImageView.image.size.width*scalingF,
                                          self.redlineImageView.image.size.height*scalingF*2)];

[self.redlineImageView setAlpha:alphaParam];

[UIView beginAnimations:@"redLineAnimation"
                context:( void *)self.redlineImageView];

/* 3 seconds animation */
[UIView setAnimationDuration:noOfBeatsInBar*timeInSecondsForOneBeat];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
/* Receive animation delegates */
[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:
 @selector(imageViewDidStop:finished:context:)];

/* End at the bottom right corner */
[self.redlineImageView setFrame:CGRectMake((65.0f+LENGTH_OF_BAR)*scalingF,
                                          50.0f*scalingF,
                                          self.redlineImageView.image.size.width*scalingF,
                                          self.redlineImageView.image.size.height*scalingF*2)];

//[self.redlineImageView setAlpha:0.0f];

[UIView commitAnimations];
}

我很惊讶,你甚至可以达到每秒100次的速度!无论如何,这不是正确的技术使用-找到一种使用核心动画的方法,因为它将在GPU中完成,因此会像苹果的动画一样平滑。啊,我以前使用的是核心动画解决方案,没有意识到它会有所不同,谢谢你的信息!