zoukankan      html  css  js  c++  java
  • 2015/10/3 iOS 笔记 细节 iOS9中UIAlertController的简单使用 ScrollView NSTimer

    一、iOS9中UIAlertController的简单使用

      很明显,简单的UIAlertView已经不能用了,我感觉很伤心。

      // 创建

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"开始了" message:@"开始了!" preferredStyle:UIAlertControllerStyleActionSheet];

      // UIAlertControllerStyleActionSheet 是显示在屏幕底部

      // UIAlertControllerStyleAlert 是显示在中间

            // 设置按钮

            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

            UIAlertAction *defult = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];

    //        UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"destructive" style:UIAlertActionStyleDestructive handler:nil];

            // 添加按钮

            [alert addAction:cancel];

            [alert addAction:defult];

    //        [alert addAction:destructive];

            

            //显示

            [self presentViewController:alert animated:YES completion:nil];

    ——----——————// 复杂的,添加TextField,并监听。

     注:文本输入框只能添加到Alert的风格中,ActionSheet是不允许的

            [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){

                textField.placeholder = @"登陆";

            }];

            [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){

                textField.placeholder = @"密码";

                textField.secureTextEntry = YES;

            }];

            [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){

                textField.placeholder = @"添加监听代码";

                // 要设置UITextFieldText的代理

                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:textField];

            }];

            

            

            [self presentViewController:alert animated:YES completion:nil];

            

     // 监听方法实现

    - (void)alertTextFieldTextDidChange:(NSNotification *)notification

    {

        UIAlertController *alert = (UIAlertController *)self.presentedViewController;

        if (alert) {

            // 下标为2的是最后一个,添加了监听的 alert.textFields[2]

            UITextField *lisen = alert.textFields[2];

            

            // 限制输出长度,超过6个则不允许点击确认键

            // 超过6个按钮变灰色 enabled = NO;

            UIAlertAction *action = alert.actions.lastObject;

            action.enabled = lisen.text.length <= 6;

        }

        

    }

    效果是这样的!

    二、NSTimer

    NSTimer准确吗?如果不准备,怎么办?

    不准确。通常用来有一定时间跨度的周期性时间的处理!

    处理Timer可以用多线程,在游戏中多用CADisplayLink。

        /** 

         参数说明 

         1. 时间间隔,double

         2. 监听时钟触发的对象

         3. 调用方法

         4. userInfo,可以是任意对象,通常传递nil

         5. repeats:是否重复

         */

        // 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];

        // 停止时钟,invalidate是唯一停止时钟的方法

        // 一旦调用了invalidate方法,timer就无效了,如果再次启动时钟,需要重新实例化

        [self.timer invalidate];

    三、ScrollView

    /**

     放大缩小

     1> 设置代理

     2> 指定最大/最小的缩放比例

     */

    // 图像的setter

    - (void)setImage:(UIImage *)image

    {

        _image = image;

        

        // 设置图像视图的内容

        self.imageView.image = image;

        // 让图像视图根据图像自动调整大小

        [self.imageView sizeToFit];

        

        // 告诉scrollView内部内容的实际大小

        self.scrollView.contentSize = image.size;

    }

    /**

     在getter方法中

     * 如果是属性自身的,使用_成员变量

     * 如果是其他属性,使用self. getter方法,从而可以保证如果该对象没有被实例化,能够及时的被创建并加载

     */

    - (UIImageView *)imageView

    {

        if (_imageView == nil) {

            _imageView = [[UIImageView alloc] init];

            

            [self.scrollView addSubview:_imageView];

        }

        return _imageView;

    }

    - (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;

    }

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // 设置图像

        self.image = [UIImage imageNamed:@"minion"];

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];

        btn.center = self.view.center;

        [self.view addSubview:btn];

        

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

    }

    - (void)click

    {

        // 移动大图的偏移位置

        CGPoint offset = self.scrollView.contentOffset;

        offset.x += 20;

        offset.y += 20;

        

        // 注意:设置contentOffset会忽略contentSize

        self.scrollView.contentOffset = offset;

    }

    #pragma mark - UIScrollView的代理方法

    /**

     1> 设置了代理

     2> 指定了最大、最小的缩放比例

     表示ScrollView是可以缩放的

     代理方法的"返回值"实际上就是控制器告诉滚动视图,要缩放的是UIImageView

     */

    // 告诉ScrollView要缩放的视图是谁,具体的缩放实现,是由ScrollView来完成的

    // 1> scrollView要知道缩放谁

    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

    {

        return self.imageView;

    }

    // 2> 滚动视图即将开始缩放,通常不需要写

    - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view

    {

        NSLog(@"%s", __func__);

    }

    // 3> 正在缩放,通常也不需要实现

    - (void)scrollViewDidZoom:(UIScrollView *)scrollView

    {

        //    NSLog(@"%s", __func__);

        NSLog(@"%@", NSStringFromCGAffineTransform(self.imageView.transform));

    }

    // 4> 完成缩放,通常也不需要实现

    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale

    {

        NSLog(@"%s", __func__);

    }

    ________________________________________________________________________________________

    运行循环演示

    int main(int argc, const char * argv[])

    {

        @autoreleasepool {

            

            int selection = -1;

            while (YES) {

                printf("请输入选择,0表示退出:");

                scanf("%d", &selection);

                

                if (selection == 0) {

                    printf("欢迎下次再来!88 ");

                    break;

                } else {

                    printf("您选择了第 %d 项功能 ", selection);

                }

            }

        }

        return 0;

    }

  • 相关阅读:
    POJ 1328 Radar Installation
    POJ 1700 Crossing River
    POJ 1700 Crossing River
    poj 3253 Fence Repair (贪心,优先队列)
    poj 3253 Fence Repair (贪心,优先队列)
    poj 3069 Saruman's Army(贪心)
    poj 3069 Saruman's Army(贪心)
    Redis 笔记与总结2 String 类型和 Hash 类型
    数据分析方法有哪些_数据分析方法
    数据分析方法有哪些_数据分析方法
  • 原文地址:https://www.cnblogs.com/pjl0426/p/4853651.html
Copyright © 2011-2022 走看看