【问题标题】:Get touches in UIImage drawn with CGContext在使用 CGContext 绘制的 UIImage 中获取触摸
【发布时间】:2012-03-14 00:37:57
【问题描述】:

我正在尝试检测用户是否点击了具有 CGContext 和如下路径的绘图中的图像(这发生在名为 SwbPlate 的 customview 类中:

  SwbPlateImage * image;
    CGFloat startAngle;
    CGFloat endAngle;

    CGContextRef context = UIGraphicsGetCurrentContext();


    for (image in plateImages) {
        endAngle = [EntGeometry pointToRadian:[image endPoint] withCenterPoint:CGPointMake(self.bounds.size.width/2 , self.bounds.size.height /2)];
        startAngle = [EntGeometry pointToRadian:[image startPoint] withCenterPoint:CGPointMake(self.bounds.size.width/2 , self.bounds.size.height /2)];
        imageToDraw = [image plateImg];
        NSLog(@"%@", image);
        CGContextSaveGState(context);
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, self.bounds.size.width/2 , self.bounds.size.height/2);
        CGPathAddArc( path, 
                    NULL, 
                    self.bounds.size.width/2, 
                    self.bounds.size.height/2, 
                    (self.bounds.size.width/2),
                    endAngle,
                    startAngle,
                    0);
        CGPathCloseSubpath(path);

        CGContextAddPath(context, path);
        CGContextClip(context);

        CGContextTranslateCTM(context, 0, self.bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, rect, [imageToDraw CGImage]);
        CGContextRestoreGState(context);

        //RVE MOD
        image.imageContext = context;

然后我尝试查看用户是否在此上下文中的某个点上移动了触摸,但这似乎不起作用

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];



    NSArray *allTouches = [touches allObjects]; 
UITouch *touch = [allTouches objectAtIndex:0];
SwbDragProduct*dragProd;





for (UIView *view in self.view.subviews) {

    if ([view isKindOfClass:[SwbPlateView class]] &(CGRectContainsPoint([view frame], [touch locationInView:self.view])) ) {
        NSLog(@" We touched in the PLATE");
        for (SwbPlateImage *imgView in plateView.plateImages){

            if(CGContextPathContainsPoint(imgView.imageContext, [touch locationInView:self.view],kCGPathFillStroke)){
                NSLog(@" We Touched within the IMAGE");
            }
        }
    }

【问题讨论】:

    标签: iphone ios xcode cocoa-touch ipad


    【解决方案1】:

    在我看来,您有 SwbPlateImage 局部变量,您在其中分配上下文,当您检查上下文时,该变量在 for 循环中不可用。您将需要管理和存储 SwbPlateImage 的上下文对象的 Singleton 类(例如 SwbPlateImageSingleTon)。如果您可以提供有关问题中提到的类的更多详细信息,我可以帮助您编写单例类。

    从您的问题看来,您需要将 CGContextRef 的 NSMutableArray(say contextArray) 作为单例类中的强对象。与其将上下文分配给 image.imageContext,不如将上下文添加到此数组中。

     [[SwbPlateImageSingleTon sharedInstance] contextArray] addObject:context]
    

    那么你的内部循环将如下所示。

       for (CGContextRef context in [[SwbPlateImageSingleTon sharedInstance] contextArray]) {
            if(CGContextPathContainsPoint(context, [touch locationInView:self.view],kCGPathFillStroke)) {
                NSLog(@" We Touched within the IMAGE");
            }
        }
    

    单例类

    SwbPlateImageSingleTon.h

    @interface SwbPlateImageSingleTon : NSObject
      @property (strong, nonatomic) NSMutableArray *contextArray;
      + (SwbPlateImageSingleTon*)sharedInstance;
    @end
    

    SwbPlateImageSingleTon.m

    #import "SwbPlateImageSingleTon.h"
    
    @implementation SwbPlateImageSingleTon
    static SwbPlateImageSingleTon *sharedObject = nil;
    
    + (SwbPlateImageSingleTon*)sharedInstance
    {
        @synchronized(self) {
            if (!sharedObject) {
                sharedObject = [[SwbPlateImageSingleTon alloc] init];
            }
            return sharedObject;
        }
    }
    @end
    

    【讨论】:

      【解决方案2】:

      我正在寻找的是对 CGContextImage 的点击检测,我发现为了执行我需要的操作,检测 CGContext 图像上的点击不是正确的方法。

      我有一个内存中的点列表,我知道它们的坐标,为了检测 uiscrollview 上的点击,我对这些点进行了迭代,以查看我点击的点是否在我已经绘制的图像的边界框内。这对我来说非常有效,我可以通过计算坐标和更新滚动视图的比例等来检测图像上的点击,从而将坐标也转换为正确的比例。

      所以简而言之,我得到的答案是不可能让 CGContext 图像来检测点击,而是使用它们的已知位置来自己识别点击。

      谢谢 达米安

      【讨论】:

      • 嗨乔纳森,我了解实际计算触摸点是否已触及正在绘制的区域的选项,但我想知道是否有一种方法可以检测使用 drawInRect 绘制的 uiimage 上的触摸CG上下文。我已经实现了计算工作正常的接触点。我认为您的方法对于人们在这种情况下使用来说很好而且很方便,所以我会将赏金奖励给您:)
      【解决方案3】:
      - (BOOL)pathContainsPoint:(NSPoint)point forMode:(CGPathDrawingMode)mode
      {
      CGPathRef path = [self quartzPath]; // Custom method to create a CGPath
      CGContextRef cgContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
      CGPoint cgPoint;
      BOOL containsPoint = NO;
      
           cgPoint.x = point.x;
          cgPoint.y = point.y;
      
           // Save the graphics state before doing the hit detection.
          CGContextSaveGState(cgContext);
      
      CGContextAddPath(cgContext, path);
      containsPoint = CGContextPathContainsPoint(cgContext, cgPoint, mode);
      
           CGContextRestoreGState(cgContext);
      
           return containsPoint;
      }
      

      【讨论】:

      • 也许是因为这正是他们想要的?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-03
      相关资源
      最近更新 更多