UILongPressGestureRecognizer处理程序使用不正确的对象

时间:2013-01-11 04:04:16

标签: iphone objective-c uigesturerecognizer

我有一个表视图,每个单元格是一个自定义的tablecell视图,有一个子视图,子视图使用CoreText绘制文本。 子视图的类名是CCoreTextView,下面是CCoreTextView的一些代码

@interface CoreTextView()
{
    CTFrameRef _frame;
}
@end

- (void) initWithFrame:(CGRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) {
        UILongPressGestureRecognizer *myLongPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(myLongPressHandle:)];
        [self addGestureRecognizer:myLongPressRecognizer];
        [myLongPressRecognizer release];

    }
}


- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawText:context];
}

- (void)drawText:(CGContextRef)context
{
    CTFontRef font = CTFontCreateWithName((CFStringRef)self.font.fontName, self.font.pointSize, NULL);
    NSDictionary *attribs = [NSDictionary dictionaryWithObjectsAndKeys:(id)font, kCTFontAttributeName, nil];
    NSMutableAttributedString *attribString = [[NSMutableAttributedString alloc] initWithString:self.text attributes:attribs];
    CFRelease(font);

    CGContextSetTextMatrix(context, CGAffineTransformIdentity);
    CGContextTranslateCTM(context, 0, self.bounds.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGFloat headIdent = CORETEXTVIEW_HEAD_IDENT;
    CGFloat tailIdent = CORETEXTVIEW_TAIL_IDENT;
    CTParagraphStyleSetting settings[] = {
        {kCTParagraphStyleSpecifierAlignment, sizeof(_textAlignment), &_textAlignment },
        {kCTParagraphStyleSpecifierLineSpacing, sizeof(CGFloat), &_leading},
        {kCTParagraphStyleSpecifierFirstLineHeadIndent, sizeof(CGFloat), &_firstLineHeadIndent},
        {kCTParagraphStyleSpecifierHeadIndent, sizeof(CGFloat), &headIdent},
        {kCTParagraphStyleSpecifierTailIndent, sizeof(CGFloat), &tailIdent}

    };

    CTParagraphStyleRef paragraphStyle = CTParagraphStyleCreate(settings, sizeof(settings) / sizeof(settings[0]));

    CFAttributedStringSetAttribute((CFMutableAttributedStringRef)attribString,
                               CFRangeMake(0, [self.text length]),
                               kCTParagraphStyleAttributeName, 
                               paragraphStyle);
    CFRelease(paragraphStyle);

    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attribString);


    CGRect columnFrame = CGRectMake(0,
                                0,
                                self.bounds.size.width,
                                self.bounds.size.height);
    columnFrame = UIEdgeInsetsInsetRect(columnFrame, UIEdgeInsetsMake(0, CORETEXTVIEW_EDGE_INSET_LEFT, 0, CORETEXTVIEW_EDGE_INSET_RIGHT));

    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, &CGAffineTransformIdentity, columnFrame);

    CFRange textRange = CFRangeMake(0, 0);
    if (NULL != _frame) {
        CFRelease(_frame);
        _frame = NULL;
    }
    _frame = CTFramesetterCreateFrame(framesetter, textRange, framePath, NULL);
    CTFrameDraw(_frame, context);

    CFRelease(framePath);


    [attribString release];
    CFRelease(framesetter);

}

- (void)myLongPressHandle:(UILongPressGestureRecognizer *)gestureRecognizer
{

    CGPoint point  = [gestureRecognizer locationInView:self];

    NSArray *lineArr    = (NSArray*)CTFrameGetLines(_frame);
    NSInteger lineCount = [lineArr count];
    NSLog(@"lineCount=%d, frame=%@, lineArr=%@", lineCount, _frame, lineArr);
}

问题是当我按下表格视图的一个单元格(名为CellA)时,不要抬起手指,将手指移动到另一个单元格(名为CellB),我发现longPressHandle仍然使用CellA的子视图。 NSLog打印CellA子视图的内容。

2 个答案:

答案 0 :(得分:0)

  

长按手势是连续的。手势开始了   (UIGestureRecognizerStateBegan)当允许手指的数量   (numberOfTouchesRequired)已按下指定的时间段   (minimumPressDuration)和触摸不会超越   允许的移动范围(allowableMovement)。手势   只要手指移动,识别器就会转换到更改状态,   当任何一个手指结束时(UIGestureRecognizerStateEnded)   被解除了。

所以你必须检查状态以检测你的

- (void)myLongPressHandle:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if(gesture.state == UIGestureRecognizerStateBegan)
    {
        //if needed do some initial setup or init of views here
    }
    else if(gesture.state == UIGestureRecognizerStateChanged)
    {
        //move your views here.
        [yourView setFrame:];
    }
    else if(gesture.state == UIGestureRecognizerStateEnded)
    {
CGPoint point  = [gestureRecognizer locationInView:self];

NSArray *lineArr    = (NSArray*)CTFrameGetLines(_frame);
NSInteger lineCount = [lineArr count];
NSLog(@"lineCount=%d, frame=%@, lineArr=%@", lineCount, _frame, lineArr);
    }
}

答案 1 :(得分:0)

我终于找到了解决问题的方法。

  1. 表视图处理UILongPressGestureRecognizer,在处理器中找出触摸的单元格。
  2. 调用单元格的触摸处理功能