效果图
主要思路
容器View类:cardView
item卡片类:cardItemView
cardView
@protocol cardViewDelegate <NSObject>
//点击响应
-(void)cardView:(cardView *)cardView viewclickedWithIndex:(NSInteger)index;
//左右滑动删除
-(void)cardView:(cardView *)cardView viewdeletWithisLeft:(BOOL)isleft WithIndex:(NSInteger)index;
@end
@protocol cardViewDataSource <NSObject>
@required
//卡片数
- (NSInteger)numberOfCardView:(cardView *)cardView;
//item卡片创建
- (cardItemView *)cardView:(cardView *)cardView itemViewAtIndex:(NSInteger)index;
//重新刷新卡片
- (void)cardViewNeedMoreData:(cardView *)cardView;
@end
创建卡片主要代码
if (_dataSource == nil) {
return ;
}
//1.移除
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
//2.添加
itemCount = [self numberOfDataSource];
for (int i=0; i<itemCount; i++) {
cardItemView * itemView = [self itemViewAtIndex:i];
itemView.delegate =self;
itemView.tag =i;
itemView.bounds =CGRectZero;
int x = arc4random() % 10;
int y = arc4random() % 10;
[UIView animateWithDuration:0.2 delay:i*0.05 options:UIViewAnimationOptionCurveEaseInOut animations:^{
itemView.frame =CGRectMake(x,y, self.bounds.size.width, self.bounds.size.height);
} completion:^(BOOL finished) {
}];
[self addSubview:itemView];
}
cardItemView 拖动手势主要代码
if (pan.state == UIGestureRecognizerStateChanged) {
CGPoint translate = [pan translationInView:self];
if (translate.x<-1) {
isleft = YES;
}
self.center =CGPointMake(self.center.x+translate.x, self.center.y+translate.y);
CGFloat angle = (self.center.x - self.frame.size.width / 2.0) / self.frame.size.width / 4.0;
self.transform = CGAffineTransformMakeRotation(angle);
[pan setTranslation:CGPointZero inView:self];
}else if (pan.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [pan velocityInView:self];
if (velocity.x>800 ||velocity.x<-800) {
[self removeWithLeft:isleft];
return;
}
if (self.center.x<10||self.center.x>Width-10) {
[self removeWithLeft:isleft];
return;
}else{
[UIView animateWithDuration:0.5 animations:^{
self.center = _originalCenter;
self.transform = CGAffineTransformMakeRotation(0);
}];
}
}
本文参照Charles,仅供学习使用