如何将CGContext实现到实际代码中?

时间:2014-05-22 02:46:44

标签: objective-c

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint endlocation = [touch locationInView:touch.view];
UIImage* thing;
UIImageView* thing2 = [[UIImageView alloc] init];
UIImageView* thing3=[[UIImageView alloc] init];
switch ([_diagramselect selectedSegmentIndex]) {
    case 0:
        thing = [UIImage imageNamed:@"makeO"];
        [thing2 setFrame:CGRectMake(endlocation.x-72,endlocation.y-172,25,25)];
        [thing2 setImage:thing];
        [_field addSubview:thing2];
        break;
    case 1:
        thing = [UIImage imageNamed:@"missX"];
        [thing2 setFrame:CGRectMake(endlocation.x-72,endlocation.y-172,25,25)];
        [thing2 setImage:thing];
        [_field addSubview:thing2];
        break;
    case 2:
        CGContextRef *context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColor(context,black);
        CGContextBeginPath(context);
        CGContextMoveToPoint(context,startlocation.x,startlocation.y);
        CGContextAddLineToPoint(context,endlocation.x,endlocation.y);
        CGContextStrokePath(context);





        /*thing = [UIImage imageNamed:@"passArrow"];
        [thing3 setFrame:CGRectMake(startlocation.x-72,startlocation.y-172,endlocation.x-startlocation.x,endlocation.y-startlocation.y)];
        [thing3 setImage:thing];
        [_field addSubview:thing3];
      */

        break;
    case 3:
        thing = [UIImage imageNamed:@"trussArrow"];
        break;

    default:
        break;
}

案例2旨在从水龙头的初始位置绘制到水龙头的最终位置。但是,以下代码给出了"期望表达式"的错误。在CGContextRef的行上,因此以下行不识别上下文。这是什么问题?是否存在此代码需要定位的特定视图,如果是,我将如何执行此操作?谢谢!

1 个答案:

答案 0 :(得分:1)

尝试不使用指针引用星号:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColor(context,black);
CGContextBeginPath(context);
CGContextMoveToPoint(context,startlocation.x,startlocation.y);
CGContextAddLineToPoint(context,endlocation.x,endlocation.y);
CGContextStrokePath(context);

此外,根据您的目的,您可能需要查看UIGraphicsBeginImageContext()UIGraphicsEndImageContext()