码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 之 UIcontrolAndSubclass

时间:2014-12-11 00:09:54      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:des   cWeb   style   blog   http   io   ar   color   os   

      先告诉大家一个好消息, 今天我的博客正式开通了,欢迎大家的光临.

  今天我们学习了UIControl以及他的一些子类并且也学习了一些常用的方法其中他的子类有UIButton、UIDatePicker、UIPageControl、UISegmentedControl、UITextField、UISlider、UISwitch。其中今天我们学习的有UISegmentedControl、UISlider和UISwitch。下面我就简单地用几行代码来说明其用途,如果有疑问或者问题请大家不吝赐教:

1.UIcontrol:UIView的子类,是所有控制控件的基类

    control = [[UIControl alloc] initWithFrame:CGRectMake(50, 350, 200, 200)];
    control.backgroundColor = [UIColor blackColor];
    [control addTarget:self action:@selector(control:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:control];
    [control release];

 2.下面是UIslider的例子

slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, 220, 31)];
    NSLog(@"%@", slider);
    slider.backgroundColor = [UIColor cyanColor];
    [slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    //默认的value取值范围时0-1;
    //如果最小值比最大值大,slider不能滑动
    slider.minimumValue = 0;
    slider.maximumValue = 255;
    [self.view addSubview:slider];
    [slider release];

    
    slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 151, 220, 31)];
    NSLog(@"%@", slider);
    slider1.backgroundColor = [UIColor cyanColor];
    [slider1 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    slider1.minimumValue = 0;
    slider1.maximumValue = 255;
    [self.view addSubview:slider1];
    [slider1 release];
    
    slider2 = [[UISlider alloc] initWithFrame:CGRectMake(0, 200, 220, 31)];
    NSLog(@"%@", slider);
    slider2.backgroundColor = [UIColor cyanColor];
    [slider2 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    slider2.minimumValue = 0;
    slider2.maximumValue = 255;
    [self.view addSubview:slider2];
    [slider2 release];

3.下面我们来做个练习:通过改变UISlider进而用UILabel来显示其示数,并由此来随机改变UIControl区域的颜色

 

- (void)viewDidLoad
{
    [super viewDidLoad];
    //UIControl:UIView的子类,是所有控制控件的基类
    control = [[UIControl alloc] initWithFrame:CGRectMake(50, 350, 200, 200)];
    control.backgroundColor = [UIColor blackColor];
    [control addTarget:self action:@selector(control:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:control];
    [control release];
    
    //UISegmentedControl:分段控制器
    //注意点
    //1.items用于segmentControl的文本显示,所以items数组中的元素必须是NSString的类型或UIImage类型的
    //2.分段的下标从0开始
    //设置某个分段的宽度,如果没有被设置宽度,每个分段宽度相等
    //    [segment setWidth:200 forSegmentAtIndex:1];
    NSArray *array = @[@"", @""];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:array];
    segment.frame = CGRectMake(0, 40, 320, 40);
    [segment addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventValueChanged];
    segment.tintColor = [UIColor blackColor];
    //设置分段控制器的初始选中值(不会触发关联的方法)
    segment.selectedSegmentIndex = 0;
    //设置分段的图片
    UIImage *image = [UIImage imageNamed:@"1"];
    [segment setImage:image forSegmentAtIndex:0];
    [self.view addSubview:segment];
    [segment release];
    
    //UISlider:滑块控件,继承于UIControl
    //slider:控件的高度是31pt,不能改变
    //frame:slider的可触摸区域
    label = [[UILabel alloc] initWithFrame:CGRectMake(240, 100, 70, 31)];
    label.textColor = [UIColor blackColor];
    label.text = @"0";
    [self.view addSubview:label];
    [label release];
    
    label1 = [[UILabel alloc] initWithFrame:CGRectMake(240, 151, 70, 31)];
    label1.textColor = [UIColor blackColor];
    label1.text = @"0";
    [self.view addSubview:label1];
    [label1 release];
    
    label2 = [[UILabel alloc] initWithFrame:CGRectMake(240, 200, 70, 31)];
    label2.textColor = [UIColor blackColor];
    label2.text = @"0";
    [self.view addSubview:label2];
    [label2 release];
    
    slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, 220, 31)];
    NSLog(@"%@", slider);
    slider.backgroundColor = [UIColor cyanColor];
    [slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    //默认的value取值范围时0-1;
    //如果最小值比最大值大,slider不能滑动
    slider.minimumValue = 0;
    slider.maximumValue = 255;
    [self.view addSubview:slider];
    [slider release];

    
    slider1 = [[UISlider alloc] initWithFrame:CGRectMake(0, 151, 220, 31)];
    NSLog(@"%@", slider);
    slider1.backgroundColor = [UIColor cyanColor];
    [slider1 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    slider1.minimumValue = 0;
    slider1.maximumValue = 255;
    [self.view addSubview:slider1];
    [slider1 release];
    
    slider2 = [[UISlider alloc] initWithFrame:CGRectMake(0, 200, 220, 31)];
    NSLog(@"%@", slider);
    slider2.backgroundColor = [UIColor cyanColor];
    [slider2 addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
    slider2.minimumValue = 0;
    slider2.maximumValue = 255;
    [self.view addSubview:slider2];
    [slider2 release];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)changeTitle:(UISegmentedControl *)title
{
    if (title.selectedSegmentIndex == 0) {
        [title setTitle:@"" forSegmentAtIndex:0];
        [title setTitle:@"" forSegmentAtIndex:1];
    } else {
        [title setTitle:@"" forSegmentAtIndex:1];
        [title setTitle:@"" forSegmentAtIndex:0];
    }
}

- (void)valueChange:(UISlider *)value
{
    NSLog(@"%f", value.value);
    if (value == slider) {
        label.text = [NSString stringWithFormat:@"%.2f", value.value];
    } else if (value == slider1) {
        label1.text = [NSString stringWithFormat:@"%.2f", value.value];
    } else {
        label2.text = [NSString stringWithFormat:@"%.2f", value.value];
    }
    control.backgroundColor = [UIColor colorWithRed:[label.text integerValue] / 255. green:[label1.text integerValue] / 255. blue:[label2.text integerValue] / 255. alpha:1.0];

}
- (void)control:(UIControl *)control1
{
    float a = arc4random() % 256;
    float b = arc4random() % 256;
    float c = arc4random() % 256;
    control1.backgroundColor = [UIColor colorWithRed:a / 255.0 green:b / 255.0 blue:c / 255.0 alpha:1.0];
    label.text = [NSString stringWithFormat:@"%.2f", a];
    label1.text = [NSString stringWithFormat:@"%.2f", b];
    label2.text = [NSString stringWithFormat:@"%.2f", c];
//    slider.value = [label.text floatValue];
//    slider1.value = [label1.text floatValue];
//    slider2.value = [label2.text floatValue];
    
    //设置slider的value,并且可以指定是否加载动画,让整个过程更自然
    [slider setValue:a animated:YES];
    [slider1 setValue:b animated:YES];
    [slider2 setValue:c animated:YES];
}

bubuko.com,布布扣

 

这个便是上面实现的结果,有什么问题希望大家提出哈。

 

4.对于UISwitch我们今天并没有学习多少,老师只是简单地提了下,下面就和大家分享下其具体做法

//UISwitch,开关控件,继承于UIControl
    //UISwitch,有固定的大小,宽51,高31
    //设置Switch的状态
    UISwitch *swichControl = [[UISwitch alloc] initWithFrame:CGRectMake(40, 40, 100, 100)];
    swichControl.on = YES;
    [swichControl addTarget:self action:@selector(changeCondition:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:swichControl];
    [swichControl release];

以上便是今天上课的主要内容了,希望可以和大家共同进步,一起学习。

下面附上今天的作业练习:

1.使?用UISegmentedControl切换登录界?面、注册界?面,找回密码界
?面。
注:UISegmentedControl放在屏幕最上?方,登录、注册、找回密码 界?面紧接着UISegmentedControl下?方显?示。

- (void)viewDidLoad
{
    [super viewDidLoad];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"登陆界面", @"注册界面", @"找回密码界面"]];
    segment.frame = CGRectMake(0, 20, 320, 30);
    [segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segment];
    [segment release];
}

- (void)change:(UISegmentedControl *)segmented
{
    for (UIView *view in self.view.subviews) {
        NSLog(@"%@", view);
        NSLog(@"%@", segmented);
        if (view != segmented) {
            
            [view removeFromSuperview];
        }
    }
    switch (segmented.selectedSegmentIndex) {
        case 0:
        {
            pass *password = [[pass alloc] init];
            [self.view addSubview:password];
            [password release];
            break;
        }
        case 1:
        {
            zhuce *zhu = [[zhuce alloc] init];
            [self.view addSubview:zhu];
            [zhu release];
            break;
        }
        case 2:
        {
            findPassWord *find = [[findPassWord alloc] init];
            [self.view addSubview:find];
            [find release];
        }
        default:
            break;
    }
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 50, 320, 568);
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 70, 60, 30)];
        label.text = @"用户名";
        label.backgroundColor = [UIColor redColor];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentRight;
        [self addSubview:label];
        [label release];
        
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(120, 70, 180, 30)];
        field.borderStyle = UITextBorderStyleRoundedRect;
        field.keyboardType = UIKeyboardTypeNumberPad;
        field.placeholder = @"请输入用户名或邮箱";
        field.delegate = self;
        [self addSubview:field];
        [field release];
        
        //设置密码
        UILabel *passWord = [[UILabel alloc] initWithFrame:CGRectMake(40, 120, 60, 30)];
        passWord.text = @"密码";
        passWord.backgroundColor = [UIColor redColor];
        passWord.textColor = [UIColor blackColor];
        passWord.textAlignment = NSTextAlignmentRight;
        [self addSubview:passWord];
        [passWord release];
        
        UITextField *pass = [[UITextField alloc] initWithFrame:CGRectMake(120, 120, 180, 30)];
        pass.placeholder = @"请输入密码";
        pass.secureTextEntry = YES;
        pass.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        pass.borderStyle = UITextBorderStyleRoundedRect;
        pass.delegate = self;
        [self addSubview:pass];
        [pass release];
        
        //设置登陆按钮
        UIImage *image = [UIImage imageNamed:@"1"];
        UIButton *loading = [UIButton buttonWithType:UIButtonTypeCustom];
        loading.frame = CGRectMake(60, 180, 70, 30);
        [loading setBackgroundImage:image forState:UIControlStateNormal];
        [loading setTitle:@"登陆" forState:UIControlStateNormal];
        [loading setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:loading];
        
        UIButton *cencle = [UIButton buttonWithType:UIButtonTypeCustom];
        cencle.frame = CGRectMake(200, 180, 70, 30);
        [cencle setBackgroundImage:image forState:UIControlStateNormal];
        [cencle setTitle:@"取消" forState:UIControlStateNormal];
        [cencle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self addSubview:cencle];

    }
    return self;
}
#import "zhuce.h"

@implementation zhuce

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(0, 50, 320, 568);
        // Initialization code
        NSArray *array = @[@"邮箱", @"请输入邮箱", @"地址", @"请输入地址", @"电话", @"请输入电话", @"账号", @"请输入账号", @"密码", @"请输入密码", @"确认密码", @"请再次输入密码"];
        int a = 0;
        int b = 0;
        for (int i = 0; i < 6; i++) {
            UILabel *emailLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 50 + i * 50, 70, 30)];
            emailLabel.text = array[b];
            emailLabel.textAlignment = NSTextAlignmentRight;
            [self addSubview:emailLabel];
            [emailLabel release];
            
            UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(110, 50 + 50 * i, 160, 30)];
            field.placeholder = array[b + 1];
            field.borderStyle = UITextBorderStyleRoundedRect;
            [self addSubview:field];
            [field release];
            field.delegate = self;
            a++;
            b = a * 2;
            
        }
        UIButton *password = [UIButton buttonWithType:UIButtonTypeSystem];
        [password setTitle:@"登陆" forState:UIControlStateNormal];
        password.frame = CGRectMake(60, 350, 60, 30);
        [self addSubview:password];
        
        UIButton *cancel = [UIButton buttonWithType:UIButtonTypeSystem];
        [cancel setTitle:@"登陆" forState:UIControlStateNormal];
        cancel.frame = CGRectMake(180, 350, 60, 30);
        [self addSubview:cancel];

    }
    return self;
}
#import "findPassWord.h"

@implementation findPassWord

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.frame = CGRectMake(0, 50, 320, 568);
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(30, 60, 50, 30)];
        label.text = @"邮箱";
        [self addSubview:label];
        [label release];
        
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(100, 60, 200, 30)];
        field.placeholder = @"请输入邮箱或电话";
        field.borderStyle = UITextBorderStyleRoundedRect;
        field.delegate = self;
        [self addSubview:field];
        [field release];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(100, 120, 60, 30);
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [self addSubview:button];
        
        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
        button1.frame = CGRectMake(160, 120, 60, 30);
        [button1 setTitle:@"取消" forState:UIControlStateNormal];
        [self addSubview:button1];

    }
    return self;
}

效果图为:

bubuko.com,布布扣

以上图片实现了简单窗口之间的切换。

      对了今天还讲了一个简单地动画制作,在制作过程中,首先要找到素材,也就是简单的几张图片,将其放到Xcode中去利用UISwitch进行图片的连续切换,从而实现动画,下面开始进入代码部分:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    beginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    beginButton.frame = CGRectMake(70, 500, 60, 30);
    [beginButton setTitle:@"开始" forState:UIControlStateNormal];
    [beginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [beginButton addTarget:self action:@selector(begin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:beginButton];
    
    stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
    stopButton.frame = CGRectMake(180, 500, 60, 30);
    [stopButton setTitle:@"停止" forState:UIControlStateNormal];
    [stopButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [stopButton addTarget:self action:@selector(begin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopButton];
    
    label = [[UILabel alloc] initWithFrame:CGRectMake(70, 450, 300, 30)];
    [self.view addSubview:label];
    [label release];
   
    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 400, 300, 31)];
    slider.backgroundColor = [UIColor blueColor];
    [slider addTarget:self action:@selector(chang:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    [slider release];
    
    
    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"huoju_1.tiff"]];
    imageView.bounds = CGRectMake(0, 0, 79 * 2, 106 * 2);
    imageView.center = CGPointMake(self.view.center.x, self.view.center.y - 100);
    //imageView的动画
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 1; i < 8; i++) {
        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"huoju_%d.tiff", i]]];
    }
    //设置imageView的动画数组,数组内存放的必须是UIIMage类型的对象
    imageView.animationImages = images;
    
    //设置动画时长(图片切换的时间间隔)
    imageView.animationDuration = 1;
    
    //设置动画重复次数(0代表无限循环)
    imageView.animationRepeatCount = 0;
    
    [self.view addSubview:imageView];
    [imageView release];
    
    //开启动画
//    [imageView startAnimating];
//
//    //停止动画
//    [imageView stopAnimating];
    
    //UISwitch,开关控件,继承于UIControl
    //UISwitch,有固定的大小,宽51,高31
    //设置Switch的状态
    UISwitch *swichControl = [[UISwitch alloc] initWithFrame:CGRectMake(40, 40, 100, 100)];
    swichControl.on = YES;
    [swichControl addTarget:self action:@selector(changeCondition:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:swichControl];
    [swichControl release];
    
    
    //NSTimer
    //创建方法1.
//    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(print:) userInfo:@"asfg" repeats:YES];
    
    //创建方法2.
//    NSTimer *timer2 = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:5] interval:1 target:self selector:@selector(print:) userInfo:nil repeats:YES];
//    //每个线程(thread)中都有一个事件循环(NSRunLoop),来实时的判断是否该执行某些事件或方法
//    //加入到事件循环中,通知init方式创建的timer不能够执行,需要加入到NSRunLoop中
//    [[NSRunLoop currentRunLoop] addTimer:timer2 forMode:NSDefaultRunLoopMode];
//    [timer2 release];
//    
//    //立即执行第一次操作
//    [timer2 fire];
//    //停止计时器
//    [timer2 invalidate];
    
    //创建方法3.
    NSTimer *timer3 = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(print:) userInfo:nil repeats:YES];
//    [[NSRunLoop currentRunLoop] addTimer:timer3 forMode:NSDefaultRunLoopMode];
    //添加到主线程的事件循环
    [[NSRunLoop mainRunLoop] addTimer:timer3 forMode:NSDefaultRunLoopMode];
    
}

- (void)print:(NSTimer *)timer
{
    NSLog(@"逗逗");
}
- (void)changeCondition:(UISwitch *)condition
{
//    condition.on = YES;
    [imageView startAnimating];
}

- (void)chang:(UISlider *)change
{
    imageView.animationDuration = change.value;
    label.text = [NSString stringWithFormat:@"当前速度为%.2f秒每张", change.value];
    [imageView startAnimating];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)begin:(UIButton *)begins
{
    if (begins == beginButton) {
        [imageView startAnimating];
    } else {
        [imageView stopAnimating];
    }
}

效果图如下所示:

bubuko.com,布布扣

      好了今天就先到这里了,要回家了,望大家多多指点,不足之处尽管指出,今天好冷明天见各位,see you!

 

iOS 之 UIcontrolAndSubclass

标签:des   cWeb   style   blog   http   io   ar   color   os   

原文地址:http://www.cnblogs.com/dyx-wx/p/4156549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!