今天给 UICollectionViewCell 添加一个点击放大图片的效果 类似button的选中高亮效果,
放大方式:使用添加动画放大
(1)添加手势的,手势只能创建 单击 移动分开等,还会禁止掉 didSelectItemAtIndexPath
(2)我采用 touch相关方法 来处理---为了不用修改didSelectItemAtIndexPath 只在cell中添加相关代码即可
采用 2 的方式
实现功能是
1.点击cell后 --添加动画
2.手势 end/move出cell 取消动画 collectionView滑动后 也取消动画
具体代码:
手势监听
下面调用super,是为了不影响didSelectItemAtIndexPath 等方法的调用
(有部分没有调用super的时候,didSelectItemAtIndexPath有时候不能正常响应)
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self addAnimationTransformScale];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self removeAnimation];
[super touchesEnded:touches withEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch=[touches anyObject];
//取得当前位置
CGPoint current=[touch locationInView:self];
if (NO == [self pointInside:current withEvent:event]) {
[self removeAnimation];
}
[super touchesMoved:touches withEvent:event];
}
// scrollview滑动或者打电话等 中断
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self removeAnimation];
[super touchesCancelled:touches withEvent:event];
}
添加和删除动画
- (void)removeAnimation {
[self.imageView.layer removeAllAnimations];
}
- (void)addAnimationTransformScale {
CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulse.duration = 0.08;
pulse.fillMode = kCAFillModeForwards; //保留动画最后的状态
pulse.fromValue= [NSNumber numberWithFloat:1];
pulse.toValue= [NSNumber numberWithFloat:1.3];
pulse.removedOnCompletion = NO;
[self.imageView.layer addAnimation:pulse forKey:nil];
}
网友评论