iOS学习笔记——第三天

iOS学习笔记——第三天

UIButton

按钮既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置。

UIButton的状态

normal(普通状态)

  • 默认情况(default)
  • 对应的枚举常量:UIControlStateNormal

highlighted(高亮状态)

  • 按钮被按下去的时候(手指还未松开)
  • 对应的枚举常量:UIControlStateHighlighted

disabled

  • 如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击
  • 对应的枚举常量:UIControlStateDisabled
UIButton的常见设置

-(void)setTitle:(nullable NSString *)title forState:(UIControlState)state;
设置按钮的文字

-(void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state;
设置按钮的文字颜色

-(void)setImage:(nullable UIImage *)image forState:(UIControlState)state;
设置按钮内部的小图片

-(void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state;
设置按钮的背景颜色

代码如下:

//1.1 创建按钮对象
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //1.2 设置按钮尺寸
    button.frame = CGRectMake(100, 300, 300, 100);
    //1.3 设置按钮背景颜色
    button.backgroundColor = [UIColor purpleColor];
    //1.4 设置按钮文字
    [button setTitle:@"普通按钮" forState:UIControlStateNormal];
    [button setTitle:@"高亮按钮" forState:UIControlStateHighlighted];
    //1.5 设置文字颜色
    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    //1.6 设置内容图片
    [button setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateHighlighted];
    //1.7 设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"6"] forState:UIControlStateHighlighted];
    //2.0 添加到控制器的view中
    [self.view addSubview:button];
一个简单的购物车实例

需要在购物车中添加/删除物品,并能够提示购物车已满/为空,大致界面如下图。
在这里插入图片描述
代码为

#import "ViewController.h"

@interface ViewController ()

//购物车
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
//添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
//删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    //裁剪多余部分(内存依旧还有)
//    self.imageView.clipsToBounds = YES;
}
- (IBAction)addProducts:(UIButton *)button {
    /****************定义一些常量*********************/
    //1.总列数
    NSInteger allCols = 3;
    //2.商品的宽度和高度
    CGFloat width = 80;
    CGFloat height = 100;
    //3.求出水平间距和垂直间距
    CGFloat hMargin = (self.imageView.frame.size.width - allCols * width) / 2;
    CGFloat vMargin = (self.imageView.frame.size.height - 2 * height) / 1;
    //4.设置索引
    NSInteger index = self.imageView.subviews.count;
    //4.求出x值
    CGFloat x = (hMargin + width) * (index % allCols);
    CGFloat y = (vMargin + height) * (index / allCols);
    
    /****************创建一个商品*********************/
    //1.0 创建一个imageview
    UIImageView *imageView1 = [[UIImageView alloc] init];
    //1.1 设置尺寸
    imageView1.frame = CGRectMake(x, y, width, height);
    //1.2 设置背景颜色
    imageView1.backgroundColor = [UIColor purpleColor];
    //1.3 设置内容图片
    [imageView1 setImage:[UIImage imageNamed:@"4"]];
    //2.0 添加到imageView
    [self.imageView addSubview:imageView1];
    
    /****************设置按钮状态*********************/
//    if(index == 5)
//    {
//        button.enabled = NO;
//    }
    button.enabled = (index !=5);
    //3.0 设置删除按钮状态
    self.removeButton.enabled = YES;
    //下标加一
    index += 1;
}

- (IBAction)removeProducts:(UIButton *)button {
    //1. 删除最后一个商品
    UIView * lastimageView1 = [self.imageView.subviews lastObject];
    [lastimageView1 removeFromSuperview];
    //2. 设置添加按钮状态
    self.addButton.enabled = YES;
    //3.设置删除按钮状态
    if(self.imageView.subviews.count ==0)
    {
        self.removeButton.enabled = NO;
    }

}


@end

成果为
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值