创建CGContext Line时图像消失

时间:2017-02-08 19:12:43

标签: ios objective-c

我正在使用Objective-C在Xcode上创建一个简单的迷你高尔夫游戏应用程序。我添加了一条引导线,如果用户触摸球并滑动手指,则一条线将显示球移动的方向。我能够创建该行,但是当我滑动屏幕时,我连接的图像(contextImage)消失了。我怎么做才能让图像不会消失?

这是我的touchesMoved代码:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
     UITouch *touch = [touches anyObject];
     movePoint = [touch locationInView:self.view];

     if (CGRectContainsPoint([ball frame], firstPoint)) {
          UIGraphicsBeginImageContext(self.view.frame.size);
          CGContextRef context = UIGraphicsGetCurrentContext();
          CGContextSetLineWidth(context, 1);
          CGContextSetStrokeColorWithColor(context, [[UIColor colorWithRed:1.0f green:1.0f blue:1.0 alpha:1.0] CGColor]);
          CGContextMoveToPoint(context, ball.center.x, ball.center.y);
          CGContextAddLineToPoint(context, movePoint.x, movePoint.y);
          CGContextStrokePath(context);

          self.contextImage.image = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
     }
}

0 个答案:

没有答案