简体   繁体   English

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

[英]UILongPressGestureRecognizer Handler use incorrect object

I have a table view, each cell is a custom tablecell view, have a subview, the subview use CoreText draw text. 我有一个表格视图,每个单元格是一个自定义表格单元格视图,有一个子视图,该子视图使用CoreText绘制文本。 The sub view's class name is CCoreTextView, below are some code of the CCoreTextView 子视图的类名是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);
}

The problem is when I press one cell (named CellA) of the table view, do not lift finger, move the finger to another cell(named CellB), i find the longPressHandle still use CellA's subview. 问题是当我按表格视图的一个单元格(名为CellA)时,不要抬起手指,将手指移到另一个单元格(名为CellB),我发现longPressHandle仍在使用CellA的子视图。 NSLog print the content of CellA's subview. NSLog打印CellA子视图的内容。

Long-press gestures are continuous. 长按手势是连续的。 The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). 当在指定时间段内(minimumPressDuration)按下了允许的手指数(numberOfTouchesRequired),并且触摸未超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognizerStateBegan)。 The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted. 每当手指移动时,手势识别器都会转换为“更改”状态,并且在任何手指抬起时手势识别器都会终止(UIGestureRecognizerStateEnded)。

So you have to check state to detect your 因此,您必须检查状态以检测您的

- (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);
    }
}

I finally find a way to solve the problem. 我终于找到解决问题的方法。

  1. table view handle the UILongPressGestureRecognizer, in the processor find out which cell is touched. 表格视图处理UILongPressGestureRecognizer,在处理器中找出被触摸的单元格。
  2. call the cell's touch process function 调用单元格的触摸处理功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM