zoukankan      html  css  js  c++  java
  • 抽屉效果

    抽屉效果,页面灵活漂亮并且节省空间。

    在根视图的h文件:

     1 #import <UIKit/UIKit.h>
     2 //实现抽屉效果
     3 //由于手机屏幕较小,内容较多,有效利用空间 。
     4 @interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
     5 {
     6     UITableView  *tv1;
     7     UITableView *tv2;
     8     CGPoint startPoint;
     9     CGPoint endPoint;
    10 }
    11 @end

    .m文件

     1 #import "RootViewController.h"
     2 
     3 @interface RootViewController ()
     4 
     5 @end
     6 
     7 @implementation RootViewController
     8 
     9 - (void)viewDidLoad {
    10     [super viewDidLoad];
    11     // Do any additional setup after loading the view.
    12     [self createUI];
    13 }
    14 -(void)createUI{
    15     tv1=[[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 600) style:UITableViewStylePlain];
    16     tv1.delegate=self;
    17     tv1.dataSource=self;
    18      tv1.tag=1;
    19     [self.view addSubview:tv1];
    20     tv2=[[UITableView alloc] initWithFrame:CGRectMake(0, 20, 375, 600) style:UITableViewStylePlain];
    21     tv2.delegate=self;
    22     tv2.dataSource=self;
    23     tv2.tag=2;
    24     [self.view addSubview:tv2];
    25     
    26     //使用拖拽手势(移动)实现抽屉效果
    27     UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan_View:)];
    28     [self.view addGestureRecognizer:pan];
    29 }
    30 -(void)pan_View:(UIPanGestureRecognizer *)sender{
    31     NSLog(@"你拖拽了视图");
    32     //分析抽屉效果的原理:通过改变坐标来实现。(tv2在tv1上面移动)
    33     //手势向右移动多少,上层视图的横坐标就向右移动多少
    34     //先获得手势的坐标
    35     CGPoint p=[sender locationInView:self.view];
    36     //判断手势的状态是否是拖拽的开始
    37     if (sender.state==UIGestureRecognizerStateBegan) {
    38         //手势开始的坐标
    39         startPoint=p;
    40     }
    41     
    42     if (p.x>startPoint.x) {
    43         if (tv2.frame.origin.x==250) {
    44             return;
    45         }else{
    46             //tv2的x随着手势的x的变化而变化
    47             tv2.frame=CGRectMake(p.x-startPoint.x, 20, 375, 600);
    48         }
    49     }else{
    50         //判断tv2的x值如果tv2的x不为0,让tv2随手势的x值变化而变化
    51         if (tv2.frame.origin.x!=0) {
    52             tv2.frame=CGRectMake(250+p.x-startPoint.x, 20, 375, 600);
    53             tv1.alpha=0.5;
    54         }
    55     }
    56 
    57     //判断手势的结束状态
    58     if (sender.state==UIGestureRecognizerStateEnded) {
    59         if (tv2.frame.origin.x>100) {
    60             [UIView animateWithDuration:1 animations:^{
    61                 tv2.frame=CGRectMake(250, 20, 375, 600);
    62             }];
    63         }else{
    64             [UIView animateWithDuration:1 animations:^{
    65                 //动画
    66                 tv2.frame=CGRectMake(0, 20, 375, 600);
    67             }];
    68         }
    69     }
    70 }
    71 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    72     return 30;
    73 }
    74 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    75     NSString *identifier=@"swipe";
    76     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    77     if (cell==nil) {
    78         cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    79     }
    80     NSInteger i=tableView.tag;
    81     if (i==1) {
    82         cell.textLabel.text=[NSString stringWithFormat:@"geqhfjchckheqwf2jffk1%ld",indexPath.row];
    83         cell.backgroundColor=[UIColor redColor];
    84     }else{
    85         cell.textLabel.text=[NSString stringWithFormat:@"hggrf e2jefwefekjf%c",'A'+(int)indexPath.row];
    86         cell.backgroundColor=[UIColor grayColor];
    87     }
    88     return cell;
    89 }
  • 相关阅读:
    (全国多校重现赛一) H Numbers
    (全国多校重现赛一)E-FFF at Valentine
    (全国多校重现赛一)B-Ch's gifts
    (全国多校重现赛一)A-Big Binary Tree
    UVA-10391 Compoud Words
    HDU-1027Ignatius and princess II
    CodeForces-501B
    UVA-136Ugly numbers
    UVA-101
    UVA-10474
  • 原文地址:https://www.cnblogs.com/crushing/p/4820331.html
Copyright © 2011-2022 走看看