UITableViewCell setHighlighted:(BOOL)highlighted始终为false

时间:2019-06-20 11:04:38

标签: ios objective-c uitableview

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

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

此技术适用于UICollectionViewCell,但我不确定是否应适用于UITableViewCell

1

每当我推电池并且不抬起手指时,就永远不会调用此方法。仅在引发并加注时才被调用,highlighted始终为false

任何人都知道如何覆盖此长按以开始动画吗?

先谢谢您!

1 个答案:

答案 0 :(得分:0)

我终于通过重写(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];
}