当前位置:   article > 正文

高仿网易酷狗侧滑与QQ新界面侧滑_android 模仿酷狗(新版)滑动侧滑栏的效果

android 模仿酷狗(新版)滑动侧滑栏的效果


高仿网易 4.0 UI 框架的 Demo

功能描述:

1.   右视图滑动导航菜单的字体大小和颜色,随scrollView滑动的变化而变化的效果.

2.   新的抽屉效果,修改于SliderViewController,仿网易新闻的界面.

3.   各种侧滑 跳转

效果图:



创建三个类

QHViewController, LeftViewController,MainViewController

Appdelegate的设置如下

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  4. // Override point for customization after application launch.
  5. self.window.backgroundColor = [UIColor whiteColor];
  6. [self.window makeKeyAndVisible];
  7. LeftViewController * LeftVC=[[LeftViewController alloc]init];
  8. [LeftVC.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
  9. [QHViewController shareSlideController].LeftVC=LeftVC;
  10. MainViewController * MainVC=[[MainViewController alloc]init];
  11. [MainVC.view setFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
  12. [QHViewController shareSlideController].MainVC=MainVC;
  13. UINavigationController * nvc=[[UINavigationController alloc]initWithRootViewController:[QHViewController shareSlideController]];
  14. self.window.rootViewController=nvc;
  15. return YES;
  16. }
QHViewController中实现主视图与左视图的滑动显示的代码
  1. //
  2. // QHViewController.m
  3. // 侧滑--仿酷狗
  4. //
  5. // Created by mac1 on 14-9-19.
  6. // Copyright (c) 2014年 mac1. All rights reserved.
  7. //
  8. #import "QHViewController.h"
  9. @interface QHViewController ()
  10. {
  11. UIView * _mainContentView;
  12. UIView * _leftSideView;
  13. UISwipeGestureRecognizer * swipeGesture;
  14. }
  15. @end
  16. @implementation QHViewController
  17. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  18. {
  19. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  20. if (self) {
  21. // Custom initialization
  22. }
  23. return self;
  24. }
  25. +(QHViewController *)shareSlideController
  26. {
  27. static QHViewController * shareVC;
  28. static dispatch_once_t oneToke;
  29. dispatch_once(&oneToke ,^{
  30. shareVC=[[self alloc]init];
  31. });
  32. return shareVC;
  33. }
  34. - (void)viewWillAppear:(BOOL)animated
  35. {
  36. /* { hide status bar } */
  37. //隐藏导航栏
  38. [[self navigationController] setNavigationBarHidden:YES];
  39. [super viewWillAppear:animated];
  40. }
  41. - (void)viewDidLoad
  42. {
  43. [super viewDidLoad];
  44. self.navigationController.navigationBarHidden=YES;
  45. //创建视图
  46. [self initSubviews];
  47. }
  48. -(void)initSubviews
  49. {
  50. //创建主视图
  51. _mainContentView = [[UIView alloc] initWithFrame:self.view.bounds];
  52. [self.view addSubview:_mainContentView];
  53. [_mainContentView addSubview:_MainVC.view];
  54. // //创建左视图
  55. _leftSideView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.5, 0, self.view.frame.size.width, self.view.frame.size.height)];
  56. [_leftSideView addSubview:_LeftVC.view];
  57. [self.view addSubview:_leftSideView];
  58. }
  59. -(void)showLeftView
  60. {
  61. [UIView beginAnimations:nil context:nil];
  62. [UIView setAnimationDuration:.5];
  63. _leftSideView.frame=CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  64. [UIView commitAnimations];
  65. }
  66. -(void)closeLeftView
  67. {
  68. [UIView beginAnimations:nil context:nil];
  69. [UIView setAnimationDuration:.5];
  70. _leftSideView.frame=CGRectMake(self.view.frame.size.width*0.5, 0, self.view.frame.size.width, self.view.frame.size.height);
  71. [UIView commitAnimations];
  72. }
  73. @end
LeftViewController的设置

属性设置

  1. //导航栏视图
  2. UIImageView * _navView;
  3. //标签栏视图
  4. UIView * _topNaviV;
  5. //滑动视图
  6. UIScrollView * _scrollView;
  7. //下拉视图
  8. UIView * _selectTabV;
  9. //标签栏滑动子视图
  10. UIScrollView * _navScrollV;
  11. //视图的轻扫手势
  12. UISwipeGestureRecognizer * swipeGesture1;

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. //给视图添加轻扫手势,轻扫显示主界面
  5. self.view.backgroundColor=[UIColor cyanColor];
  6. swipeGesture1=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesture1Action)];
  7. swipeGesture1.direction=UISwipeGestureRecognizerDirectionRight;
  8. [self.view addGestureRecognizer:swipeGesture1];
  9. //创建导航栏
  10. _navView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.2)];
  11. [_navView setBackgroundColor:[UIColor redColor]];
  12. [self.view addSubview:_navView];
  13. //导航栏标题
  14. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((_navView.frame.size.width - 200)/2, (_navView.frame.size.height - 40)/2, 200, 40)];
  15. [titleLabel setText:@"新闻列表"];
  16. [titleLabel setTextAlignment:NSTextAlignmentCenter];
  17. [titleLabel setTextColor:[UIColor whiteColor]];
  18. [titleLabel setFont:[UIFont boldSystemFontOfSize:22]];
  19. [titleLabel setBackgroundColor:[UIColor clearColor]];
  20. [_navView addSubview:titleLabel];
  21. //导航栏button,点击可显示主界面
  22. UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake(10, 40, 30, 30)];
  23. [button setTitle:@"+" forState:UIControlStateNormal];
  24. [button setBackgroundColor:[UIColor blueColor]];
  25. button.selected=NO;
  26. [button addTarget:self action:@selector(baButton:) forControlEvents:UIControlEventTouchUpInside];
  27. _navView.userInteractionEnabled=YES;
  28. [_navView addSubview:button];
  29. //创建标签栏_navView.frame.origin.y是_navView的起始坐标,创建标签视图,可滑动
  30. _topNaviV = [[UIView alloc] initWithFrame:CGRectMake(0, _navView.frame.size.height + _navView.frame.origin.y, self.view.frame.size.width, 36)];
  31. _topNaviV.backgroundColor = [UIColor orangeColor];
  32. [self.view addSubview:_topNaviV];
  33. //创建滑动视图
  34. _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0,_topNaviV.frame.origin.y+_topNaviV.frame.size.height, self.view.frame.size.width,self.view.frame.size.height- (_topNaviV.frame.origin.y+_topNaviV.frame.size.height))];
  35. [_scrollView setPagingEnabled:YES];
  36. _scrollView.showsHorizontalScrollIndicator=NO;
  37. [self.view insertSubview:_scrollView belowSubview:
  38. _navView];
  39. _scrollView.delegate=self;
  40. //创建一个平移手势,添加到滑动视图上 可以调用handelPan:方法
  41. [_scrollView.panGestureRecognizer addTarget:self action:@selector(scrollHandlePan:)];
  42. [_scrollView setContentSize:CGSizeMake(self.view.frame.size.width * 10,_scrollView.frame.size.height)];
  43. _scrollView.tag=101;
  44. //创建下拉视图,设置为隐藏
  45. _selectTabV = [[UIView alloc] initWithFrame:CGRectMake(0, _scrollView.frame.origin.y - _scrollView.frame.size.height, _scrollView.frame.size.width, _scrollView.frame.size.height)];
  46. [_selectTabV setBackgroundColor:[UIColor orangeColor]];
  47. [_selectTabV setHidden:YES];
  48. [self.view insertSubview:_selectTabV belowSubview:_navView];
  49. //调用标签视图布局
  50. [self createTwo];
  51. }
  52. -(void)baButton:(UIButton*)button
  53. {
  54. if (button.selected==YES) {
  55. [[QHViewController shareSlideController]closeLeftView];
  56. }
  57. else if(button.selected==NO)
  58. {
  59. [[QHViewController shareSlideController]showLeftView];
  60. }
  61. button.selected=!button.selected;
  62. }
  63. //标签视图的布局
  64. -(void)createTwo
  65. {
  66. float btnW = 30;
  67. //创建下拉视图的button
  68. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  69. [btn setFrame:CGRectMake(_topNaviV.frame.size.width - btnW, 0, btnW, 36)];
  70. [btn setBackgroundColor:[UIColor grayColor]];
  71. [btn setTitle:@"+" forState:UIControlStateNormal];
  72. //button添加在滑动标签视图上
  73. [_topNaviV addSubview:btn];
  74. [btn addTarget:self action:@selector(showSelectView:) forControlEvents:UIControlEventTouchUpInside];
  75. NSArray *arT = @[@"推荐", @"彩票", @"热点", @"社会", @"娱乐", @"财经", @"军事", @"科技", @"国际", @"体育"];
  76. //创建滑动标签视图
  77. _navScrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - btnW, 36)];
  78. [_navScrollV setShowsHorizontalScrollIndicator:NO];
  79. _navScrollV.delegate=self;
  80. _navScrollV.tag=100;
  81. for (int i = 0; i < [arT count]; i++)
  82. {
  83. //创建标签试图上的每一个button
  84. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  85. [btn setFrame:CGRectMake(60 * i, 0, 60,36)];
  86. [btn setTitle:[arT objectAtIndex:i] forState:UIControlStateNormal];
  87. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  88. btn.tag = i + 1;
  89. if(i==0)
  90. {
  91. //第一个button颜色字体的设置
  92. [self changeColorForButton:btn red:1];
  93. btn.titleLabel.font = [UIFont systemFontOfSize:18.f];
  94. }else
  95. {
  96. //其它button的设置
  97. btn.titleLabel.font = [UIFont systemFontOfSize:13.f];
  98. //[self changeColorForButton:btn red:0];
  99. }
  100. //添加按钮事件
  101. [btn addTarget:self action:@selector(actionbtn:) forControlEvents:UIControlEventTouchUpInside];
  102. [_navScrollV addSubview:btn];
  103. }
  104. //设置标签视图的内容视图大小
  105. [_navScrollV setContentSize:CGSizeMake(60 * [arT count], 36)];
  106. [_topNaviV addSubview:_navScrollV];
  107. //调用滑动视图布局
  108. [self addView2Page:_scrollView count:[arT count] frame:CGRectZero];
  109. }
  110. //滑动视图布局
  111. - (void)addView2Page:(UIScrollView *)scrollV count:(NSUInteger)pageCount frame:(CGRect)frame
  112. {
  113. for (int i = 0; i < pageCount; i++)
  114. {
  115. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(scrollV.frame.size.width * i, 0, scrollV.frame.size.width, scrollV.frame.size.height)];
  116. view.tag = i + 1;
  117. view.userInteractionEnabled = YES;
  118. //给每个视图添加点击事件
  119. UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] init];
  120. singleTapRecognizer.numberOfTapsRequired = 1;
  121. [singleTapRecognizer addTarget:self action:@selector(pust2View:)];
  122. [view addGestureRecognizer:singleTapRecognizer];
  123. //调用滑动视图子视图设置方法
  124. [self initPageView:view];
  125. [scrollV addSubview:view];
  126. }
  127. //设置scrollV的宽度为10个屏幕的宽度
  128. [scrollV setContentSize:CGSizeMake(scrollV.frame.size.width * pageCount, scrollV.frame.size.height)];
  129. }
  130. -(void)pust2View:(UITapGestureRecognizer *)singleTapRecognizer
  131. {
  132. }
  133. //滑动视图子视图设置方法
  134. - (void)initPageView:(UIView *)view
  135. {
  136. int width = (view.frame.size.width - 20)/3;
  137. float x = 5;
  138. float y = 4;
  139. UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height - 64)];
  140. int sumJ = (int)(1+arc4random()%4);
  141. int sumI = (int)(1+arc4random()%3);
  142. for (int j = 1; j <= sumJ; j++)
  143. {
  144. for (int i = 1; i <= 3; i++)
  145. {
  146. if (j == sumJ && i > sumI)
  147. {
  148. break;
  149. }
  150. float w = x * i + width * (i - 1);
  151. float h = y * j + width * (j - 1);
  152. UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(w, h, width, width)];
  153. [l setBackgroundColor:[QHCommonUtil getRandomColor]];
  154. [v addSubview:l];
  155. }
  156. }
  157. [view addSubview:v];
  158. }
  159. //显示下拉视图的方法,有动画
  160. - (void)showSelectView:(UIButton *)btn
  161. {
  162. if ([_selectTabV isHidden] == YES)
  163. {
  164. [_selectTabV setHidden:NO];
  165. [UIView animateWithDuration:0.6 animations:^
  166. {
  167. [_selectTabV setFrame:CGRectMake(0, _scrollView.frame.origin.y, _scrollView.frame.size.width, _scrollView.frame.size.height)];
  168. } completion:^(BOOL finished)
  169. {
  170. }];
  171. }else
  172. {
  173. [UIView animateWithDuration:0.6 animations:^
  174. {
  175. [_selectTabV setFrame:CGRectMake(0, _scrollView.frame.origin.y - _scrollView.frame.size.height, _scrollView.frame.size.width, _scrollView.frame.size.height)];
  176. } completion:^(BOOL finished)
  177. {
  178. [_selectTabV setHidden:YES];
  179. }];
  180. }
  181. }
  182. -(void)swipeGesture1Action
  183. {
  184. [[QHViewController shareSlideController]closeLeftView];
  185. }
  186. -(void)scrollHandlePan:(UIPanGestureRecognizer*) panParam
  187. {
  188. BOOL isPaning = NO;
  189. //contentOffset 是scrollview当前显示区域顶点相对于frame顶点的偏移量,比如上个例子你拉到最下面,contentoffset就是(0 ,480),也就是y偏移了480
  190. //假如x偏移量为0
  191. if(_scrollView.contentOffset.x < 0)
  192. {
  193. isPaning = YES;
  194. // isLeftDragging = YES;
  195. // [self showMask];
  196. }
  197. //假如偏移量大于从第一个页面到最后一个页面的长度,
  198. else if(_scrollView.contentOffset.x > (_scrollView.contentSize.width - _scrollView.frame.size.width))
  199. {
  200. isPaning = YES;
  201. // isRightDragging = YES;
  202. // [self showMask];
  203. }
  204. //如果偏移量在范围内,要执行偏移
  205. if(isPaning==NO)
  206. {
  207. //[[QHViewController sharedSliderController] moveViewWithGesture:panParam];
  208. }
  209. }
  210. //标签是图按钮的点击方法,点击button,所对应的滑动视图也会滑动到相应的地方
  211. - (void)actionbtn:(UIButton *)btn
  212. {
  213. //如果上一次是点击button,则把上个button的字体与颜色恢复
  214. UIButton * button2=(UIButton *)[_navScrollV viewWithTag:currentTag];
  215. [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  216. button2.titleLabel.font = [UIFont systemFontOfSize:13.f];
  217. //如果上一次是滑动slidView视图,则把上个button的字体与颜色恢复
  218. UIButton * button3=(UIButton *)[_navScrollV viewWithTag:currentIndex];
  219. [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  220. button3.titleLabel.font = [UIFont systemFontOfSize:13.f];
  221. [UIView beginAnimations:nil context:nil];
  222. [UIView setAnimationDuration:.5];
  223. //点击button时主页面滑动,主页的x坐标改变
  224. //_scrollView的滑动
  225. [_scrollView scrollRectToVisible:CGRectMake(_scrollView.frame.size.width * (btn.tag - 1), _scrollView.frame.origin.y, _scrollView.frame.size.width, _scrollView.frame.size.height) animated:YES];
  226. //随着tag值增加,xx增大,标签视图的x坐标改变
  227. float xx = _scrollView.frame.size.width * (btn.tag - 1) * (60 / self.view.frame.size.width) ;
  228. //标签视图滑动
  229. [_navScrollV scrollRectToVisible:CGRectMake(xx, 0, _navScrollV.frame.size.width, _navScrollV.frame.size.height) animated:YES];
  230. [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
  231. btn.titleLabel.font = [UIFont systemFontOfSize:18.f];
  232. currentTag=btn.tag;
  233. [UIView commitAnimations];
  234. }
  235. - (void)changeColorForButton:(UIButton *)btn red:(float)nRedPercent
  236. {
  237. //float value = [QHCommonUtil lerp:nRedPercent min:0 max:212];
  238. [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
  239. }
  240. //- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
  241. //{
  242. // _startPointX = scrollView.contentOffset.x;
  243. // //NSLog(@"___________________%f",_startPointX);
  244. //
  245. //}
  246. //滑动视图将要结束拖拽时调用
  247. -(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
  248. {
  249. if(scrollView.tag==101)
  250. {
  251. //index是滑动视图滑动到第几个页面
  252. int index = targetContentOffset->x /320;
  253. UIButton * button2=(UIButton *)[_navScrollV viewWithTag:currentIndex];
  254. [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  255. button2.titleLabel.font = [UIFont systemFontOfSize:13.f];
  256. UIButton * button3=(UIButton *)[_navScrollV viewWithTag:currentTag];
  257. [button3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  258. button3.titleLabel.font = [UIFont systemFontOfSize:13.f];
  259. //NSLog(@"**************%d",index);
  260. //标签视图的x坐标
  261. float xx = _scrollView.frame.size.width * (index) * (60 / self.view.frame.size.width) ;
  262. //标签视图滑动
  263. [_navScrollV scrollRectToVisible:CGRectMake(xx, 0, _navScrollV.frame.size.width, _navScrollV.frame.size.height) animated:YES];
  264. UIButton * button1=(UIButton *)[_navScrollV viewWithTag:index+1];
  265. [button1 setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
  266. button1.titleLabel.font = [UIFont systemFontOfSize:18.f];
  267. currentIndex=index+1;
  268. }
  269. }


MainViewController的设置

  1. //
  2. // MainViewController.m
  3. // 侧滑--仿酷狗
  4. //
  5. // Created by mac1 on 14-9-19.
  6. // Copyright (c) 2014年 mac1. All rights reserved.
  7. //
  8. #import "MainViewController.h"
  9. @interface MainViewController ()
  10. {
  11. NSArray *_arData;
  12. }
  13. @end
  14. @implementation MainViewController
  15. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  16. {
  17. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  18. if (self) {
  19. // Custom initialization
  20. }
  21. return self;
  22. }
  23. - (void)viewDidLoad
  24. {
  25. [super viewDidLoad];
  26. [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"b"] ]];
  27. _arData = @[@"新闻", @"订阅", @"图片", @"视频", @"跟帖", @"电台"];
  28. float h =self.view.frame.size.height * 0.7/[_arData count];
  29. UIView * listV=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*0.1, h, self.view.frame.size.width-self.view.frame.size.width*0.3, h*[_arData count])];
  30. for (int i=0; i<6; i++) {
  31. UIButton * button=[[UIButton alloc]initWithFrame:CGRectMake(0, i*h,listV.frame.size.width, h)];
  32. UILabel * label=[[UILabel alloc]initWithFrame:CGRectMake(0,0,button.frame.size.width, h)];
  33. [label setFont:[UIFont systemFontOfSize:20]];
  34. [label setTextColor:[UIColor whiteColor]];
  35. [label setBackgroundColor:[UIColor clearColor]];
  36. [label setText:_arData[i]];
  37. [button addSubview:label];
  38. button.tag=200+i;
  39. [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  40. [listV addSubview:button];
  41. [self.view addSubview:listV];
  42. }
  43. UISwipeGestureRecognizer * swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipAction)];
  44. swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;
  45. [listV addGestureRecognizer:swipeGesture];
  46. // UITapGestureRecognizer *tapGestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(swipAction)];
  47. // [listV addGestureRecognizer:tapGestureRec];
  48. }
  49. -(void)buttonAction:(UIButton*)button
  50. {
  51. if (button.tag==1) {
  52. NSLog(@"订阅");
  53. }
  54. // switch (button.tag) {
  55. // case 1:
  56. // NSLog(@"订阅");
  57. //
  58. // break;
  59. // case 2:
  60. // NSLog(@"图片");
  61. // break;
  62. // default:
  63. // break;
  64. // }
  65. }
  66. -(void)swipAction
  67. {
  68. [[QHViewController shareSlideController]showLeftView];
  69. }
  70. - (void)didReceiveMemoryWarning
  71. {
  72. [super didReceiveMemoryWarning];
  73. // Dispose of any resources that can be recreated.
  74. }
  75. @end

QQ空间侧滑


这里有四个主要的类:MainViewController,LeftViewController,RightViewController,WWSideslipViewController

init方法,把LeftView,MainView,RightView三个视图控制器传入,把左右视图设为隐藏,给主视图设置滑动,单击手势

  1. -(instancetype)initWithLeftView:(UIViewController *)LeftView
  2. andMainView:(UIViewController *)MainView
  3. andRightView:(UIViewController *)RighView
  4. andBackgroundImage:(UIImage *)image;
  5. {
  6. if(self){
  7. speedf = 0.5;
  8. leftControl = LeftView;
  9. mainControl = MainView;
  10. righControl = RighView;
  11. UIImageView * imgview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  12. [imgview setImage:image];
  13. [self.view addSubview:imgview];
  14. //滑动手势
  15. UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
  16. [mainControl.view addGestureRecognizer:pan];
  17. //单击手势
  18. sideslipTapGes= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];
  19. [sideslipTapGes setNumberOfTapsRequired:1];
  20. [mainControl.view addGestureRecognizer:sideslipTapGes];
  21. leftControl.view.hidden = YES;
  22. righControl.view.hidden = YES;
  23. [self.view addSubview:leftControl.view];
  24. [self.view addSubview:righControl.view];
  25. [self.view addSubview:mainControl.view];
  26. }
  27. return self;
  28. }


设置滑动时主视图的缩放,手势结束后设置为固定的大小和位置,根据滑动速度与x坐标偏移量选择主视图的三中显示模式


  1. //滑动手势
  2. - (void) handlePan: (UIPanGestureRecognizer *)rec{
  3. //得到坐标点
  4. CGPoint point = [rec translationInView:self.view];
  5. NSLog(@"____________________%@",NSStringFromCGPoint(point));
  6. scalef = (point.x*speedf+scalef);
  7. NSLog(@"scalef%f",scalef);
  8. //根据主视图位置判断是左滑还是右边滑动
  9. if (rec.view.frame.origin.x>=0){
  10. rec.view.center = CGPointMake(rec.view.center.x + point.x*speedf,rec.view.center.y);
  11. //设置缩放
  12. rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1-scalef/1000,1-scalef/1000);
  13. [rec setTranslation:CGPointMake(0, 0) inView:self.view];
  14. //设置左视图不隐藏,右视图隐藏
  15. righControl.view.hidden = YES;
  16. leftControl.view.hidden = NO;
  17. }
  18. else
  19. {
  20. rec.view.center = CGPointMake(rec.view.center.x + point.x*speedf,rec.view.center.y);
  21. rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1+scalef/1000,1+scalef/1000);
  22. [rec setTranslation:CGPointMake(0, 0) inView:self.view];
  23. righControl.view.hidden = NO;
  24. leftControl.view.hidden = YES;
  25. }
  26. //手势结束后修正位置
  27. if (rec.state == UIGestureRecognizerStateEnded) {
  28. if (scalef>140*speedf){
  29. [self showLeftView];
  30. }
  31. else if (scalef<-140*speedf) {
  32. [self showRighView]; }
  33. else
  34. {
  35. [self showMainView];
  36. scalef = 0;
  37. }
  38. }
  39. }


单击恢复主视图原大小

  1. #pragma mark - 单击手势
  2. -(void)handeTap:(UITapGestureRecognizer *)tap{
  3. if (tap.state == UIGestureRecognizerStateEnded) {
  4. [UIView beginAnimations:nil context:nil];
  5. tap.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
  6. tap.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
  7. [UIView commitAnimations];
  8. scalef = 0;
  9. }
  10. }

主视图的三中显示模式

  1. #pragma mark - 修改视图位置
  2. //恢复位置
  3. -(void)showMainView{
  4. [UIView beginAnimations:nil context:nil];
  5. mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
  6. mainControl.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
  7. [UIView commitAnimations];
  8. }
  9. //显示左视图
  10. -(void)showLeftView{
  11. [UIView beginAnimations:nil context:nil];
  12. mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
  13. mainControl.view.center = CGPointMake(340,[UIScreen mainScreen].bounds.size.height/2);
  14. [UIView commitAnimations];
  15. }
  16. //显示右视图
  17. -(void)showRighView{
  18. [UIView beginAnimations:nil context:nil];
  19. mainControl.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
  20. mainControl.view.center = CGPointMake(-60,[UIScreen mainScreen].bounds.size.height/2);
  21. [UIView commitAnimations];
  22. }
  23. #warning 为了界面美观,所以隐藏了状态栏。如果需要显示则去掉此代码
  24. - (BOOL)prefersStatusBarHidden
  25. {
  26. return YES; //返回NO表示要显示,返回YES将hiden
  27. }







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/279500
推荐阅读
相关标签
  

闽ICP备14008679号