UIButton,UIImageView,UITextField

1.UIButton的基本设置

 1    //创建UIButton
 2     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
 3     button.frame = CGRectMake(150, 200, 70, 40);
 4     button.backgroundColor = [UIColor redColor];
 5     /**
 6      *  设置按钮标题
 7      *  标题内容
 8      *  按钮状态
 9      */
10    
11     [button setTitle:@"登录" forState:UIControlStateNormal];
12     //设置标题颜色
13     [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
14     //处于点击时发生高亮效果
15     [button setShowsTouchWhenHighlighted:YES];
16     //(常用)设置指定状态下的图片
17     [button setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
18    
19     //设置指定状态下前景图
20     [button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
21     //添加tag值
22     
23     /*
24      背景图片:照片的大小随着UIButton的frame大小改变
25      前景图片:照片的大小就是照片的实际大小
26      */
27     //第一种
28     //1.执行方法的对象 2.要执行的方法 3.需要触发的条件
29    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
30     
31     //第二种
32     //[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchDown];
33     //第三种,移除按钮点击事件
34     //[button removeTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchDown];
35     [self.window addSubview:button];
36 
37 //点击UIButton触发的事件
38 -(void)buttonAction
39  {
40      //通过tag值可以关联相应的视图
41      UIButton *bu1 = (UIButton *)[self.window viewWithTag:1000];
42      bu1.backgroundColor = [UIColor redColor];
43      UITextField *tex = (UITextField *)[self.window viewWithTag:1001];
44      [tex resignFirstResponder];
45      UITextField *tex1 = (UITextField *)[self.window viewWithTag:1002];
46      [tex1 resignFirstResponder];
47     
48      NSLog(@"气温要回升了");
49  }
 1 UITextField *textField1 = [[UITextField alloc] initWithFrame:CGRectMake(200, 50, 200, 40)];
 2     textField1.placeholder = @"请输入用户名";
 3     textField1.borderStyle = UITextBorderStyleRoundedRect;
 4     textField1.keyboardType = UIKeyboardTypeASCIICapable;
 5     //成为代理
 6     textField1.delegate = self;
 7     textField1.tag = 1001;
 8     [self.window addSubview:textField1];
 9 //AppDelegate.h签署代理协议
10 @interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate>
11 
12 //Appdelegate.m实现代理方法
13 //代理方法
14 -(BOOL)textFieldShouldReturn:(UITextField *)textField
15 {
16     //释放第一响应者
17     [textField resignFirstResponder];
18     return YES;
19 }
20 
21  // 点击键盘return之后,回收键盘
22     /**
23      *  1.appdelete.h签订协议
24      2.指定代理人(Appdelete)
25      3.实现协议的方法
26      */
27     /*
28      UITextField不应该在类的内部实现
29       textFieldShouldReturn,因为有的时候,点击return时,并不是想收回键盘
30      
31      对于一个V来说,自己只负责触发事件,事件由外部实现,即delegate
32      */

2.UIImageView的基本设置

 1  NSMutableArray *array1 = [NSMutableArray array];
 2     for (int i = 1; i < 19; i++) {
 3         NSString *name = [NSString stringWithFormat:@"flower%d.tiff",i];
 4         UIImage *iamge = [UIImage imageNamed:name];
 5         [array1 addObject:iamge];
 6     }
 7     UIImageView *imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(50, 420, 100, 100)];
 8     //设置一组动态图片
 9     imageView4.animationImages = array1;
10     //播放一组动态图片的时间
11     imageView4.animationDuration = 1;
12     //设置重复次数
13     imageView4.animationRepeatCount = 0;
14     //开始动画
15     [imageView4 startAnimating];
16     
17     imageView4.tag = 1000;
18     //添加到视图
19     [self.imageView addSubview:imageView4];
 1 UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(350, 50, 100, 150)];
 2     imageView1.animationImages = array4;
 3     imageView1.animationDuration = 1;
 4     imageView1.animationRepeatCount =0;
 5     [imageView1 startAnimating];
 6     [self.imageView addSubview:imageView1];
 7     
 8     //动画改变一个或者多个视图使用指定的持续时间
 9     [UIView animateWithDuration:20 animations:^{
10         imageView1.frame = CGRectMake(-150, 500, 150, 150);
11     }];

 -150表示的是imageView1最后的X的坐标是从window的原点向左偏移150,由于它自身的长为150,要想最后图片全部消失在视图上则需要X的坐标与它的长度和小于零才可以

3.UITextField的基本设置

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 @interface AppDelegate ()
 4 
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 
10 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
11     
12     self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
13     self.window.backgroundColor = [UIColor greenColor];
14         [self.window makeKeyAndVisible];
15     self.window.rootViewController = [[ViewController alloc] init];
16     
17 #pragma makr--------UITextField--------
18     
19     
20     //创建UITextField
21     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
22     //设置颜色
23     textField.backgroundColor = [UIColor whiteColor];
24     //
25     textField.alpha = 1;
26     //设置字体颜色
27     textField.textColor = [UIColor redColor];
28     //----------外观控制-----------
29       //1.边框样式
30     textField.borderStyle = UITextBorderStyleRoundedRect;
31       //2.清楚按钮
32     textField.clearButtonMode = UITextFieldViewModeAlways;
33     //-----------输入控制-----------
34       //1.默认占位字符----一旦用户输入,自行隐藏
35     textField.placeholder = @"请出入用户名";
36       //2.加密模式 - 黑点模式
37     textField.secureTextEntry = YES;
38       //3.改变键盘样式
39     textField.keyboardType = UIKeyboardTypeNumberPad;
40     [self.window addSubview:textField];
View Code
原文地址:https://www.cnblogs.com/DevinSMR/p/5158496.html