美文网首页UI基础
UISwitch - UISlider - UIDatePick

UISwitch - UISlider - UIDatePick

作者: 居然是村长 | 来源:发表于2016-03-15 21:39 被阅读118次

    内容比较少,用的也比较少,稍微记录一下。都是 UIControl 的 子类,UIControl 需要好好研究一下。

    UISwitch 简单示例

        // 写个默认的背景,对比下开关设置的rect
        UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
        backgroundView.backgroundColor = [UIColor grayColor];
        [self.view addSubview:backgroundView];
        
        // seitch 大小固定,只与(x,y) 有关
        UISwitch *aswitch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];
        aswitch.backgroundColor = [UIColor yellowColor];
        [self.view addSubview:aswitch];
        
        // 几个属性
        aswitch.thumbTintColor = [UIColor orangeColor]; // 滑块
        aswitch.onTintColor = [UIColor greenColor]; // on
        aswitch.tintColor = [UIColor redColor]; // off(背景)
        // 设置开关
        [aswitch setOn:!aswitch.isOn animated:YES];
        
        // 下面的设置无效了,也没有提示...在文档中才有说 iOS7 之后无效(实在是这玩意没啥用啊)
        aswitch.offImage = [UIImage imageNamed:@"iconfont-off"];
        aswitch.onImage = [UIImage imageNamed:@"iconfont-on"];
    

    UISlider

        // 滑块 高度已定,内容也会自动调整,
        self.aslider = [[UISlider alloc] initWithFrame:CGRectMake(10, 100, CGRectGetWidth(self.view.bounds) - 20, 100)];
        [self.view addSubview:self.aslider];
        
        self.aslider.minimumValue = 100;// 最小值
        self.aslider.maximumValue = 900;// 最大值
        self.aslider.value = 200.;// 当前值,一般用来获取
        
        self.aslider.minimumTrackTintColor = [UIColor redColor];// 已完成的颜色
        self.aslider.maximumTrackTintColor = [UIColor greenColor];// 未完成的颜色
        self.aslider.thumbTintColor = [UIColor blueColor];// 滑块颜色
        
        self.aslider.minimumValueImage = [UIImage imageNamed:@"iconfont-on"];// 左侧图片,滑块,也会对应变短
        self.aslider.maximumValueImage = [UIImage imageNamed:@"iconfont-off"];// 右侧图片
        
        self.aslider.continuous = YES; // yes 时:value 变化就会通知;no 时:拖动结束才通知
        
        // 根据不同的state 设置图片,并获取
        // 注意这里的image 不做处理的话是随着滑块的移动而拉伸的,可以考虑重写,不使用拉伸的imageView
        [self.aslider setThumbImage:[UIImage imageNamed:@"iconfont-on"] forState:UIControlStateNormal];
        [self.aslider setMinimumTrackImage:[UIImage imageNamed:@"iconfont-on"] forState:UIControlStateNormal];
        [self.aslider setMaximumTrackImage:[UIImage imageNamed:@"iconfont-off"] forState:UIControlStateNormal];
        // 对应有获取图片,就不写了...
    
    // 添加事件,value 变化。
        [self.aslider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    
    // 重写一个 举例
    -(CGRect)minimumValueImageRectForBounds:(CGRect)bounds; {
        for(UIView *view in [self subviews]) {
            if ([view isKindOfClass:[UIImageView class]]) {
                view.clipsToBounds = YES;
                view.contentMode = UIViewContentModeBottomLeft;
                // max 类似
            }
        }
        return bounds;
    }
    

    UIDatePick

    • 一般日期选择
        self.datePick = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.bounds), 200)];
        [self.view addSubview:self.datePick];
        
        self.datePick.datePickerMode = UIDatePickerModeDateAndTime;
        /*
        typedef NS_ENUM(NSInteger, UIDatePickerMode) {
            UIDatePickerModeTime,           // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)
            UIDatePickerModeDate,           // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)
            UIDatePickerModeDateAndTime,    // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)
            UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
        };
        */
    
    // 设置 时间选择 范围
        self.datePick.minimumDate = [NSDate dateWithTimeIntervalSince1970:0];
        self.datePick.maximumDate = [NSDate date];
        
    // 一些设置,看另一篇把 分别是:本地,时区,历法
        self.datePick.locale = [NSLocale currentLocale];
        self.datePick.timeZone = [NSTimeZone defaultTimeZone];
        self.datePick.calendar = [NSCalendar currentCalendar];
        
    // 获取 和 设置时间
        NSDate *selectDate = self.datePick.date;
        [self.datePick setDate:[NSDate dateWithTimeIntervalSinceNow:-3600] animated:YES];
        
    // 添加事件
        [self.datePick addTarget:self action:@selector(testAction) forControlEvents:UIControlEventValueChanged];
    
    
    • 倒计时
    就是: UIDatePickerMode:UIDatePickerModeCountDownTimer
    
        self.datePick.minuteInterval = 1.;// 显示的时间间隔 n分钟
        self.datePick.countDownDuration = 120;// 剩余时间(还要自己写 --);
    

    UISegmentedControl

        NSArray *items = @[@"1",@"2222",@"3",@"444"];
        self.segmentedControl = [[UISegmentedControl alloc] initWithItems:items];
        self.segmentedControl.frame = CGRectMake(10, 100, 300, 66);
        [self.view addSubview:self.segmentedControl];
        
    
        self.segmentedControl.tintColor = [UIColor redColor];// 颜色
        self.segmentedControl.apportionsSegmentWidthsByContent = YES;// 默认NO:平均分; YES: 宽度自动调整。
        NSInteger selectindex = self.segmentedControl.selectedSegmentIndex;// 选中项
        NSLog(@"%zi",self.segmentedControl.selectedSegmentIndex);
    
        // 移除 插入
        [self.segmentedControl removeSegmentAtIndex:9 animated:YES];
        [self.segmentedControl removeAllSegments];
        [self.segmentedControl insertSegmentWithTitle:@"555" atIndex:0 animated:YES];
        [self.segmentedControl insertSegmentWithTitle:@"555" atIndex:0 animated:YES];
        [self.segmentedControl insertSegmentWithTitle:@"555" atIndex:0 animated:YES];
        [self.segmentedControl insertSegmentWithTitle:@"555" atIndex:0 animated:YES];
    
        // 设置属性 内容 宽 偏移 可用性
        [self.segmentedControl setTitle:@"1231" forSegmentAtIndex:0];
        [self.segmentedControl setWidth:100 forSegmentAtIndex:0];
        [self.segmentedControl setContentOffset:CGSizeMake(-40, 20) forSegmentAtIndex:0];
        [self.segmentedControl setEnabled:YES forSegmentAtIndex:0];
        // 对应有获取,也不写了
    
        
        // 背景
        [self.segmentedControl setBackgroundImage:[UIImage imageNamed:@"iconfont-off"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        // 属性字符串
        [self.segmentedControl setTitleTextAttributes:@{NSUnderlineStyleAttributeName:@1} forState:UIControlStateNormal];
        // 内容 偏移 type 表示那些需要设置
        [self.segmentedControl setContentPositionAdjustment:UIOffsetMake(10, 20) forSegmentType:UISegmentedControlSegmentCenter barMetrics:UIBarMetricsDefault];
        
        // 添加事件
        [self.segmentedControl addTarget:self action:@selector(testAction) forControlEvents:UIControlEventValueChanged];
    
    
    // title 也可以是 image
    

    UIPageControl

    一般结合 scrolleView 使用,简单也一下本身的使用。

        self.apageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        [self.view addSubview:self.apageControl];
        
        self.apageControl.numberOfPages = 5;// 总页数
        self.apageControl.currentPage = 2;// 当前页
        self.apageControl.hidesForSinglePage = YES;// 单页时,隐藏
        
        self.apageControl.defersCurrentPageDisplay = YES;// 相当于 标记 需要更新page,结合下面的使用
        [self.apageControl updateCurrentPageDisplay];// 更新 page
     
        self.apageControl.pageIndicatorTintColor = [UIColor redColor];// 未选择的点
        self.apageControl.currentPageIndicatorTintColor = [UIColor greenColor];// 选中的点
        
        // 添加 点击圆点的 事件
        [self.apageControl addTarget:self action:@selector(testAction) forControlEvents:UIControlEventValueChanged];
    

    1

    相关文章

      网友评论

        本文标题:UISwitch - UISlider - UIDatePick

        本文链接:https://www.haomeiwen.com/subject/mggflttx.html