CALayer scale animation without blur

I have a CALayer that implements drawInContext: and draws a simple circle, for example:

- (void)drawInContext:(CGContextRef)ctx
{
     CGContextScaleCTM(ctx, DrawingScale, DrawingScale);
     CGContextSetRGBFillColor (ctx, 1, 0, 0, 1);
     CGContextFillEllipseInRect (ctx, LocationRectangle);
}

I experimented with different ways to increase the size of my circle based on a touch event. I tried using beginAnimation UIView and CABasicAnimation, but they all blur the image. In my reading, I found that this is because CALayer seems to be processing as bitmap images for these parameters.

Fair enough ... but I want to scale my graphics without blur, for example. to use vector scaling.

So in my code, I have the DrawingScale property.

Is there a neat way to scale this property with the Layer animation? Or is this what people do with NSTimers (yuk)?

Perhaps this is one of the situations where I can use the Core Animation ability to animate custom properties? If so, I would like to know what example people find, is it the best, or which section of Apple's documents can be a good place to start this topic.

It seems that when it comes to graphics on Mac / iOS, there are many ways to throw / draw a cat ...?

+3
source share
2 answers

After several days of working on this, I want to have a variable called DrawingScale as a member variable (this is what they call in Obj-C), and use Core Animation to animate this property.

- (void)drawInContext:(CGContextRef)ctx
{
    CGFloat size = (CGFloat)(DrawingScale*DEFAULT_SIZE);
    CGRect elipseRect = CGRectMake(xPos, yPos, size, size);
    CGContextStrokeEllipseInRect(ctx, elipseRect);
    CGContextFillEllipseInRect (ctx, elipseRect);
}

Then in the event, I configured the animation as such:

-(void) : (CGPoint) location
{
    /* Set up an explicit animation to scale the players size up */
    CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"DrawingScale"];
    anim.fromValue = [NSNumber numberWithFloat: 1];
    anim.toValue = [NSNumber numberWithFloat: 5];
    anim.removedOnCompletion = YES;
    anim.duration = 0.1;
    anim.delegate = self;
    anim.fillMode = kCAFillModeForwards;
    anim.autoreverses = NO;
    [self addAnimation: anim forKey:@"ElipseGettingBigger"];
}

, DrawingScale 1 5 0,1 . .

, !

+3

(CAShapeLayer). , , , . , , , , ( /). - :

CALayer *layer = [CALayer layer];
[layer setMasksToBounds:YES];
[layer setBackgroundColor[[UIColor redColor] CGColor]];
[layer setCornerRadius:25.0f];
[layer setBounds:CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
[layer setPosition:CGPointMake([view bounds].size.width/2.0f, 
                               [view bounds].size.height/2.0f]);

[[view layer] addSublayer:layer];

WWDC Apple. Core Animation . . .

.

+2

Source: https://habr.com/ru/post/1750883/


All Articles