效果圖.gif
簡(jiǎn)介
- 控件基于
UIView
封裝完成昌屉,采用UIPanGestureRecognizer
監(jiān)聽自身的觸摸事件擂煞,以此處理各種滑動(dòng)動(dòng)畫操作贸宏。 - 內(nèi)容之間可以循環(huán)切換,采用類似
tableView
加載機(jī)制家妆,達(dá)到復(fù)用效果。
思路設(shè)計(jì)
整體設(shè)計(jì)
- 繼承
UIView
創(chuàng)建一個(gè)容器SMSwipeView
冕茅,用來(lái)放當(dāng)前顯示的cell
伤极。 - 在
SMSwipeView
中實(shí)現(xiàn)滑動(dòng)事件的監(jiān)聽,以此來(lái)處理相應(yīng)的動(dòng)畫效果姨伤,以及cell
的移除哨坪、緩存、載入操作姜挺。 - 創(chuàng)建一個(gè)
Delegate
齿税,用來(lái)獲取需要顯示的數(shù)據(jù)量彼硫,以及獲取顯示cell
的對(duì)象炊豪。
循環(huán)設(shè)計(jì)
當(dāng)前下標(biāo)index
假如為0
- 獲取當(dāng)前的三個(gè)要顯示的
cell
凌箕,便是0、1词渤、2
三個(gè)cell
牵舱,疊加顯示在當(dāng)前容器之中。 - 滑動(dòng)到下一個(gè)界面情況:
index++
變?yōu)?code>1缺虐,那么index=0
的cell
則顯示在左側(cè)不可見的區(qū)域芜壁,加載index=3
的cell
,如果此時(shí)超出范圍則加載index=0
的cell
高氮。 - 手勢(shì)需要返回到上一頁(yè)的時(shí)候慧妄,就移除疊加最底層的
cell
,并載入上上一個(gè)cell
剪芍,如果當(dāng)前的index=0
塞淹,則上一個(gè)cell
應(yīng)該為最后一個(gè)。
圖片.png
代碼實(shí)現(xiàn)
SMSwipeView.h
//定義協(xié)議
@protocol SMSwipeDelegate <NSObject>
@required
//獲取顯示數(shù)據(jù)內(nèi)容
-(UITableViewCell*)SMSwipeGetView:(SMSwipeView*)swipe withIndex:(int)index;
//獲取數(shù)據(jù)源總量
-(NSInteger)SMSwipeGetTotaleNum:(SMSwipeView*)swipe;
@end
@interface SMSwipeView : BaseView
@property(nonatomic,weak)id<SMSwipeDelegate> delegate;
-(void)reloadData;//加載方法
-(UITableViewCell*)dequeueReusableUIViewWithIdentifier:(NSString*)identifier;//根據(jù)id獲取緩存的cell
@end
SMSwipeView.m
//
// SMSwipeView.m
// Base
//
// Created by whbt_mac on 15/12/28.
// Copyright ? 2015年 StoneMover. All rights reserved.
//
// 淘寶滑動(dòng)特效效果
#import "SMSwipeView.h"
#define degreeTOradians(x) (M_PI * (x)/180)
const int LEFT_RIGHT_MARGIN=10;//childView距離父View左右的距離
const int TOP_MARGTIN=16;//當(dāng)前view距離父view的頂部的值
@interface SMSwipeView()
@property(nonatomic,weak)UITableViewCell * viewRemove;//已經(jīng)劃動(dòng)到邊界外的一個(gè)view
@property(nonatomic,strong)NSMutableArray * cacheViews;//放當(dāng)前顯示的子View的數(shù)組
@property(nonatomic,assign)int totalNum;//view總共的數(shù)量
@property(nonatomic,assign)int nowIndex;//當(dāng)前的下標(biāo)
@property(nonatomic,assign)CGPoint pointStart;//觸摸開始的坐標(biāo)
@property(nonatomic,assign)CGPoint pointLast;//上一次觸摸的坐標(biāo)
@property(nonatomic,assign)CGPoint pointEnd;//最后一次觸摸的坐標(biāo)
@property(nonatomic,weak)UITableViewCell * nowCell;//正在顯示的cell
@property(nonatomic,weak)UITableViewCell * nextCell;//下一個(gè)cell
@property(nonatomic,weak)UITableViewCell * thirdCell;//第三個(gè)cell
@property(nonatomic,assign)int w;//自身的寬度
@property(nonatomic,assign)int h;//自身的高度
@property(nonatomic,assign)BOOL isFirstLayoutSub;//是否是第一次執(zhí)行
@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ù)方法罪裹,會(huì)再首次執(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<self.totalNum?self.nowIndex+1:0];
UITableViewCell * thirdCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex+2<self.totalNum?self.nowIndex+2:self.nowIndex+2-self.totalNum];
[thirdCell removeFromSuperview];
thirdCell.layer.anchorPoint=CGPointMake(1, 1);
thirdCell.frame=CGRectMake(LEFT_RIGHT_MARGIN*2, 0, self.w-2*2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
[self addSubview:thirdCell];
self.thirdCell=thirdCell;
[nextCell removeFromSuperview];
nextCell.layer.anchorPoint=CGPointMake(1, 1);
nextCell.frame=CGRectMake(LEFT_RIGHT_MARGIN, TOP_MARGTIN/2*1, self.w-2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
[self addSubview:nextCell];
self.nextCell=nextCell;
[nowCell removeFromSuperview];
nowCell.layer.anchorPoint=CGPointMake(1, 1);
nowCell.frame=CGRectMake(0, TOP_MARGTIN, self.w, self.h-TOP_MARGTIN);
[self addSubview:nowCell];
self.nowCell=nowCell;
}
#pragma mark swipe觸摸的相關(guān)手勢(shì)處理
-(void)swipe:(UISwipeGestureRecognizer*)sender{
NSLog(@"swipe");
}
-(void)pan:(UIPanGestureRecognizer*)sender{
CGPoint translation = [sender translationInView: self];
//CGPoint speed=[sender velocityInView:self];//獲取速度
if (sender.state==UIGestureRecognizerStateBegan) {
//NSLog(@"begin");
self.pointStart=translation;
self.pointLast=translation;
}
if (sender.state==UIGestureRecognizerStateChanged) {
//NSLog(@"change");
// CGFloat xMove=translation.x-self.pointLast.x;
// CGFloat yMove=translation.y-self.pointLast.y;
// self.pointLast=translation;
//
// CGPoint center=self.nowCell.center;
// self.nowCell.center=CGPointMake(center.x+xMove, center.y+yMove);
CGFloat xTotalMove=translation.x-self.pointStart.x;
if (xTotalMove<0) {
self.nowCell.transform = CGAffineTransformMakeRotation(degreeTOradians(90*xTotalMove/self.w));
self.nextCell.transform= CGAffineTransformMakeRotation(degreeTOradians(90*xTotalMove/self.w/2));
}else{
self.nowCell.transform = CGAffineTransformMakeRotation(degreeTOradians(0));
self.nextCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
}
}
if (sender.state==UIGestureRecognizerStateEnded) {
//NSLog(@"end");
CGFloat xTotalMove=translation.x-self.pointStart.x;
if (xTotalMove<0) {
[self swipeEnd];
}else{
[self swipeGoBack];
}
}
// NSLog(@"%@%f%@%f",@"x:",speed.x,@"y:",speed.y);
//NSLog(@"%@%f%@%f",@"x:",translation.x,@"y:",translation.y);
}
/**
* @author StoneMover, 16-12-29 14:12:33
*
* @brief 獲取為顯示的cell,復(fù)用機(jī)制
*
* @param identifier id標(biāo)志
*
* @return 返回的cell,如果緩存中沒有則返回空
*/
-(UITableViewCell*)dequeueReusableUIViewWithIdentifier:(NSString *)identifier{
for (UITableViewCell * cell in self.cacheViews) {
if ([identifier isEqualToString:cell.reuseIdentifier]) {
[self.cacheViews removeObject:cell];
return cell;
}
}
return nil;
}
//滑動(dòng)到下一個(gè)界面
-(void)swipeEnd{
[UIView animateWithDuration:0.3 animations:^{
self.nextCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
}];
//self.nowCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
CGPoint center=self.nowCell.center;
[UIView animateWithDuration:0.3 animations:^{
self.nowCell.center=CGPointMake(center.x-self.w, center.y);
self.nowCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
// [self.nowCell setAlpha:0.0];
} completion:^(BOOL finished) {
self.nowIndex++;
self.nowIndex=self.nowIndex<self.totalNum?self.nowIndex:0;
if (self.viewRemove&&[self isNeedAddToCache:self.viewRemove]) {
[self.cacheViews addObject:self.viewRemove];
[self.viewRemove removeFromSuperview];
}
self.viewRemove=self.nowCell;
//self.viewRemove.layer.anchorPoint=CGPointMake(0, 0);
//self.viewRemove.transform=CGAffineTransformMakeRotation(degreeTOradians(-35));
self.nowCell=self.nextCell;
self.nextCell=self.thirdCell;
UITableViewCell * thirdCell=[self.delegate SMSwipeGetView:self withIndex:self.nowIndex+2<self.totalNum?(int)self.nowIndex+2:(int)self.nowIndex+2-(int)self.totalNum];
[thirdCell removeFromSuperview];
thirdCell.layer.anchorPoint=CGPointMake(1, 1);
thirdCell.frame=CGRectMake(LEFT_RIGHT_MARGIN*2, 0, self.w-2*2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
self.thirdCell=thirdCell;
[self insertSubview:thirdCell belowSubview:self.nextCell];
[UIView animateWithDuration:0.2 animations:^{
self.nowCell.frame=CGRectMake(0, TOP_MARGTIN, self.w, self.h-TOP_MARGTIN);
self.nextCell.frame=CGRectMake(LEFT_RIGHT_MARGIN, TOP_MARGTIN/2*1, self.w-2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
}];
}];
}
//滑動(dòng)到上一個(gè)界面
-(void)swipeGoBack{
if (!self.viewRemove) {
NSLog(@"!viewRemove");
return;
}
if (self.nowIndex==0) {
NSLog(@"!viewRemove+index");
return;
}
CGPoint center=self.viewRemove.center;
self.nowIndex--;
// if ([self isNeedAddToCache:self.thirdCell]) {
// [self.cacheViews addObject:self.thirdCell];
// }
[self.thirdCell removeFromSuperview];
self.thirdCell=self.nextCell;
self.nextCell=self.nowCell;
self.nowCell=self.viewRemove;
if (self.nowIndex==0) {
self.viewRemove=nil;
}else{
UITableViewCell * cell=[self.delegate SMSwipeGetView:self withIndex:(int)self.nowIndex-1];
[cell removeFromSuperview];
[self insertSubview:cell aboveSubview:self.nowCell];
cell.layer.anchorPoint=CGPointMake(1, 1);
cell.frame=self.viewRemove.frame;
self.viewRemove=cell;
}
[UIView animateWithDuration:.5 animations:^{
self.nowCell.center=CGPointMake(center.x+self.w, center.y);
self.nowCell.transform= CGAffineTransformMakeRotation(degreeTOradians(0));
self.nextCell.frame=CGRectMake(LEFT_RIGHT_MARGIN, TOP_MARGTIN/2*1, self.w-2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
self.thirdCell.frame=CGRectMake(LEFT_RIGHT_MARGIN*2, 0, self.w-2*2*LEFT_RIGHT_MARGIN, self.h-TOP_MARGTIN);
}];
}
//是否需要加入到緩存中去
-(BOOL)isNeedAddToCache:(UITableViewCell*)cell{
for (UITableViewCell * cellIn in self.cacheViews) {
if ([cellIn.reuseIdentifier isEqualToString:cell.reuseIdentifier]) {
return NO;
}
}
return YES;
}
@end