这篇文章给大家分享的是有关iOS自定义View如何实现卡片滑动的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、服务器托管、等保安全、私有云建设等企业级互联网基础服务,来电联系:18980820575
说明
控件基于UIView封装完成,采用UIPanGestureRecognizer监听自身的触摸事件,以此处理各种滑动动画操作。
内容之间可以循环切换,采用类似tableView加载机制,达到复用效果
效果

代码实现
#import@class SMSwipeView; @protocol SMSwipeDelegate @required //获取显示数据内容 -(UITableViewCell*)SMSwipeGetView:(SMSwipeView*)swipe withIndex:(int)index; //获取数据源总量 -(NSInteger)SMSwipeGetTotaleNum:(SMSwipeView*)swipe; @end @interface SMSwipeView : UIView @property(nonatomic,weak)id delegate; //层叠透明方式显示 默认NO @property(nonatomic,assign)BOOL isStackCard; //加载方法 -(void)reloadData; //根据id获取缓存的cell -(UITableViewCell*)dequeueReusableUIViewWithIdentifier:(NSString*)identifier; @end
#import "SMSwipeView.h"
#define degreeTOradians(x) (M_PI * (x)/180)
//childView距离父View左右的距离
const int LEFT_RIGHT_MARGIN=10;
//当前view距离父view的顶部的值
const int TOP_MARGTIN=16;
@interface SMSwipeView()
//已经划动到边界外的一个view
@property(nonatomic,weak)UITableViewCell * viewRemove;
//放当前显示的子View的数组
@property(nonatomic,strong)NSMutableArray * cacheViews;
//view总共的数量
@property(nonatomic,assign)int totalNum;
//当前的下标
@property(nonatomic,assign)int nowIndex;
//触摸开始的坐标
@property(nonatomic,assign)CGPoint pointStart;
//上一次触摸的坐标
@property(nonatomic,assign)CGPoint pointLast;
//最后一次触摸的坐标
@property(nonatomic,assign)CGPoint pointEnd;
//正在显示的cell
@property(nonatomic,weak)UITableViewCell * nowCell;
//下一个cell
@property(nonatomic,weak)UITableViewCell * nextCell;
//第三个cell
@property(nonatomic,weak)UITableViewCell * thirdCell;
//自身的宽度
@property(nonatomic,assign)int w;
//自身的高度
@property(nonatomic,assign)int h;
//是否是第一次执行
@property(nonatomic,assign)BOOL isFirstLayoutSub;
@end
@implementation SMSwipeView
//从xib中加载该类
-(void)awakeFromNib{
[super awakeFromNib];
[self initSelf];
}
//直接用方法初始化
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
[self initSelf];
return self;
}
//进行一些自身的初始化和设置
-(void)initSelf{
self.clipsToBounds=YES;
self.cacheViews=[[NSMutableArray alloc]init];
//手势识别
UIPanGestureRecognizer * pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[self addGestureRecognizer:pan];
}
//布局subview的方法
-(void)layoutSubviews{
if(!self.isFirstLayoutSub){
self.isFirstLayoutSub=YES;
self.w=self.bounds.size.width;
self.h=self.bounds.size.height;
[self reloadData];
}
}
//重新加载数据方法,会再首次执行layoutSubviews的时候调用
-(void)reloadData{
if (!self.delegate||![self.delegate respondsToSelector:@selector(SMSwipeGetView:withIndex:)]||![self.delegate respondsToSelector:@selector(SMSwipeGetTotaleNum:)]) {
return;
}
self.totalNum=(int)[self.delegate SMSwipeGetTotaleNum:self];
self.viewRemove=nil;
UITableViewCell * nowCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex];
UITableViewCell * nextCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex+1感谢各位的阅读!关于“iOS自定义View如何实现卡片滑动”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
新闻标题:iOS自定义View如何实现卡片滑动
分享路径:http://cdysf.com/article/iecgie.html