首页 > 解决方案 > UITableViewCell setHighlighted:(BOOL)highlighted 始终为 false

问题描述

我正在重写UITableViewCell类以向我的单元格添加波纹/墨水效果。基于 iOS Material Component List,我唯一应该做的就是像这样覆盖 setHighlighted 方法:

- (void)setHighlighted:(BOOL)highlighted {
    [super setHighlighted:highlighted];
    if (highlighted) {
        [self startInk];
    } else {
        [self endInk];
    }
}

这种技术适用,UICollectionViewCell但我不确定它是否应该适用于UITableViewCell.

1

每当我推动单元格并且我没有抬起手指时,这个方法永远不会被调用。只有在何时被跟注并加注并且highlighted总是false

任何人都知道如何覆盖这个长按开始动画?

提前谢谢你!

标签: iosobjective-cuitableview

解决方案


我终于通过覆盖 (void)touchesBegan:withEvent: 方法得到它。这是我基于 iOS Material Component List的代码:

@property(strong, nonatomic, nonnull) MDCInkView *inkView;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    self.lastTouch = location;
    if (self.selectionStyle != UITableViewCellSelectionStyleNone){
        NSInteger selectionStyle = self.selectionStyle;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [UIView animateWithDuration:0.25f
                              delay:0.0
                            options:UIViewAnimationOptionCurveEaseOut
                         animations:^{
                             [self startInk];
                         }completion:^(BOOL finished) {
                             [UIView animateWithDuration:0.25f
                                                   delay:0.0
                                                 options:UIViewAnimationOptionCurveEaseOut
                                              animations:^{
                                                  [self endInk];
                                              }completion:^(BOOL finished) {
                                                  self.selectionStyle = selectionStyle;
                                              }];
                         }];
    }
    [super touchesBegan:touches withEvent:event];
}

- (void)startInk {
    [self.inkView startTouchBeganAtPoint:_lastTouch animated:YES withCompletion:nil];
}

- (void)endInk {
    [self.inkView startTouchEndAtPoint:_lastTouch animated:YES withCompletion:nil];
}



推荐阅读