界面之间的转换以及按钮提示框的演示

在appdelegate.m中 

//导入头文件然后更改主窗口

 _window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[ViewController new]];

//在ViewController,m中

//先定义两个宏

#define SWidth self.view.frame.size.width

#define SHeight self.view.frame.size.height

//注意不要忘了定义协议

@interface ViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>{

//滚动视图

    UIScrollView *_scrollView;

//按钮

    UIButton *_lastBtn;

//视图

    UIView *_barView;

//表格

    UITableView *_table;

//数组

    NSArray *_dataArr;

    NSArray *_contentArr;

}

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    _dataArr = @[@"111",@"222",@"333",@"444",@"555",@"666",@"+"];

    _contentArr = @[@"添加",@"删除",@"关闭"];

    // 设置标题

    self.title = @"八维教育";

    self.edgesForExtendedLayout = UIRectEdgeNone;

    // 创建滚动视图

    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, SHeight-40)];

    _scrollView.delegate = self;

    _scrollView.pagingEnabled = YES;

    _scrollView.contentSize = CGSizeMake(SWidth*(_dataArr.count-1), SHeight-40);

    _scrollView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:_scrollView];

    for (int i = 0; i<_dataArr.count-1; i++) {

//创建UIView视图

        UIView *v = [[UIView alloc]initWithFrame:CGRectMake(SWidth*i, 0, SWidth, _scrollView.frame.size.height)];

//设置背景颜色

        v.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0)green:((float)arc4random_uniform(256) / 255.0)blue:((float)arc4random_uniform(256) / 255.0)alpha:1.0];

        [_scrollView addSubview:v];

    }

    // 创建自制bar

    _barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SWidth, 40)];

    _barView.backgroundColor = [UIColor whiteColor];

    for (int i=0; i<_dataArr.count; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        [btn setTitle:_dataArr[i] forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

        btn.frame = CGRectMake(SWidth/_dataArr.count*i, 0, SWidth/_dataArr.count, 40);

        btn.tag = 100+i;

        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

        if (i==0) {

            btn.selected = YES;

            btn.backgroundColor = [UIColor orangeColor];

            _lastBtn = btn;

        }

        btn.layer.cornerRadius = 5;

        btn.layer.masksToBounds = YES;

        [_barView addSubview:btn];

    }

    [self.view addSubview:_barView];


    // 创建表格

    _table = [[UITableView alloc]initWithFrame:CGRectMake(SWidth-10, 41, 0.1, 0.1)];

    _table.delegate = self;

    _table.dataSource = self;

    [self.view addSubview:_table];


}

// 按钮点击方式

-(void)click:(UIButton *)sender{


    if (sender.tag != 100+_dataArr.count-1) {

        _lastBtn.selected = NO;

        _lastBtn.backgroundColor = [UIColor whiteColor];

        sender.backgroundColor = [UIColor orangeColor];

        _lastBtn = sender;

        if (sender.selected == YES) {

            sender.selected = NO;

        }

        else if (sender.selected == NO){

            sender.selected = YES;

        }

        _scrollView.contentOffset = CGPointMake(SWidth*(sender.tag-100), 0);

    }

    else{

        if (sender.selected == YES) {

            sender.selected = NO;

            _table.hidden = YES;

            sender.backgroundColor = [UIColor whiteColor];

            _table.frame = CGRectMake(SWidth-10, 41, 0.1, 0.1);

        }

        else if (sender.selected == NO){

            sender.selected = YES;

            _table.hidden = NO;

            sender.backgroundColor = [UIColor orangeColor];

            [UIView animateWithDuration:0.3 animations:^{

                _table.frame = CGRectMake(SWidth*2/3, 41, SWidth/3-10, 128);

            }];

        }

    }

}

#pragma mark scrollView的代理

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{


    CGFloat n = scrollView.contentOffset.x/SWidth;

    UIButton *btn = [_barView viewWithTag:n+100];

    [self click:btn];

}

#pragma mark tableView的代理

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

    return 3;

}

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

    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];


    cell.textLabel.text = _contentArr[indexPath.row];

    return cell;

}

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


    // 显示提示框

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:_contentArr[indexPath.row] message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];

    [alert show];

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值