0

我有一个表格视图,每个单元格都是一个自定义的表格单元格视图,有一个子视图,子视图使用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 的子视图的内容。

4

2 回答 2

0

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

  1. 表格视图处理UILongPressGestureRecognizer,在处理器中找出哪个单元格被触摸。
  2. 调用单元格的触摸处理函数
于 2013-01-16T09:25:11.957 回答
0

长按手势是连续的。当允许的手指数量 (numberOfTouchesRequired) 在指定的时间 (minimumPressDuration) 内被按下并且触摸没有超出允许的移动范围 (allowableMovement) 时,手势开始 (UIGestureRecognizerStateBegan)。每当手指移动时,手势识别器就会转换到 Change 状态,并在任何手指抬起时结束 (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);
    }
}
于 2013-01-11T04:20:29.400 回答