2012-09-24 50 views
0

以下是我的代码,它正常工作,它只是在行if(guidelineOn)中的移动线的“动画”不平滑。 drawRect每0.01秒调用一次。iOS - draw_rect in UIView:建议平滑动画

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; 
} 

} 
+0

我很惊讶,你甚至可以接近每秒100 drawRects的速度!在任何情况下,这都不是正确的技术 - 找到一种使用核心动画的方法,因为它将在GPU中完成,因此将显示为与Apple动画一样流畅。 –

+0

啊,我以前使用的是Core Animation解决方案,没有意识到它会有所不同,谢谢你的信息! – lppier

回答

0

这是我对那些感兴趣的Core Animation解决方案。

- (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]; 
}