学习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
//
// 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
向左清扫
向右清扫