我正在研究iPad的计算器。当用户键入一个函数,例如x ^ 2然后按下一个按钮时,它应该打开一个带有该函数图形的屏幕。当用户缩放时,坐标应相应地改变。目前它工作,但在最大缩放比例,它只显示整数值1,2,3 ...要求是,在继续缩放时,它应显示小数位,如0.1,0.2 ...至少一个小数位。在UIView drawRect方法中的方法
[AxesDrawer drawAxesInRect:self.bounds originAtPoint:midpoint scale:self.scale*self.contentScaleFactor];
调用,在绘制轴之后调用
[self drawHashMarksInRect:bounds originAtPoint:axisOrigin scale:pointsPerUnit];
绘制标签的方法:
#define ANCHOR_CENTER 0
#define ANCHOR_TOP 1
#define ANCHOR_LEFT 2
#define ANCHOR_BOTTOM 3
#define ANCHOR_RIGHT 4
#define HASH_MARK_FONT_SIZE 20.0
#define HORIZONTAL_TEXT_MARGIN 6
#define VERTICAL_TEXT_MARGIN 3
#define HASH_MARK_SIZE 3
#define MIN_PIXELS_PER_HASHMARK 25
+ (void)drawHashMarksInRect:(CGRect)bounds originAtPoint:(CGPoint)axisOrigin scale:
(CGFloat)pointsPerUnit
{
if (!pointsPerUnit) return;
if (((axisOrigin.x < bounds.origin.x) || (axisOrigin.x > bounds.origin.x+bounds.size.width)) &&
((axisOrigin.y < bounds.origin.y) || (axisOrigin.y > bounds.origin.y+bounds.size.height))) {
return;
}
int unitsPerHashmark = MIN_PIXELS_PER_HASHMARK * 2 / pointsPerUnit;
//NSLog(@"unitsperhashmark %d",unitsPerHashmark);
if (!unitsPerHashmark)
{
unitsPerHashmark = 1;
}
CGFloat pixelsPerHashmark = pointsPerUnit * unitsPerHashmark;
BOOL boundsContainsOrigin = CGRectContainsPoint(bounds, axisOrigin);
if (boundsContainsOrigin)
{
if ((axisOrigin.x - pixelsPerHashmark < bounds.origin.x) &&
(axisOrigin.x + pixelsPerHashmark > bounds.origin.x + bounds.size.width) &&
(axisOrigin.y - pixelsPerHashmark < bounds.origin.y) &&
(axisOrigin.y + pixelsPerHashmark > bounds.origin.y + bounds.size.height)) {
return;
}
}
else
{
if ((axisOrigin.y >= bounds.origin.y) &&
(axisOrigin.y <= bounds.origin.y+bounds.size.height) &&
(bounds.size.width <= pixelsPerHashmark)) {
return;
}
if ((axisOrigin.x >= bounds.origin.x) &&
(axisOrigin.x <= bounds.origin.x+bounds.size.width) &&
(bounds.size.height <= pixelsPerHashmark)) {
return;
}
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5);
CGContextBeginPath(context);
int started = NO;
int stillGoing = YES;
NSLog(@"unitsperhashmark 2 %d",unitsPerHashmark);
for (int offset = unitsPerHashmark; !started || stillGoing; offset += unitsPerHashmark)
{
NSString *positiveLabel = nil;
NSString *negativeLabel = nil;
BOOL drew = NO;
CGFloat scaledOffset = floor(offset * pointsPerUnit);
CGPoint hashMarkPoint;
hashMarkPoint.x = axisOrigin.x+scaledOffset;
hashMarkPoint.y = axisOrigin.y;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x, hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context, hashMarkPoint.x, hashMarkPoint.y+HASH_MARK_SIZE);
if (!positiveLabel) positiveLabel = [NSString stringWithFormat:@"%d", offset];
//NSLog(@"positivelabel %@",positiveLabel);
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
drew = YES;
}
hashMarkPoint.x = axisOrigin.x-scaledOffset;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x, hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context, hashMarkPoint.x, hashMarkPoint.y+HASH_MARK_SIZE);
if (boundsContainsOrigin) negativeLabel = positiveLabel;
if (!negativeLabel) negativeLabel = [NSString stringWithFormat:@"%d", (boundsContainsOrigin ?
offset : -offset)];
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
drew = YES;
}
hashMarkPoint.x = axisOrigin.x;
hashMarkPoint.y = axisOrigin.y-scaledOffset;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x-HASH_MARK_SIZE, hashMarkPoint.y);
CGContextAddLineToPoint(context, hashMarkPoint.x+HASH_MARK_SIZE, hashMarkPoint.y);
if (!positiveLabel) {
if (boundsContainsOrigin) positiveLabel = negativeLabel;
if (!positiveLabel) positiveLabel = [NSString stringWithFormat:@"%d", offset];
}
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
drew = YES;
}
hashMarkPoint.y = axisOrigin.y+scaledOffset;
if (CGRectContainsPoint(bounds, hashMarkPoint)) {
CGContextMoveToPoint(context, hashMarkPoint.x-HASH_MARK_SIZE, hashMarkPoint.y);
CGContextAddLineToPoint(context, hashMarkPoint.x+HASH_MARK_SIZE, hashMarkPoint.y);
if (!negativeLabel) {
if (boundsContainsOrigin) negativeLabel = positiveLabel;
if (!negativeLabel) negativeLabel = [NSString stringWithFormat:@"%d", (boundsContainsOrigin ?
offset : -offset)];
}
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
drew = YES;
}
positiveLabel = nil;
negativeLabel = nil;
if (drew) started = YES;
stillGoing = drew;
}
CGContextStrokePath(context);
}
我真的很感激任何帮助。提前谢谢。
祝你好运
答案 0 :(得分:0)
好的。我假设
positiveLabel = [NSString stringWithFormat:@“%d”,offset];
positiveLabel负责绘制字符串,格式为%d(十进制)
将其更改为%f(浮动),因此显示的值将处于浮点状态。
答案 1 :(得分:0)
我更新了方法,以便用作计算井号的步骤的偏移量变量根据缩放而变化。当较少的散列标记为1时,如果用户继续缩放,则散列标记具有步骤0.2,并且如果他继续缩放,则它们具有步骤0.1。为此我使用了两个不同的循环,一个用于int hashmarks,一个用于float hash
+(void)drawHashMarksInRect:(CGRect)bounds originAtPoint:(CGPoint)axisOrigin scale:
(CGFloat的)pointsPerUnit
{
if(!pointsPerUnit)返回;
if(((axisOrigin.x&lt; bounds.origin.x)||(axisOrigin.x&gt; bounds.origin.x + bounds.size.width))&amp;&amp;
((axisOrigin.y&lt; bounds.origin.y)||(axisOrigin.y&gt; bounds.origin.y + bounds.size.height))){
返回;
}
int unitsPerHashmark = MIN_PIXELS_PER_HASHMARK * 2 / pointsPerUnit;
if(!unitsPerHashmark)
{
unitsPerHashmark = 1;
}
CGFloat pixelsPerHashmark = pointsPerUnit * unitsPerHashmark;
BOOL boundsContainsOrigin = CGRectContainsPoint(bounds,axisOrigin);
if(boundsContainsOrigin)
{
if((axisOrigin.x - pixelsPerHashmark&lt; bounds.origin.x)&amp;&amp;
(axisOrigin.x + pixelsPerHashmark&gt; bounds.origin.x + bounds.size.width)&amp;&amp;
(axisOrigin.y - pixelsPerHashmark&lt; bounds.origin.y)&amp;&amp;
(axisOrigin.y + pixelsPerHashmark&gt; bounds.origin.y + bounds.size.height)){
返回;
}
}
其他
{
if((axisOrigin.y&gt; = bounds.origin.y)&amp;&amp;
(axisOrigin.y&lt; = bounds.origin.y + bounds.size.height)&amp;&amp;
(bounds.size.width&lt; = pixelsPerHashmark)){
返回;
}
if((axisOrigin.x&gt; = bounds.origin.x)&amp;&amp;
(axisOrigin.x&lt; = bounds.origin.x + bounds.size.width)&amp;&amp;
(bounds.size.height&lt; = pixelsPerHashmark)){
返回;
}
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,5);
CGContextBeginPath(上下文);
int started = NO;
int stillGoing = YES;
float decimalUnits;
if((unitsPerHashmark == 1)&amp;&amp;(pointsPerUnit&gt; 86))
{
if(pointsPerUnit&gt; 490){
decimalUnits = 0.1;
} else if(pointsPerUnit&gt; 420){
decimalUnits = 0.2;
}其他
{
decimalUnits = 0.5;
}
for(float offsetFloat = decimalUnits;!started || stillGoing; offsetFloat + = decimalUnits)
{
NSString * positiveLabel = nil;
NSString * negativeLabel = nil;
BOOL画了= NO;
CGFloat scaledOffset = floor(offsetFloat * pointsPerUnit);
CGPoint hashMarkPoint;
hashMarkPoint.x = axisOrigin.x + scaledOffset;
hashMarkPoint.y = axisOrigin.y;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x,hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context,hashMarkPoint.x,hashMarkPoint.y + HASH_MARK_SIZE);
if(!positiveLabel)
{
positiveLabel = [NSString stringWithFormat:@&#34;%0.01f&#34;,offsetFloat];
if([[positiveLabel substringFromIndex:1] compare:@&#34; .0&#34;] == NSOrderedSame){
positiveLabel = [positiveLabel substringToIndex:1];
}
}
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
画了=是;
}
hashMarkPoint.x = axisOrigin.x-scaledOffset;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x,hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context,hashMarkPoint.x,hashMarkPoint.y + HASH_MARK_SIZE);
if(boundsContainsOrigin)negativeLabel = [NSString stringWithFormat:@&#34; - %@&#34;,positiveLabel];
if(!negativeLabel)negativeLabel = [NSString stringWithFormat:@&#34;%0.01f&#34;,(boundsContainsOrigin
? offsetFloat:-offsetFloat)];
if([[negativeLabel substringFromIndex:1] compare:@&#34; .0&#34;] == NSOrderedSame){
negativeLabel = [negativeLabel substringToIndex:1];
}
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
画了=是;
}
hashMarkPoint.x = axisOrigin.x;
hashMarkPoint.y = axisOrigin.y-scaledOffset;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x-HASH_MARK_SIZE,hashMarkPoint.y);
CGContextAddLineToPoint(context,hashMarkPoint.x + HASH_MARK_SIZE,hashMarkPoint.y);
if(!positiveLabel){
if(boundsContainsOrigin)
{
if([negativeLabel characterAtIndex:0] ==&#39; - &#39;){
positiveLabel = [negativeLabel substringFromIndex:1];
}其他
{
positiveLabel = negativeLabel;
}
}
if(!positiveLabel)positiveLabel = [NSString stringWithFormat:@&#34;%0.01f&#34;,offsetFloat];
if([[positiveLabel substringFromIndex:1] compare:@&#34; .0&#34;] == NSOrderedSame){
positiveLabel = [positiveLabel substringToIndex:1];
}
}
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
画了=是;
}
hashMarkPoint.y = axisOrigin.y + scaledOffset;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x-HASH_MARK_SIZE,hashMarkPoint.y);
CGContextAddLineToPoint(context,hashMarkPoint.x + HASH_MARK_SIZE,hashMarkPoint.y);
if(!negativeLabel){
if(boundsContainsOrigin)negativeLabel = [NSString stringWithFormat:@&#34; - %@&#34;,positiveLabel];
if(!negativeLabel)negativeLabel = [NSString stringWithFormat:@&#34;%0.01f&#34;,(boundsContainsOrigin
? offsetFloat:-offsetFloat)];
if([[negativeLabel substringFromIndex:1] compare:@&#34; .0&#34;] == NSOrderedSame){
negativeLabel = [negativeLabel substringToIndex:1];
}
}
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
画了=是;
}
positiveLabel = nil;
negativeLabel = nil;
if(画)开始= YES;
stillGoing = drew;
}
}
其他
{
for(int offset = unitsPerHashmark;!started || stillGoing; offset + = unitsPerHashmark)
{
NSString * positiveLabel = nil;
NSString * negativeLabel = nil;
BOOL画了= NO;
CGFloat scaledOffset = floor(offset * pointsPerUnit);
CGPoint hashMarkPoint;
hashMarkPoint.x = axisOrigin.x + scaledOffset;
hashMarkPoint.y = axisOrigin.y;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x,hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context,hashMarkPoint.x,hashMarkPoint.y + HASH_MARK_SIZE);
if(!positiveLabel)positiveLabel = [NSString stringWithFormat:@&#34;%d&#34;,offset];
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
画了=是;
}
hashMarkPoint.x = axisOrigin.x-scaledOffset;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x,hashMarkPoint.y-HASH_MARK_SIZE);
CGContextAddLineToPoint(context,hashMarkPoint.x,hashMarkPoint.y + HASH_MARK_SIZE);
if(boundsContainsOrigin)negativeLabel = [NSString stringWithFormat:@&#34; - %@&#34;,positiveLabel];
if(!negativeLabel)negativeLabel = [NSString stringWithFormat:@&#34;%d&#34;,(boundsContainsOrigin?
offset:-offset)];
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_TOP];
画了=是;
}
hashMarkPoint.x = axisOrigin.x;
hashMarkPoint.y = axisOrigin.y-scaledOffset;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x-HASH_MARK_SIZE,hashMarkPoint.y);
CGContextAddLineToPoint(context,hashMarkPoint.x + HASH_MARK_SIZE,hashMarkPoint.y);
if(!positiveLabel){
if(boundsContainsOrigin)
{
if([negativeLabel characterAtIndex:0] ==&#39; - &#39;){
positiveLabel = [negativeLabel substringFromIndex:1];
}其他
{
positiveLabel = negativeLabel;
}
}
if(!positiveLabel)positiveLabel = [NSString stringWithFormat:@&#34;%d&#34;,offset];
}
[self drawString:positiveLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
画了=是;
}
hashMarkPoint.y = axisOrigin.y + scaledOffset;
if(CGRectContainsPoint(bounds,hashMarkPoint)){
CGContextMoveToPoint(context,hashMarkPoint.x-HASH_MARK_SIZE,hashMarkPoint.y);
CGContextAddLineToPoint(context,hashMarkPoint.x + HASH_MARK_SIZE,hashMarkPoint.y);
if(!negativeLabel){
if(boundsContainsOrigin)negativeLabel = [NSString stringWithFormat:@&#34; - %@&#34;,positiveLabel];
if(!negativeLabel)negativeLabel = [NSString stringWithFormat:@&#34; - %d&#34;,(boundsContainsOrigin?
offset:-offset)];
}
[self drawString:negativeLabel atPoint:hashMarkPoint withAnchor:ANCHOR_LEFT];
画了=是;
}
positiveLabel = nil;
negativeLabel = nil;
if(画)开始= YES;
stillGoing = drew;
}
}
CGContextStrokePath(上下文);
}