I tried to override the drawRect method in a simple UIView extension to draw a rectangle with rounded edges and fill it with black color, and also animate the view to resize on touch. The thing is, resizing completely deforms the drawing, as if the context remains the same for the whole time. drawRect is never called during the animation.. However, if I tell the view to draw itself after the animation it gets drawn correctly.
How can I animate the resizing without the drawing becoming deformed?
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat radius = rect.size.height * 0.5f;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect),
CGRectGetMaxX(rect), CGRectGetMaxY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMaxY(rect),
CGRectGetMinX(rect), CGRectGetMaxY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect),
CGRectGetMinX(rect), CGRectGetMinY(rect), radius);
CGPathAddArcToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect),
CGRectGetMaxX(rect), CGRectGetMinY(rect), radius);
CGPathCloseSubpath(path);
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextAddPath(context, path);
CGContextFillPath(context);
CGContextRestoreGState(context);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:1.f animations:^{
self.frame = CGRectInset(self.frame, 0.f, -100.f);
}];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:1.f animations:^{
self.frame = CGRectInset(self.frame, 0.f, +100.f);
}];
}