十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章給大家分享的是有關(guān)iOS自定義View如何實(shí)現(xiàn)卡片滑動的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)-云計(jì)算及IDC服務(wù)提供商,涵蓋公有云、IDC機(jī)房租用、服務(wù)器托管、等保安全、私有云建設(shè)等企業(yè)級互聯(lián)網(wǎng)基礎(chǔ)服務(wù),來電聯(lián)系:18980820575
說明
控件基于UIView封裝完成,采用UIPanGestureRecognizer監(jiān)聽自身的觸摸事件,以此處理各種滑動動畫操作。
內(nèi)容之間可以循環(huán)切換,采用類似tableView加載機(jī)制,達(dá)到復(fù)用效果
效果
代碼實(shí)現(xiàn)
#import@class SMSwipeView; @protocol SMSwipeDelegate @required //獲取顯示數(shù)據(jù)內(nèi)容 -(UITableViewCell*)SMSwipeGetView:(SMSwipeView*)swipe withIndex:(int)index; //獲取數(shù)據(jù)源總量 -(NSInteger)SMSwipeGetTotaleNum:(SMSwipeView*)swipe; @end @interface SMSwipeView : UIView @property(nonatomic,weak)id delegate; //層疊透明方式顯示 默認(rèn)NO @property(nonatomic,assign)BOOL isStackCard; //加載方法 -(void)reloadData; //根據(jù)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; //當(dāng)前view距離父view的頂部的值 const int TOP_MARGTIN=16; @interface SMSwipeView() //已經(jīng)劃動到邊界外的一個(gè)view @property(nonatomic,weak)UITableViewCell * viewRemove; //放當(dāng)前顯示的子View的數(shù)組 @property(nonatomic,strong)NSMutableArray * cacheViews; //view總共的數(shù)量 @property(nonatomic,assign)int totalNum; //當(dāng)前的下標(biāo) @property(nonatomic,assign)int nowIndex; //觸摸開始的坐標(biāo) @property(nonatomic,assign)CGPoint pointStart; //上一次觸摸的坐標(biāo) @property(nonatomic,assign)CGPoint pointLast; //最后一次觸摸的坐標(biāo) @property(nonatomic,assign)CGPoint pointEnd; //正在顯示的cell @property(nonatomic,weak)UITableViewCell * nowCell; //下一個(gè)cell @property(nonatomic,weak)UITableViewCell * nextCell; //第三個(gè)cell @property(nonatomic,weak)UITableViewCell * thirdCell; //自身的寬度 @property(nonatomic,assign)int w; //自身的高度 @property(nonatomic,assign)int h; //是否是第一次執(zhí)行 @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; } //進(jìn)行一些自身的初始化和設(shè)置 -(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]; } } //重新加載數(shù)據(jù)方法,會再首次執(zhí)行l(wèi)ayoutSubviews的時(shí)候調(diào)用 -(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感謝各位的閱讀!關(guān)于“iOS自定義View如何實(shí)現(xiàn)卡片滑動”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
網(wǎng)站名稱:iOS自定義View如何實(shí)現(xiàn)卡片滑動
文章鏈接:http://m.jiaotiyi.com/article/iecgie.html