【问题标题】:I want to draw a graphic over my existing UIView我想在现有的 UIView 上绘制图形
【发布时间】:2015-01-19 16:24:43
【问题描述】:

我想创建一个模拟速度计的 UIView。我创建了一个新类 SpeedometerView 并将其链接到我的主视图上的 UIView。然后使用下面的代码,我创建了一个类似于速度计的图像。

#import "SpeedometerView.h"

@implementation SpeedometerView
static int gX = 57;
static int gY = 180;

- (void)drawRect:(CGRect)rect
{
    CGContextRef contextRef = UIGraphicsGetCurrentContext();

    CGContextFillEllipseInRect(contextRef, CGRectMake(135, 105, 10, 10));
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.0);

    CGContextStrokeEllipseInRect(contextRef, CGRectMake(30, 0, 220, 220));
    CGContextSetRGBFillColor(contextRef, 0, 0, 255, 1.5);

    CGContextSetStrokeColorWithColor(contextRef, [[UIColor redColor] CGColor]);
    CGContextSetLineWidth(contextRef, 3.0);
    CGContextMoveToPoint(contextRef, 140.0, 110.0);
    CGContextAddLineToPoint(contextRef, gX, gY);

    CGContextDrawPath(contextRef, kCGPathStroke);

    CGContextSetFillColor(contextRef, CGColorGetComponents([UIColor redColor].CGColor));

    CGContextBeginPath(contextRef);
    CGContextMoveToPoint(contextRef, gX, gY);
    CGContextAddLineToPoint(contextRef, gX + 1, gY - 5);
    CGContextAddLineToPoint(contextRef, gX + 5, gY - 1);
    CGContextClosePath(contextRef);
    CGContextDrawPath(contextRef, kCGPathFillStroke);
}

@end

现在的问题是我想将值传递给这个类以根据速度移动指针。为此,我已经设置了 gX 和 gY 的值,但我不确定如何继续。将值发送到 SpeedometerView 的代码如下,我只是不确定如何每次刷新 SpeedometerView。

-(void) updateSpeed
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.gX = 50 + resultVSC;
    appDelegate.gY = 180 - resultVSC;
    dispatch_async(dispatch_get_main_queue(), ^{
    if (resultVSC > 0)
        self.mphOutput.text = [[NSString alloc] initWithFormat:@"%d Mph", resultVSC];
    //Update SpeedometerView??
    [self.view setNeedsDisplay];
    });
}

【问题讨论】:

  • 运行代码时会发生什么? setNeedsDisplay 绝对是用于重绘视图 - 您不应该使用静态 gX、gY 等,而是以某种方式将其作为数据模型的一部分并在重绘时从视图中访问它......

标签: ios xcode core-graphics cgrect


【解决方案1】:

您需要更改 SpeedometerView 的“gX”和“gY”。当你这样做时,调用“setNeedsDisplay”和drawInRect: 将被调用。关键是改变这些值,这会在绘制方法中绘制 UIView 时体现出来。

【讨论】:

    【解决方案2】:

    您可以实现 Observer 来监听变化或使用 NSNotifications 并刷新速度计以更新值。

    【讨论】:

      【解决方案3】:

      使用属性而不是statics。

      @interface SpeedometerView : ...
      @property int gX = 57;
      @property int gY = 180;
      @end
      

      您可以在drawRect: 中访问这些内容,如下所示:

      - (void)drawRect:(CGRect)rect {
        // ...
        CGContextAddLineToPoint(contextRef, self.gX, self.gY);
        // ...
      }
      

      您可以像这样访问updateSpeed

      - (void)updateSpeed {
        // ...
        dispatch_async(dispatch_get_main_queue(), ^{
        if (resultVSC > 0)
          self.mphOutput.text = [[NSString alloc] initWithFormat:@"%d Mph", resultVSC];
          self.view.gX = appDelegate.gX;
          self.view.gY = 180 + resultVSC;
          [self.view setNeedsDisplay];
        });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 2020-02-05
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        相关资源
        最近更新 更多