I am attempting to create a "marching ants" (an animated dashed line) selection indicator around UIView that contains a UIImageView subview. Drawing the dashed line around the perimeter of the UIView is trivial using core graphics:
-(void) drawSelectedMarquee{
CGContextRef c=UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(c, [[UIColor whiteColor] CGColor] );
CGRect f=self.bounds;
CGContextSetLineWidth(c, 2.0);
CGFloat dashes[]={1,2};
CGContextSetLineDash(c, 0, dashes, 2);
CGContextBeginPath(c);
CGContextAddRect(c, f);
CGContextStrokePath(c);
}
However, I need to scale (and rotate) the view using affine transforms so that the UIImageView inside can be scaled and rotated:
-(void) scaleByX:(CGFloat) xScale andY:(CGFloat) yScale{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.1];
[UIView setAnimationBeginsFromCurrentState:YES];
self.transform=CGAffineTransformScale(self.transform, xScale, yScale);
[UIView commitAnimations];
}
However, because core graphics draws in relative "user space points" which apparently are scaled by the transform as well, scaling the view causes the dashed line to scale in proportion. So, as the view scale smaller, the line grows thinner and the dashes closer together and has the view scales larger, the line grows thicker and the dashes father apart.
As an interface element, the selection marquee should remain of fixed width with fixed sized dashes. I've tried just rotating and scaling the UIImageView subview of the main view but that causes the UIImageView to scale out of the superview.
I can think of some brute force methods to properly draw the marquee. I've could track the absolute size of the view and then recalculate the end user space units every time it redraws or I create a new view with selection marquee every time the UIImageView. But those solutions are inelegant and bug prone.
I think I've missed something in documentation about how to draw and transform graphics in UIViews.
[Update: I've decided that using marching ants isn't useful on the iPhone's small screen so I've adopted another method. However I'm still interested in the solution to this particular problem.]