iOS文本框处理方式的不同

- (IBAction)login
{
    NSLog(@"%s %@ %@", __func__, self.userNameText.text, self.pwdText.text);
}


#pragma mark 文本框代理方法
// 在文本框中按回车的处理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"%@", textField);
    // 如果光标在用户名,切换到密码
    if (textField == self.userNameText) {
        // 密码成为第一响应者
        [self.pwdText becomeFirstResponder];
    } else if (textField == self.pwdText) {
        // 输入焦点就在密码框中
        // 如果是密码,直接调用登录方法
        [self login];
        
        // 关闭键盘
//        [self.view endEditing:YES];
        // 让密码文本框关闭键盘
//        [textField resignFirstResponder];
        [self.pwdText resignFirstResponder];
    }
    
    return YES;
}




    // 设置间距
    // 只是指定内容外侧边距,并不会根据contentSize自动调整contentOffset
    self.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);
//    // 修改contentOffset
//    self.scrollView.contentOffset = CGPointMake(0, -64);
    
    
    // 设置滚动视图内容
    // 1> 如果当前有间距,根据间距自动调整contentOffset
    // 2> 如果没有间距,contentOffset是(0,0)
    CGFloat h = CGRectGetMaxY(self.lastButton.frame) + 10;
    self.scrollView.contentSize = CGSizeMake(0, h);




/**
 setter方法
 
 contentSize    会根据边距调整offset
 contentInset   不会调整offset
 */
// 系统加载了Main.storyboard后,给scrollView对象进行赋值
// setScrollView是由系统自动调用的






- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        
        // 设置属性
        // 设置边距
        _scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
        
        // 不显示水平滚动标示
        _scrollView.showsHorizontalScrollIndicator = NO;
        // 不显示垂直滚动标示
        _scrollView.showsVerticalScrollIndicator = NO;
        
        // *** 偏移位置
        _scrollView.contentOffset = CGPointMake(0, 0);
        
        // 取消弹簧效果,内容固定,不希望出现弹簧效果时
        // 不要跟bounds属性搞混了
        _scrollView.bounces = NO;
        
        // 设置代理
        _scrollView.delegate = self;
        // 设置最大/最小缩放比例
        _scrollView.maximumZoomScale = 2.0;
        _scrollView.minimumZoomScale = 0.2;
        
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}






/** 开始 */
- (IBAction)start
{
    // 倒计时10秒,每秒更新一下Label的显示
    // 计时器
    /** 
     参数说明 
     1. 时间间隔,double
     2. 监听时钟触发的对象
     3. 调用方法
     4. userInfo,可以是任意对象,通常传递nil
     5. repeats:是否重复
     */
    self.counterLabel.text = @"10";
    
    // scheduledTimerWithTimeInterval 方法本质上就是创建一个时钟,
    // 添加到运行循环的模式是DefaultRunLoopMode
    // ----------------------------------------------
    // 1>
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:@"hello timer" repeats:YES];
    
    // ----------------------------------------------
    // 2> 与1等价
//    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
//    // 将timer添加到运行循环
//    // 模式:默认的运行循环模式
//    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    
    // ----------------------------------------------
    // 3>
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer:) userInfo:nil repeats:YES];
    // 将timer添加到运行循环
    // 模式:NSRunLoopCommonModes的运行循环模式(监听滚动模式)
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    //[self updateTimer:self.timer];
}


/** 时钟更新方法 */
- (void)updateTimer:(NSTimer *)timer
{
   // NSLog(@"%s", __func__);
    // 1. 取出标签中的数字
    int counter = self.counterLabel.text.intValue;
    
    // 2. 判断是否为零,如果为0,停止时钟
    if (--counter < 0) {
        // 停止时钟
        [self pause];
        
        // 提示用户,提示框
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦......" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"哈哈", nil];
//
//        [alert show];
        [[[UIAlertView alloc] initWithTitle:@"开始" message:@"开始啦......" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"哈哈", nil] show];
    } else {
        // CTRL + I
        // 3. 修改数字并更新UI
        self.counterLabel.text = [NSString stringWithFormat:@"%d", counter];
    }
}


/** 暂停 */
- (IBAction)pause
{
    // 停止时钟,invalidate是唯一停止时钟的方法
    // 一旦调用了invalidate方法,timer就无效了,如果再次启动时钟,需要重新实例化
    [self.timer invalidate];
}


#pragma mark - alertView代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ldaaa-------", (long)buttonIndex);
}






- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 300, 130)];
        _scrollView.backgroundColor = [UIColor redColor];
        
        [self.view addSubview:_scrollView];
        
        // 取消弹簧效果
        _scrollView.bounces = NO;
        
        // 取消水平滚动条
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        
        // 要分页
        _scrollView.pagingEnabled = YES;
        
        // contentSize
        _scrollView.contentSize = CGSizeMake(kImageCount * _scrollView.bounds.size.width, 0);
        
        // 设置代理
        _scrollView.delegate = self;
    }
    return _scrollView;
}


- (UIPageControl *)pageControl
{
    if (_pageControl == nil) {
        // 分页控件,本质上和scrollView没有任何关系,是两个独立的控件
        _pageControl = [[UIPageControl alloc] init];
        // 总页数
        _pageControl.numberOfPages = kImageCount;
        // 控件尺寸
        CGSize size = [_pageControl sizeForNumberOfPages:kImageCount];
        
        _pageControl.bounds = CGRectMake(0, 0, size.width, size.height);
        _pageControl.center = CGPointMake(self.view.center.x, 130);
        
        // 设置颜色
        _pageControl.pageIndicatorTintColor = [UIColor redColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
        
        [self.view addSubview:_pageControl];
        
        // 添加监听方法
        /** 在OC中,绝大多数"控件",都可以监听UIControlEventValueChanged事件,button除外" */
        [_pageControl addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return _pageControl;
}


// 分页控件的监听方法
- (void)pageChanged:(UIPageControl *)page
{
    NSLog(@"%d", page.currentPage);
    
    // 根据页数,调整滚动视图中的图片位置 contentOffset
    CGFloat x = page.currentPage * self.scrollView.bounds.size.width;
    [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
}


// 视图加载完成调用,通常用来设置数据
- (void)viewDidLoad
{
    [super viewDidLoad];


    // 设置图片
    for (int i = 0; i < kImageCount; i++) {
        NSString *imageName = [NSString stringWithFormat:@"img_%02d", i + 1];
        UIImage *image = [UIImage imageNamed:imageName];
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.scrollView.bounds];
        imageView.image = image;
        
        [self.scrollView addSubview:imageView];
    }
    
    // 计算imageView的位置
    [self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
        
        // 调整x => origin => frame
        CGRect frame = imageView.frame;
        frame.origin.x = idx * frame.size.width;
        
        imageView.frame = frame;
    }];
//    NSLog(@"%@", self.scrollView.subviews);
    
    // 分页初始页数为0
    self.pageControl.currentPage = 0;
    
    // 启动时钟
    [self startTimer];
}


- (void)startTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}


- (void)updateTimer
{
    // 页号发生变化
    // (当前的页数 + 1) % 总页数
    int page = (self.pageControl.currentPage + 1) % kImageCount;
    self.pageControl.currentPage = page;
    
    NSLog(@"%d", self.pageControl.currentPage);
    // 调用监听方法,让滚动视图滚动
    [self pageChanged:self.pageControl];
}


#pragma mark - ScrollView的代理方法
// 滚动视图停下来,修改页面控件的小点(页数)
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 停下来的当前页数
    NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
    
    // 计算页数
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    self.pageControl.currentPage = page;
}


@end





























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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值