iOS自定义弹出视图,以及简单的手势讲解

学习iOS也有一段时间了。也在试着用iOS来写项目,感谢各路大神的帮助,就不一一@了。本文只是记载本人的学习过程。

                                                                           ---------------------学如逆水行舟不进则退。

1.相信有很多的应用都有弹出框的需求,本文来讲解一下简单的弹出框,以及弹出框对应的手势问题

在一个视图控制器中的一个按钮来响应我们的弹出图例如遮掩:


我们要实现点击左上的按钮来实现效果应该怎么办?

2,首先自定义一个弹出视图,手势也会加在里面的,代码如下

.h

//

//  H_tanchuVw.h

//  Dragon

//

//  Created by 黄权浩 on 14-12-9.

//  Copyright (c) 2014 ZHAO. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface H_tanchuVw : UIView<UITableViewDataSource, UITableViewDelegate>

{

@private

    //弹出表视图

    UITableView *tableView1;

}

//创建二级页面的弹出视图

- (void)_init;


//开启

- (void)open;


//关闭

- (void)gandiaotanchu;


//向右的手势

@property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipeGestureRecognizer;


//向左的手势

@property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer;

@end


.m

//

//  H_tanchuVw.m

//  Dragon

//

//  Created by 黄权浩 on 14-12-9.

//  Copyright (c) 2014 ZHAO. All rights reserved.

//


#import "H_tanchuVw.h"


@implementation H_tanchuVw


- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

        

        self.leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

        self.rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];

        

        self.leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;

        self.rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;

        

        [self addGestureRecognizer:self.leftSwipeGestureRecognizer];

        [self addGestureRecognizer:self.rightSwipeGestureRecognizer];

        

        UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gandiaotanchu)];

        UIView *sdasdas= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, self.bounds.size.height)];

        sdasdas.backgroundColor = [UIColor clearColor];

        [gest setNumberOfTapsRequired:1];

        [sdasdas addGestureRecognizer:gest];

        [self addSubview:sdasdas];

        self.backgroundColor = [UIColor clearColor];

        

        [self _init];

    }

    return self;

}


//创建弹出视图

- (void)_init

{

    

    //弹出图

    tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(60, 64, 320, self.bounds.size.height-64)];

//    tableView1.separatorStyle = UITableViewCellSeparatorStyleNone;

    tableView1.backgroundColor = [UIColor whiteColor];

    tableView1.delegate = self;

    tableView1.dataSource = self;

    tableView1.rowHeight = 44;

    [self addSubview:tableView1];

}


//开启弹出图

- (void)open

{

    

}


//关闭弹出图

- (void)gandiaotanchu

{

    tableView1.frame = CGRectMake(60, 64, 320, self.bounds.size.height-64);

    [[NSNotificationCenter defaultCenter]postNotificationName:@"gandiao" object:nil];

}


#pragma mark - TableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 88;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSString *cellIdentifier = [NSString stringWithFormat:@"cell%ld",(long)indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    }

    

    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];

    lb.textColor = [UIColor blackColor];

    lb.backgroundColor = [UIColor clearColor];

    lb.textAlignment = NSTextAlignmentLeft;

    lb.font = [UIFont systemFontOfSize:15];

    lb.text = [NSString stringWithFormat:@"dadawdawdasdadw%ld",(long)indexPath.row];

    [cell addSubview:lb];

    

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;

}




-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"%ld",(long)indexPath.row);

}


//滑动的手势这里实现了向左滑动跟向右滑动

- (void)handleSwipes:(UISwipeGestureRecognizer *)sender

{

    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {

        [UIView beginAnimations:nil context:nil];

        [UIView setAnimationDuration:0.2f];

        tableView1.frame = CGRectMake(0, 64, 320, self.bounds.size.height-64);

        [UIView commitAnimations];

    }

    

    if (sender.direction == UISwipeGestureRecognizerDirectionRight) {

        tableView1.frame = CGRectMake(60, 64, 320, self.bounds.size.height-64);

        [[NSNotificationCenter defaultCenter]postNotificationName:@"gandiao" object:nil];

    }

}


@end

//先贴出图片吧,比较直观
原图

弹出图

向左清扫


向右清扫


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄权浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值