iOS自定义View实现卡片滑动
本文实例为大家分享了iOS自定义View实现卡片滑动效果的具体代码,供大家参考,具体内容如下
说明
控件基于UIView封装完成,采用UIPanGestureRecognizer监听自身的触摸事件,以此处理各种滑动动画操作。
内容之间可以循环切换,采用类似tableView加载机制,达到复用效果
效果
代码实现
#import@classSMSwipeView; @protocolSMSwipeDelegate @required //获取显示数据内容 -(UITableViewCell*)SMSwipeGetView:(SMSwipeView*)swipewithIndex:(int)index; //获取数据源总量 -(NSInteger)SMSwipeGetTotaleNum:(SMSwipeView*)swipe; @end @interfaceSMSwipeView:UIView @property(nonatomic,weak)id delegate; //层叠透明方式显示默认NO @property(nonatomic,assign)BOOLisStackCard; //加载方法 -(void)reloadData; //根据id获取缓存的cell -(UITableViewCell*)dequeueReusableUIViewWithIdentifier:(NSString*)identifier; @end
#import"SMSwipeView.h"
#definedegreeTOradians(x)(M_PI*(x)/180)
//childView距离父View左右的距离
constintLEFT_RIGHT_MARGIN=10;
//当前view距离父view的顶部的值
constintTOP_MARGTIN=16;
@interfaceSMSwipeView()
//已经划动到边界外的一个view
@property(nonatomic,weak)UITableViewCell*viewRemove;
//放当前显示的子View的数组
@property(nonatomic,strong)NSMutableArray*cacheViews;
//view总共的数量
@property(nonatomic,assign)inttotalNum;
//当前的下标
@property(nonatomic,assign)intnowIndex;
//触摸开始的坐标
@property(nonatomic,assign)CGPointpointStart;
//上一次触摸的坐标
@property(nonatomic,assign)CGPointpointLast;
//最后一次触摸的坐标
@property(nonatomic,assign)CGPointpointEnd;
//正在显示的cell
@property(nonatomic,weak)UITableViewCell*nowCell;
//下一个cell
@property(nonatomic,weak)UITableViewCell*nextCell;
//第三个cell
@property(nonatomic,weak)UITableViewCell*thirdCell;
//自身的宽度
@property(nonatomic,assign)intw;
//自身的高度
@property(nonatomic,assign)inth;
//是否是第一次执行
@property(nonatomic,assign)BOOLisFirstLayoutSub;
@end
@implementationSMSwipeView
//从xib中加载该类
-(void)awakeFromNib{
[superawakeFromNib];
[selfinitSelf];
}
//直接用方法初始化
-(instancetype)initWithFrame:(CGRect)frame{
self=[superinitWithFrame:frame];
[selfinitSelf];
returnself;
}
//进行一些自身的初始化和设置
-(void)initSelf{
self.clipsToBounds=YES;
self.cacheViews=[[NSMutableArrayalloc]init];
//手势识别
UIPanGestureRecognizer*pan=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];
[selfaddGestureRecognizer:pan];
}
//布局subview的方法
-(void)layoutSubviews{
if(!self.isFirstLayoutSub){
self.isFirstLayoutSub=YES;
self.w=self.bounds.size.width;
self.h=self.bounds.size.height;
[selfreloadData];
}
}
//重新加载数据方法,会再首次执行layoutSubviews的时候调用
-(void)reloadData{
if(!self.delegate||![self.delegaterespondsToSelector:@selector(SMSwipeGetView:withIndex:)]||![self.delegaterespondsToSelector:@selector(SMSwipeGetTotaleNum:)]){
return;
}
self.totalNum=(int)[self.delegateSMSwipeGetTotaleNum:self];
self.viewRemove=nil;
UITableViewCell*nowCell=[self.delegateSMSwipeGetView:selfwithIndex:self.nowIndex];
UITableViewCell*nextCell=[self.delegateSMSwipeGetView:selfwithIndex:self.nowIndex+1
源码下载点击查看
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。