iOS 仿AppStore首页Today列表Cell触碰或按下效果

思路:通过touch事件来实现

新建AnimationBaseCell,需要有动画效果的Cell都来继承这个cell

首先,动画效果

- (void)jn_animate:(BOOL)highlight
{
    if (highlight) {
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.95, 0.95);
        } completion:^(BOOL finished) {
            
        }];
    }
    else{
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
            self.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
        }];
    }

}

其次,触发动画时机 拦截touch事件

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self jn_animate:YES];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [self jn_animate:NO];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
    [self jn_animate:NO];
}

 

最后,解决响应问题

子视图会先响应touch事件,通过hitTest方法让子视图不响应,全部由cell自身来响应

拦截事件后,比如按钮无法响应事件了,所以这里把按钮排除出去。

有其他需要自行添加条件

 

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *view = [super hitTest:point withEvent:event];
    if ([view isKindOfClass:[UIButton class]]) {
        return view;
    }
    if ([view isDescendantOfView:self]) {
        return self;
    }
    return view;
}

 

你可能感兴趣的:(iOS笔记)