【发布时间】:2009-10-15 19:26:53
【问题描述】:
我正在尝试围绕 UIView 创建一个 "marching ants" (an animated dashed line) selection indicator,其中包含一个 UIImageView 子视图。使用核心图形在 UIView 周围绘制虚线很简单:
-(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);
}
但是,我需要使用仿射变换来缩放(和旋转)视图,以便可以缩放和旋转内部的 UIImageView:
-(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];
}
但是,由于核心图形绘制了相对“用户空间点”,这些点显然也被变换缩放,缩放视图会导致虚线按比例缩放。因此,随着视图比例变小,线变细,虚线更靠近,视图缩放更大,线变粗,虚线分开。
作为界面元素,选择框应保持固定宽度和固定大小的破折号。我试过旋转和缩放主视图的 UIImageView 子视图,但这会导致 UIImageView 超出超级视图。
我可以想出一些蛮力方法来正确绘制选取框。我可以跟踪视图的绝对大小,然后在每次重绘时重新计算最终用户空间单位,或者在每次 UIImageView 时创建一个带有选择框的新视图。但这些解决方案不优雅且容易出错。
我想我在文档中遗漏了有关如何在 UIView 中绘制和转换图形的内容。
[更新:我已经决定在 iPhone 的小屏幕上使用行军蚂蚁没有用,所以我采用了另一种方法。但是我仍然对这个特定问题的解决方案感兴趣。]
【问题讨论】:
标签: iphone cocoa cocoa-touch iphone-sdk-3.0