iOS每日一记之———————————————模仿今日头条栏目选择效果 并且附加cell颤抖效果
2017-06-16 21:19
351 查看
嗯 基本上要做的功能就是克雷可深View 的长按 拖动 交换数据 颤抖功能 至于克雷可深View的布局问题就不用我说了吧 都是老生常谈了 嗯 闲话少说 下面上重点代码
//给你的克雷可深View添加一个长安手势和pan手势
self.gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pangestureOperation:)];
self.longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onLongPressed:)];
self.longGesture.minimumPressDuration = 1;
[self.amphotericCollection addGestureRecognizer:self.longGesture];
//长按的点击方法
CGPoint point = [sender locationInView:sender.view];
NSIndexPath *indexPath = [self.amphotericCollection indexPathForItemAtPoint:point];
// 只允许第一区可移动
if (indexPath.section != 0) {
return;
}
if (indexPath.section == 0 && indexPath.item == 0) {
return;
}
self.isEditing = YES;
_isBegin= YES;//开始抖动
[self.amphotericCollection reloadData];
//添加pan手势 用来拖动
[self.amphotericCollection addGestureRecognizer:self.gesture];
/拖拽cellItem
- (void)pangestureOperation:(UIPanGestureRecognizer *)sender{
if (!self.isEditing) {
return;
}
CGPoint point = [sender locationInView:sender.view];
NSIndexPath *indexPath = [self.amphotericCollection indexPathForItemAtPoint:point];
// 只允许第一区可移动
if (indexPath.section != 0) {
return;
}
switch (sender.state) {
case UIGestureRecognizerStateBegan: {
if (indexPath) {
[self.amphotericCollection beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
NSIndexPath* indexPath = [self.amphotericCollection indexPathForItemAtPoint:[sender locationInView:self.amphotericCollection]];
if(indexPath.item <1){
break;//第一个不可移动 个人限制
}
[self.amphotericCollection updateInteractiveMovementTargetPosition:point];
break;
}
case UIGestureRecognizerStateEnded: {
[self.amphotericCollection endInteractiveMovement];
break;
}
default: {
[self.amphotericCollection cancelInteractiveMovement];
break;
}
}
}
//iOS9之后 提供了系统的move方法
//数据源交换方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) {
[listTop exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
}
}
//之后是克雷可深View的点击方法
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (indexPath.item == 0) {
return;
}
if (self.isEditing) {
InlineModel *model = [listTop objectAtIndex:indexPath.item];
//listTop 是上面区的那个数组 listBottom是下面的那个区的数组
[listTop removeObject:model];
[listBottom addObject:model];
NSInteger index = listBottom.count - 1;
if (listBottom.count == 0) {
index = 0;
}
XYSAmphotericCollectionCell *cell = (XYSAmphotericCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.deleteBtn.hidden = YES;
[self.amphotericCollection moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:1]];
} else {
// 选择某一个
return;
}
} else {
InlineModel *model = [listBottom objectAtIndex:indexPath.item];
[listBottom removeObject:model];
[listTop addObject:model];
NSInteger index = listTop.count - 1;
if (listTop.count == 0) {
index = 0;
}
if (self.isEditing) {
XYSAmphotericCollectionCell *cell = (XYSAmphotericCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.deleteBtn.hidden = NO;
}
[collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
}
//这里面并没有reloadData方法 是因为 moveItemAtIndexPath这个 用了这个之后 克雷可深View 不会刷新数据 去掉这个之后就可以刷新了 这也是我所困扰的一个问题
所以这里采用刷新区的方法 但是刷新区会有白色的闪光 下面的方法是用来去除闪光效果的
[self performSelector:@selector(updateData) withObject:self afterDelay:0.3];
}
//用来去除闪光效果的动画
- (void)updateData{
[UIView animateWithDuration:0 animations:^{
[self.amphotericCollection performBatchUpdates:^{
[self.amphotericCollection reloadSections:[NSIndexSet indexSetWithIndex:0]];
[self.amphotericCollection reloadSections:[NSIndexSet indexSetWithIndex:1]];
} completion: nil];
}];
}
//添加抖动效果
- (void)starLongPress:(XYSAmphotericCollectionCell *)cell{
CABasicAnimation *animation = (CABasicAnimation *)[cell.layer animationForKey:@"rotation"];
if (animation == nil) {
[self shakeImage:cell];
}else {
[self resume:cell];
}
}
- (void)shakeImage:(XYSAmphotericCollectionCell*)cell {
//创建动画对象,绕Z轴旋转
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//设置属性,周期时长
[animation setDuration:0.08];
//抖动角度
animation.fromValue = @(-M_1_PI/2);
animation.toValue = @(M_1_PI/2);
//重复次数,无限大
animation.repeatCount = HUGE_VAL;
//恢复原样
animation.autoreverses = YES;
//锚点设置为图片中心,绕中心抖动
cell.layer.anchorPoint = CGPointMake(0.5, 0.5);
[cell.layer addAnimation:animation forKey:@"rotation"];
}
- (void)pause:(XYSAmphotericCollectionCell *)cell {
cell.layer.speed = 0.0;
}
- (void)resume:(XYSAmphotericCollectionCell *)cell {
cell.layer.speed = 1.0;
}
嗯这样就实现了 是不是很简单呢 至于克雷可深View为什么不能刷新的问题 我也会继续探讨下去的 。。。。。。。。。。
//给你的克雷可深View添加一个长安手势和pan手势
self.gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pangestureOperation:)];
self.longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onLongPressed:)];
self.longGesture.minimumPressDuration = 1;
[self.amphotericCollection addGestureRecognizer:self.longGesture];
//长按的点击方法
CGPoint point = [sender locationInView:sender.view];
NSIndexPath *indexPath = [self.amphotericCollection indexPathForItemAtPoint:point];
// 只允许第一区可移动
if (indexPath.section != 0) {
return;
}
if (indexPath.section == 0 && indexPath.item == 0) {
return;
}
self.isEditing = YES;
_isBegin= YES;//开始抖动
[self.amphotericCollection reloadData];
//添加pan手势 用来拖动
[self.amphotericCollection addGestureRecognizer:self.gesture];
/拖拽cellItem
- (void)pangestureOperation:(UIPanGestureRecognizer *)sender{
if (!self.isEditing) {
return;
}
CGPoint point = [sender locationInView:sender.view];
NSIndexPath *indexPath = [self.amphotericCollection indexPathForItemAtPoint:point];
// 只允许第一区可移动
if (indexPath.section != 0) {
return;
}
switch (sender.state) {
case UIGestureRecognizerStateBegan: {
if (indexPath) {
[self.amphotericCollection beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
NSIndexPath* indexPath = [self.amphotericCollection indexPathForItemAtPoint:[sender locationInView:self.amphotericCollection]];
if(indexPath.item <1){
break;//第一个不可移动 个人限制
}
[self.amphotericCollection updateInteractiveMovementTargetPosition:point];
break;
}
case UIGestureRecognizerStateEnded: {
[self.amphotericCollection endInteractiveMovement];
break;
}
default: {
[self.amphotericCollection cancelInteractiveMovement];
break;
}
}
}
//iOS9之后 提供了系统的move方法
//数据源交换方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
if (sourceIndexPath.section == 0 && destinationIndexPath.section == 0) {
[listTop exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
}
}
//之后是克雷可深View的点击方法
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if (indexPath.item == 0) {
return;
}
if (self.isEditing) {
InlineModel *model = [listTop objectAtIndex:indexPath.item];
//listTop 是上面区的那个数组 listBottom是下面的那个区的数组
[listTop removeObject:model];
[listBottom addObject:model];
NSInteger index = listBottom.count - 1;
if (listBottom.count == 0) {
index = 0;
}
XYSAmphotericCollectionCell *cell = (XYSAmphotericCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.deleteBtn.hidden = YES;
[self.amphotericCollection moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:1]];
} else {
// 选择某一个
return;
}
} else {
InlineModel *model = [listBottom objectAtIndex:indexPath.item];
[listBottom removeObject:model];
[listTop addObject:model];
NSInteger index = listTop.count - 1;
if (listTop.count == 0) {
index = 0;
}
if (self.isEditing) {
XYSAmphotericCollectionCell *cell = (XYSAmphotericCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
cell.deleteBtn.hidden = NO;
}
[collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
}
//这里面并没有reloadData方法 是因为 moveItemAtIndexPath这个 用了这个之后 克雷可深View 不会刷新数据 去掉这个之后就可以刷新了 这也是我所困扰的一个问题
所以这里采用刷新区的方法 但是刷新区会有白色的闪光 下面的方法是用来去除闪光效果的
[self performSelector:@selector(updateData) withObject:self afterDelay:0.3];
}
//用来去除闪光效果的动画
- (void)updateData{
[UIView animateWithDuration:0 animations:^{
[self.amphotericCollection performBatchUpdates:^{
[self.amphotericCollection reloadSections:[NSIndexSet indexSetWithIndex:0]];
[self.amphotericCollection reloadSections:[NSIndexSet indexSetWithIndex:1]];
} completion: nil];
}];
}
//添加抖动效果
- (void)starLongPress:(XYSAmphotericCollectionCell *)cell{
CABasicAnimation *animation = (CABasicAnimation *)[cell.layer animationForKey:@"rotation"];
if (animation == nil) {
[self shakeImage:cell];
}else {
[self resume:cell];
}
}
- (void)shakeImage:(XYSAmphotericCollectionCell*)cell {
//创建动画对象,绕Z轴旋转
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//设置属性,周期时长
[animation setDuration:0.08];
//抖动角度
animation.fromValue = @(-M_1_PI/2);
animation.toValue = @(M_1_PI/2);
//重复次数,无限大
animation.repeatCount = HUGE_VAL;
//恢复原样
animation.autoreverses = YES;
//锚点设置为图片中心,绕中心抖动
cell.layer.anchorPoint = CGPointMake(0.5, 0.5);
[cell.layer addAnimation:animation forKey:@"rotation"];
}
- (void)pause:(XYSAmphotericCollectionCell *)cell {
cell.layer.speed = 0.0;
}
- (void)resume:(XYSAmphotericCollectionCell *)cell {
cell.layer.speed = 1.0;
}
嗯这样就实现了 是不是很简单呢 至于克雷可深View为什么不能刷新的问题 我也会继续探讨下去的 。。。。。。。。。。
相关文章推荐
- iOS每日一记之———————————————为Cell设置部分圆角 类似于ipad的设置界面效果
- iOS tableview cell 去除点击效果 和 选择效果
- IOS UIScrollView 翻页,并且露出左边视图和右边视图的边缘效果。
- iOS之取消点击cell的效果
- iOS软件开发 设置cell动画效果
- iOS小技巧-UITableView取消或设置cell的选中效果
- iOS每日一记之——————点击按钮进入到国家选择列表并回调
- cocos2d-x 模拟IOS时间选择器效果
- IOS-通过自定义iCarousel来高仿土巴兔选择装修风格效果(中间选中项放大)
- iOS开源项目 Cell滑动效果
- iOS集合视图UICollectionViewCell的选种(高亮)效果
- iOS学习笔记 自定义cell并且文本自适应高度
- cocos2d-x 模拟IOS时间选择器效果
- IOS 取消表格单元格 TableViewCell 去掉高亮状态 点击Cell取消选择状态
- IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果
- AndroidCityPicker仿IOS选择效果
- iOS 粒子效果 CAEmitterCell CAEmitterLayer
- IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果
- iOS 选择tableViewCell中的控件确定cell位置
- IOS-通过自定义iCarousel来高仿土巴兔选择装修风格效果(中间选中项放大)