• [6]UIControl及其⼦子类


    UISegmentedControl 分段控件

    //根控制器  .h文件
    #import <UIKit/UIKit.h>
    @interface RootViewController : UIViewController<UITextFieldDelegate>
    //添加视图属性 方便切换a和b,然后重写get方法实现 初始化
    @property (nonatomic, retain) UIView *aView;
    @property (nonatomic, retain) UIView *bView;
    @property (nonatomic, retain) UIImageView *imageView;
    @end
    //.m文件
    @implementation RootViewController
    - (void)dealloc
    {
        [_aView release];
        [_bView release];
        [_imageView release];
        [super dealloc];
    }
    //点击return释放手势
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
        }
    //点击手势 随机一张照片
    - (void)tapView
    {
        _imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d",arc4random() % 5 + 1]];
    }
    //aView的get方法
    - (UIView *)aView
    {
        if (_aView == nil)
        {
            _aView = [[UIView alloc] initWithFrame:CGRectMake(30, 150, 300, 500)];
            UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(30, 10, 200, 40)];
            tf.placeholder = @"输入框";
            tf.borderStyle = UITextBorderStyleRoundedRect;
            tf.delegate = self;
            [_aView addSubview:tf];
            [tf release];
        }
        return _aView;
    }
    //bView的get方法
    - (UIView *)bView
    {
        if (_bView == nil)
        {
            _bView = [[UIView alloc] initWithFrame:CGRectMake(30, 150, 300, 500)];
            _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
            _imageView.frame = CGRectMake(0, 0, 300, 500);
            _imageView.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView)];
            tap.numberOfTapsRequired = 1;
            tap.numberOfTouchesRequired = 1;    
            [_imageView addGestureRecognizer:tap];
            [_bView addSubview:_imageView];
            [_imageView release];
            [tap release];
        }
        return _bView;
    }
    - (void)click:(UISegmentedControl *)seg
    {
        switch (seg.selectedSegmentIndex) {
            case 0:
                //防止重复添加的代码。
                for (UIView *view in self.view.subviews) {
    //如果子类视图中的tag不等1001就代表有bView在上面,然后释放并添加aView,也方便多分段来显示
                    if (view.tag != 1001) {
                        [view removeFromSuperview];
                    }
                }
                [self.view addSubview:self.aView];
                break;
            case 1:
                for (UIView *view in self.view.subviews) {
                    if (view.tag != 1001) {
                        [view removeFromSuperview];
                    }
                }
                [self.view addSubview:self.bView];
                break;
            default:
                break;
        }
    }
    - (void)viewDidLoad {
        [super viewDidLoad];    
        //创建一个分段控件
        UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"男",@"女",@"伪娘",@"人妖",@"其他"]];//这个初始化方法是分段控件特有的一个初始化方法。    
        seg.tag = 1001;    
        seg.frame = CGRectMake(30, 80, 300, 40);//设置它的大小和位置
        seg.selectedSegmentIndex = 2;//设置刚开始显示时,在哪个segment上面
        seg.tintColor = [UIColor redColor];//设置主题颜色,包含边框以及每个segment显示的文本颜色
        [seg addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];//添加点击方法
        [self.view addSubview:seg];//添加显示
        [seg release];//释放内存
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
    }
    @end
    

    UISlider

    UISlider是iOS中的滑块控件
    minimumValue //设置滑块的最小值
    maximumValue //设置滑块的最⼤值
    value //设置滑块的当前值 minimumTrackTinkColor //定义划过区域的颜色
    addTarget: action: forControlEvents: 给UISlider添加事件, controlEvent为UIControlEventValueChanged。
    [slider thumbImageForState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"o2"] forState:UIControlStateNormal];//设置了普通状态和高亮状态的滑轮样式
    slider.minimumTrackTintColor = [UIColor redColor]; //划过的线的颜色
    slider.maximumTrackTintColor = [UIColor whiteColor];//没划过的线的颜色
    //竖向显示 slider 使用transform属性 旋转一定弧度 旋转九十度 pi代表180度
    slider.transform = CGAffineTransformMakeRotation(M_PI * 0.5);

    //根视图控制器中建
    @property (nonatomic, retain) UIImageView *imageView;
    
    //.m文件
    @implementation RootViewController
    - (void)voiceChange:(UISlider *)slider
    {
        NSLog(@"%.2f",slider.value);
        [_imageView stopAnimating];
        _imageView.animationDuration = slider.value;
        [_imageView startAnimating];
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        //创建一个滑块对象
        UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(30, 80, 300, 100)];    
        slider.backgroundColor = [UIColor blueColor];    
        //设置它的最小值
        slider.minimumValue = 0;
        //设置它的最大值
        slider.maximumValue = 2;
        //设置它初始位置在中间
    //    slider.value = (slider.minimumValue + slider.maximumValue)     
        //设置划过区域的颜色为红色
        slider.minimumTrackTintColor = [UIColor redColor];
        [slider setThumbImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
        
        //设置球的颜色
        slider.thumbTintColor = [UIColor yellowColor];
        [slider addTarget:self action:@selector(voiceChange:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:slider];
        [slider release];
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(30, 150, 166, 144)];
        
        NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:22];
        
        for (int i = 0; i < 22; i ++)
        {
            NSString *imageName = [NSString stringWithFormat:@"Zombie%d.tiff",i + 1];
            UIImage *image = [UIImage imageNamed:imageName];
            [imageArray addObject:image];
        }
        
        //指定需要做动画的图片
        _imageView.animationImages = imageArray;
        _imageView.animationDuration = 0.5;//设置播放的时间
        _imageView.animationRepeatCount = 0;//设置循环次数
        [self.view addSubview:_imageView];
        [_imageView startAnimating];//让其开始播放
        [_imageView release];
        // Do any additional setup after loading the view.
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    

    注意

        //6.3之前的版本有bug,需要先设置颜色在设置球的颜色才可以
        [slider setThumbImage:[UIImage imageNamed:@"100.jpg"] forState:UIControlStateNormal];
        //设置球的颜色
        slider.thumbTintColor = [UIColor blackColor];
        //6.31版本 bug不能用下面方法添加图片
        [slider setThumbImage:[UIImage imageNamed:@"100.jpg"] forState:UIControlStateHighlighted];
    
    On the road。。。
  • 相关阅读:
    Protobuf
    iOS保持App真后台运行
    oc之考试答题类效果
    oc之脚本
    IOS
    Xcode中控制台中打印中文处理
    iOS-拍照后裁剪,不可拖动照片的问题
    iOS开发之一句代码检测APP版本的更新
    AVAudioSesion和AVAudioPlayer的基本使用
    GCD使用dispatch_semaphore_t创建多线程网络同步请求
  • 原文地址:https://www.cnblogs.com/ianhao/p/4461090.html
Copyright © 2020-2023  润新知