oc基本控件3

UIButton

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"



@interface ViewController ()


@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
   // 1 创建按钮对象
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    // 2 设置frame
    button.frame = CGRectMake(100, 100, 170, 60);
    // 设置背景颜色
    button.backgroundColor = [UIColor redColor];
    // 设置文字
    //button.titleLabel.text = @"普通文字";
    [button setTitle:@"普通文字" forState:UIControlStateNormal];
    // 高亮按钮
    [button setTitle:@"高亮文字" forState:UIControlStateHighlighted];
    
    // 设置文字颜色
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
    
    // 阴影颜色
//    [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
//
//    [button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    
    // 偏移量
    //button.titleLabel.shadowOffset = CGSizeMake(1, 2);
    
    // 设置内容图片
    [button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"player_btn_pause_highlignt"] forState:UIControlStateHighlighted];
//
//    // 设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighed"] forState:UIControlStateHighlighted];
    
    // 禁用
    //button.enabled =NO;
    // 添加到控制器view中
    [self.view addSubview:button];
    
    // 监听方法
   
    [button addTarget:self action:@selector(demo) forControlEvents:UIControlEventTouchUpInside];
}

-(void)demo{
    NSLog(@"点击了按钮");
}


@end

oc基本控件3_第1张图片

//
//  ViewController.m
//  OcDemoTest
//
//  Created by Mac on 2023/7/14.
//

#import "ViewController.h"



@interface ViewController ()
// 购物车
@property (weak, nonatomic) IBOutlet UIView *shopCarView;
// 全局下标
@property (nonatomic,assign) NSInteger index;
// 添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
// 删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeButton;

// 数据数组
@property (nonatomic,strong) NSArray *dataArr;


@end

@implementation ViewController

/**
  懒加载
 作用
 用到的时候再加载
 全局只会被加载一次
 全局都可以使用
 **/
-(NSArray *) dataArr {
    if(_dataArr == nil){
        self.dataArr = @[
            @{@"name":@"单肩包",@"icon":@"danjianbao"},
            @{@"name":@"钱包",@"icon":@"qianbao"},
            @{@"name":@"链条包",@"icon":@"liantiaobao"},
            @{@"name":@"手提包",@"icon":@"shoutibao"},
            @{@"name":@"双肩包",@"icon":@"shuangjianbao"},
            @{@"name":@"斜挎包",@"icon":@"xiekuabao"},
        
        ];
    }
    return _dataArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // 给下标赋值
    self.index = 0;
   
}
// 添加到购物车
- (IBAction)add:(UIButton *)sender {
    // 总列数
    NSInteger allCols = 3;
    // 商品的宽度
    CGFloat width = 80;
    CGFloat height = 100;
    // 求出水平间距 和垂直间距
    CGFloat hMargin = (self.shopCarView.frame.size.width - allCols *width)/(allCols -1);
    CGFloat vMargin = (self.shopCarView.frame.size.height - 2 * height)/1;
    NSInteger index = self.shopCarView.subviews.count;
    // 求出x值
    CGFloat x = (hMargin + width) * (index % allCols);
    CGFloat y = (vMargin + height) *(index/allCols);
    // 设置索引
    
    //创建商品view
    UIView *shopView = [[UIView alloc] init];
    // 设置frame
    shopView.frame = CGRectMake(x, y, 80, 100);
    // 设置背景颜色
    shopView.backgroundColor = [UIColor greenColor];
    // 添加到购物车
    [self.shopCarView addSubview:shopView];
    
    // 创建商品的UIImageView对象
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.frame = CGRectMake(0, 0, width, width);
    iconView.backgroundColor = [UIColor blueColor];
    [shopView addSubview:iconView];
    
    // 创建商品标题对象
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.frame = CGRectMake(0, width, width, height - width);
    titleLabel.backgroundColor = [UIColor yellowColor];
    titleLabel.textAlignment = NSTextAlignmentCenter; // 居中
    [shopView addSubview:titleLabel];
    
    // 设置数据(方式一)
//    iconView.image = [UIImage imageNamed:@"danjianbao"];
//    titleLabel.text = @"单肩包";
    // 方式二 (不可取,)
//    if(index == 0){
//        iconView.image = [UIImage imageNamed:@"danjianbao"];
//        titleLabel.text = @"单肩包";
//    }else if(index == 1){
//        iconView.image = [UIImage imageNamed:@"danjianbao"];
//        titleLabel.text = @"单肩包";
//    }else if(index == 1){
//        iconView.image = [UIImage imageNamed:@"danjianbao"];
//        titleLabel.text = @"单肩包";
//    }else if(index == 1){
//        iconView.image = [UIImage imageNamed:@"danjianbao"];
//        titleLabel.text = @"单肩包";
//    }
    
    // 方式三 数组
//    NSArray *imageNames = @[@"danjianbao",@"qianbao",@"liantiaobao",@"shoutibao",@"shuangjianbao",@"xiekuabao"];
//    NSArray *titleNames = @[@"单肩包",@"钱包",@"链条包",@"手提包",@"双肩包",@"斜挎包"];
//    iconView.image = [UIImage imageNamed:imageNames[index]];
//    titleLabel.text = titleNames[index];
    
    // 方式四 数组 字典
//    NSArray *dataArr = @[
//        @{@"name":@"单肩包",@"icon":@"danjianbao"},
//        @{@"name":@"钱包",@"icon":@"qianbao"},
//        @{@"name":@"链条包",@"icon":@"liantiaobao"},
//        @{@"name":@"手提包",@"icon":@"shoutibao"},
//        @{@"name":@"双肩包",@"icon":@"shuangjianbao"},
//        @{@"name":@"斜挎包",@"icon":@"xiekuabao"},
//
//    ];
    
    // 设置数据
    NSDictionary *dict = self.dataArr[index];
    iconView.image = [UIImage imageNamed:dict[@"icon"]];
    titleLabel.text = dict[@"name"];
    
    // 设置按钮的状态
    if(index == 5){
        sender.enabled = NO;
    }
    
    // 设置删除按钮的状态
    self.removeButton.enabled = YES;
    
    
}


- (IBAction)remove:(UIButton *)sender {
    // 删除最后一个商品
    UIView *lastShow = [self.shopCarView.subviews lastObject];
    [lastShow removeFromSuperview];
    
    // 设置索引值
    //self.index -= 1;
    self.addButton.enabled = YES;
    if(self.shopCarView.subviews.count == 0){
        self.removeButton.enabled = NO;
    }
    
    
}

@end

你可能感兴趣的:(oc,ui)