源码-02-UIView简介

 : 继承某个类;//ViewController : UIViewController

<>协议遵守,协议实现;

()类扩展->为已有的类添加方法和属性(私有);//@interface ViewController ()

(name)分类添加->为已有的类添加方法;

.ViewController.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

 

.ViewController.m文件

//
//  ViewController.m
//  02-UIView简介
//
#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@end

@implementation ViewController

// 控制器的view加载完毕时调用
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    NSLog(@"%@", self.label.superview);
//    NSLog(@"%@", self.view.subviews);
    
//    self.view.frame.origin.x == 100;
//    self.view.frame.origin.y == 90;
//    self.view.frame.size.width;
//    self.view.frame.size.height;
    
    NSLog(@"%@", NSStringFromCGRect(self.view.frame));
    
}

- (IBAction)btnClick {
    UIView *tempView = [[UIView alloc] init];
    tempView.backgroundColor = [UIColor orangeColor];
    tempView.frame = CGRectMake(275, 567, 100, 100);
    [self.view addSubview:tempView];
    
//    UISwitch *s = [[UISwitch alloc] init];
//    UIStepper *stepper = [[UIStepper alloc] init];
//    [self.view addSubview:stepper];
//    [self.view addSubview:s];
//
//    UISlider *slider = [[UISlider alloc] init];
//    [self.view insertSubview:slider belowSubview:s];
//    [self.view insertSubview:slider aboveSubview:stepper];
//
//    NSLog(@"before --- %@", self.view.subviews);
//
//
//    NSLog(@"after --- %@", self.view.subviews);
    
//    [self.label removeFromSuperview];
//    
//    [[self.view viewWithTag:99] removeFromSuperview];
}

@end

 

- (IBAction)click:(UIButton *)sender {
  //    if (sender.tag == 20) {
    //        NSLog(@"")
    //    } else if (sender.tag == 10) {
    //    
  //    }
}

 

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 @property (weak, nonatomic) IBOutlet UIView *shopsView;
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad
10 {
11     [super viewDidLoad];
12     
13     [self addButtonWithImage:@"add" highImage:@"add_highlighted" disableImage:@"add_disabled" frame:CGRectMake(30, 30, 50, 50) 
14                tag:10 action:@selector(add)];
15     [self addButtonWithImage:@"remove" highImage:@"remove_highlighted" disableImage:@"remove_disabled" frame:CGRectMake(270, 30, 50, 50) 
16                tag:20 action:@selector(remove)];
17 }
18 
19 #pragma mark 添加按钮
20 - (void)addButtonWithImage:(NSString *)image highImage:(NSString *)highImage disableImage:(NSString *)disableImage frame:(CGRect)frame 
21               tag:(NSInteger)tag action:(SEL)action
22 {
23     // 创建按钮
24     UIButton *btn = [[UIButton alloc] init];
25     // 设置背景图片
26     [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
27     [btn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
28     [btn setBackgroundImage:[UIImage imageNamed:disableImage] forState:UIControlStateDisabled];
29     // 设置位置和尺寸
30     btn.frame = frame;
31     // 监听按钮点击
32     [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
33     // 绑定tag标记
34     btn.tag = tag;
35     // 添加按钮
36     [self.view addSubview:btn];
37 }
38 
39 #pragma mark 添加
40 - (void)add
41 {
42     // 添加图片
43     UIImageView *iconView = [[UIImageView alloc] init];
44     iconView.image = [UIImage imageNamed:@"danjianbao"];
45     iconView.frame = CGRectMake(0, 0, 50, 50);
46     [self.shopsView addSubview:iconView];
47     
48     // 添加文字
49     UILabel *label = [[UILabel alloc] init];
50     label.text = @"单肩包";
51     label.frame = CGRectMake(0, 50, 50, 20);
52     label.font = [UIFont systemFontOfSize:11];
53     label.textAlignment = NSTextAlignmentCenter;
54     [self.shopsView addSubview:label];
55 }
56 
57 #pragma mark 删除
58 - (void)remove
59 {
60     NSLog(@"删除。。。。");
61 }
62 
63 - (void)click:(UIButton *)btn
64 {
65     if (btn.tag == 10) {
66         NSLog(@"添加");
67     } else {
68         NSLog(@"删除");
69     }
70 }
71 
72 @end

 

posted @ 2017-02-10 15:34  laugh  阅读(308)  评论(0编辑  收藏  举报