samedi 15 novembre 2014

Drawing a round rectange by CADisplayLink


Vote count:

0




I am drawing a rectangle with rounded corners which is a frame-by-frame animation by CADisplayLink.


You know, running looks like a snake.


But drawing does not work.


The rectangle's length and width is 50, which's corner radius is 5.


I defined some variables to focus the moving line.



@property (nonatomic, assign) CGPoint linePoint;
@property (nonatomic, assign) CGPoint originPoint;

@property (nonatomic, assign) BOOL isEnd;


Of courese, and some marcos:



#define degreesToRadian(angle) (M_PI * (angle) / 180.0)
#define framePerSecond 60.0f
#define velocity 4.0f


Here is how I implement the Delegate method to draw the shape with deltaTime.



- (void)displayWillUpdateWithDeltaTime:(CFTimeInterval)deltaTime {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
CGFloat deltaValue = MIN(1.0f, deltaTime / (1.0f / framePerSecond));
CGFloat tempX = self.linePoint.x;
CGFloat tempY = self.linePoint.y;
self.originPoint = CGPointMake(0, 0);
self.linePoint = self.originPoint;

switch (self.status) {
case LineStatusGoStraightRight:
tempX += velocity * deltaValue;
if (tempX >= 50 && tempY == 0) {
self.status = LineStatusTurn;
}
break;

case LineStatusTurn:
if (tempX > 0 && tempY == 0) {
tempX = tempX + 5;
tempY = tempY + 5;
self.status = LineStatusGoDown;
break;
}
if (tempX >= 55 && tempY > 0) {
tempX = tempX - 5;
tempY = tempY + 5;
self.status = LineStatusGoStraightLeft;
break;
}
if (tempY >= 60 && tempX <= 0) {
tempX = tempX - 5;
tempY = tempY - 5;
self.status = LineStatusGoUp;
break;
}
if (tempY <= 0 && tempX <= -5) {
tempX = tempX + 5;
tempY = tempY - 5;
break;
}

case LineStatusGoDown:
tempY += velocity * deltaValue;
if (tempY >= 55 && tempX >= 0) {
self.status = LineStatusTurn;
}
break;

case LineStatusGoStraightLeft:
tempX -= velocity * deltaValue;
if (tempX <= 0 && tempY >= 55) {
self.status = LineStatusTurn;
}
break;

case LineStatusGoUp:
tempY -= velocity *deltaValue;
if (tempY <= 5 && tempX < 0) {
self.status = LineStatusTurn;
}
break;

default:
break;
}
dispatch_async(dispatch_get_main_queue(), ^{

[self setNeedsDisplay];
});
});
}


and draw method:



- (UIImage *)preDrawRectImage
{
UIImage *rectImage;
UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width, self.bounds.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();

//LineCap
CGContextSetLineCap(context, kCGLineCapRound);

//LineWidth
CGContextSetLineWidth(context, self.hudLineWidth);

//Fill color
CGContextStrokePath(context);

//
CGContextMoveToPoint(context, self.linePoint.x, self.linePoint.y);

rectImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return rectImage;
}


and also drawInRect method:



- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[self.rectImage drawInRect:rect];
}


Is the CGContextMoveToPoint method shouldn't be called like this? Or I miss something important maybe?



asked 48 secs ago







Drawing a round rectange by CADisplayLink

Aucun commentaire:

Enregistrer un commentaire