本文主要是介绍UI 第六节 uisegmentedControl uiimage uislider 课后习题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
作业1:使⽤UISegmentedControl切换登录界⾯、注册界面,找回密码界 面。
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, retain)UISegmentedControl *segment;
@end
@implementation ViewController
-(void)dealloc
{
[super dealloc];
[_segment release];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadSegment];
}
// 自定义方法
-(void)loadSegment
{
self.segment = [[UISegmentedControl alloc]initWithItems:@[@"登录", @"注册", @"忘记密码"]];
self.segment.frame = CGRectMake(10, 30, self.view.frame.size.width - 20, 30);
[self.segment addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_segment];
[self.segment release];
}
-(void)change:(UISegmentedControl *)sender
{
//判断 把没有sender的界面全部移除
for(UIView *aview in self.view.subviews)
if (aview != sender) {
[aview removeFromSuperview];
}
switch (sender.selectedSegmentIndex) {
case 0:
[self denglu];
break;
case 1:
[self zhuce];
break;
case 2:
[self forgetPassWord];
break;
default:
break;
}
}
-(void)denglu
{
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, self.view.frame.size.width - 40, 30)];
lab.text = @"用户名";
UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 180, self.view.frame.size.width - 40, 30)];
lab1.text = @"密码";
UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(40, 240, self.view.frame.size.width - 80, 30)];
[but setTitle:@"登 录" forState:UIControlStateNormal];
but.backgroundColor = [UIColor cyanColor];
UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(lab.frame)+80, CGRectGetMinY(lab.frame), 150, 30)];
field.placeholder = @"请输入用户名";
field.borderStyle = UITextBorderStyleRoundedRect;
UITextField *field1 = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(lab.frame)+80, CGRectGetMinY(lab1.frame), 150, 30)];
field1.placeholder = @"密码";
field1.secureTextEntry = YES;
field1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:lab];
[self.view addSubview:lab1];
[self.view addSubview:but];
[self.view addSubview:field];
[self.view addSubview:field1];
[lab release];
[lab1 release];
[but release];
[field release];
[field1 release];
}
-(void)zhuce
{
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, self.view.frame.size.width - 40, 30)];
lab.text = @"注册";
UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 180, self.view.frame.size.width - 40, 30)];
lab1.text = @"设置密码";
UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(40, 240, self.view.frame.size.width - 80, 30)];
[but setTitle:@"注 册 完 成" forState:UIControlStateNormal];
but.backgroundColor = [UIColor cyanColor];
UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(lab.frame)+80, CGRectGetMinY(lab.frame), 150, 30)];
field.placeholder = @"请输入用户名";
field.borderStyle = UITextBorderStyleRoundedRect;
UITextField *field1 = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(lab.frame)+80, CGRectGetMinY(lab1.frame), 150, 30)];
field1.placeholder = @"密码";
field1.secureTextEntry = YES;
field1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:lab];
[self.view addSubview:lab1];
[self.view addSubview:but];
[self.view addSubview:field];
[self.view addSubview:field1];
[lab release];
[lab1 release];
[but release];
[field release];
[field1 release];
}
-(void)forgetPassWord
{
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(20, 120, self.view.frame.size.width - 40, 30)];
lab.text = @"注册用手机/邮箱";
UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 180, self.view.frame.size.width - 40, 30)];
lab1.text = @"验证码";
UIButton *but = [[UIButton alloc]initWithFrame:CGRectMake(40, 240, self.view.frame.size.width - 80, 30)];
[but setTitle:@"确定找回" forState:UIControlStateNormal];
but.backgroundColor = [UIColor cyanColor];
UITextField *field = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(lab.frame)+150, CGRectGetMinY(lab.frame), 150, 30)];
field.placeholder = @"手机号码/邮箱";
field.borderStyle = UITextBorderStyleRoundedRect;
UITextField *field1 = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(lab.frame)+150, CGRectGetMinY(lab1.frame), 150, 30)];
field1.placeholder = @"验证码";
field1.secureTextEntry = YES;
field1.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:lab];
[self.view addSubview:lab1];
[self.view addSubview:but];
[self.view addSubview:field];
[self.view addSubview:field1];
[lab release];
[lab1 release];
[but release];
[field release];
[field1 release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
作业2:
创建3个UISlider对象。⽤用这3个UISlider对象控制 self.view.backgroundColor的变化。 3个UISlider对象分别控制UIColor的red,green,blue。 滑动任何⼀一个UISlider都会引起背景颜⾊色变化。
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic, retain)UISlider *s1;
@property(nonatomic, retain)UISlider *s2;
@property(nonatomic, retain)UISlider *s3;
@property(nonatomic, retain)UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self loadSlider];
[self loadNewView];
self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}
-(void)dealloc
{
[super dealloc];
[self.s1 release];
[self.s2 release];
[self.s3 release];
[self.imageView release];
}
//设置属性,添加s1,s2,s3的三种value 取值范围在0~1
-(void)loadSlider
{
self.s1 = [[UISlider alloc]initWithFrame:CGRectMake(40, 50, self.view.frame.size.width - 80, 30)];
self.s1.minimumTrackTintColor = [UIColor redColor];
[self.s1 setThumbImage:[UIImage imageNamed:@"iconfont-shuxian"] forState:UIControlStateNormal];
self.s1.thumbTintColor = [UIColor redColor];
self.s1.minimumValue = 0.0f;
self.s2.maximumValue = 1.0f;
[self.view addSubview:_s1];
[self.s1 release];
self.s2 = [[UISlider alloc]initWithFrame:CGRectMake(40, 80, self.view.frame.size.width - 80, 30)];
self.s2.minimumTrackTintColor = [UIColor greenColor];
[self.s2 setThumbImage:[UIImage imageNamed:@"iconfont-shuxian"] forState:UIControlStateNormal];
self.s2.thumbTintColor = [UIColor greenColor];
self.s2.minimumValue = 0.0f;
self.s3.maximumValue = 1.0f;
[self.view addSubview:_s2];
[self.s2 release];
self.s3 = [[UISlider alloc]initWithFrame:CGRectMake(40,110, self.view.frame.size.width - 80, 30)];
self.s3.minimumTrackTintColor = [UIColor blueColor];
[self.s3 setThumbImage:[UIImage imageNamed:@"iconfont-shuxian"] forState:UIControlStateNormal];
self.s3.thumbTintColor = [UIColor blueColor];
self.s3.minimumValue = 0.0f;
self.s3.maximumValue = 1.0f;
[self.view addSubview:_s3];
[self.s3 release];
[self.s1 addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
[self.s2 addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
[self.s3 addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
}
//圆形显示动态图
-(void)loadNewView
{
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(40, 200, 300, 300)];
self.imageView.backgroundColor = [UIColor whiteColor];
self.imageView.layer.masksToBounds = YES;
self.imageView.layer.cornerRadius = 150;
NSMutableArray *imgarr = [NSMutableArray array];
for (int i = 1 ; i < 9 ; i++) {
NSString *imgStr = [NSString stringWithFormat:@"abc_%d.tiff",i];
//添加的是图片 不是图片名字(崩溃的敌方敌方)
UIImage *img = [UIImage imageNamed:imgStr];
[imgarr addObject:img];
}
self.imageView.animationImages = imgarr;
[self.imageView startAnimating];
[self.view addSubview:_imageView];
[self.imageView release];
}
-(void)change:(UISlider *)sender
{
//设置三原色混合
self.view.backgroundColor = [UIColor colorWithRed:_s1.value green:_s2.value blue:_s3.value alpha:1];
self.imageView.animationDuration = sender.value;
[self.imageView startAnimating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这篇关于UI 第六节 uisegmentedControl uiimage uislider 课后习题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!