IOS开发-基础篇-Three

UI基础-第三天

人生就像卫生纸,没事的时候尽量少扯;时间就像卫生纸,看着挺多,用着挺少…

UIButton

什么是按钮

.这是一个非常重要的UI控件,中文“按钮”
.一般情况下,能够做出反应的控件都是按钮
.按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置
如图所示

UIButton的状态

.normal(普通状态)

normal

默认情况(Default)

对应的枚举常量:UIControlStateNormal

.highlighted(高亮状态)

highlighted

按钮按下去的时候(手指还未松开)

对应的枚举常量:UIControlStateHighlighted

.disabled(失效状态,不可用状态)

disabled


如果enabled属性为NO,就是处于disable状态,嗲表按钮不可以被点击

对应的枚举常量:UIControlStateDisabled

设置按钮的背景图片

设置按钮的背景图片

按钮的样式

样式

.在用代码创建按钮的同时指定按钮样式

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

UIButtonTypeCustom:无类型,按钮的内容需要自定义

UIButtonTypeDetailDisclosure:

UIButtonTypeInfoLight:

UIButtonTypeInfoDark:

UIButtonTypeContactAdd:

UIButton的常见设置

. -(void)setTitle:(NSString *)title forState:(UIConttrolState)state;

设置按钮的文字

. -(void)setTitleColor:(UIColor *)color forState:(UIControlState) state;

设置按钮的文字颜色

. -(void)setImage:(UIImage *)image forState:(UIControlState)state;

设置按钮内部的小图片

. -(void)setBackgroundImage:(UIImage *)image
forState:(UIControlState)state;

设置按钮的背景图片

.设置按钮的文字字体(需要拿到按钮内部的label来设置)

btn.titleLabel.font = [UIFont systemFontOfSize:13];

. -(NSString *)titleForState:(UIControlState)state;

获得按钮的文字

. -(UIColor *)titleColorForState:(UICotrolState)state;

获得按钮的文字颜色

.-(UIImage *)imageForState:(UIControlState)state;获得按钮内部的小图片

.-(UIImage *)backgroundImageForState:(UIControlState)state;

获得按钮的背景图片

UIButton、UIImageView、UILabel的选择

.UIButton

.特点

既能显示文字,又能显示图片(能显示2张图片,背景图片、内容图片)

长按高亮的时候可以切换图片、文字

直接通过addTarget…方法监听点击

.UIImageView

能显示图片,不能直接通过addTarget…方法监听点击

.UILabel

能显示文字,不能直接通过addTarget…方法监听点击

.选择

.仅仅是显示数据,不需要点击

建议选择UIImageView、UILabel

.不仅显示数据,还需要监听点击

建议选择UIButton

其实UIImageView、UILabel也可以通过手势识别器来监听

.长按控件后,会改变显示的内容

不用考虑了,选择UIButton(因为UIButton有highlighted这种状态)

.同时显示2张图片:背景图片、内容图片

不用考虑了,选择UIButton

Storyboard到代码的转换

显示

九宫格计算思路

九宫格

九宫格

九宫格

查看UI界面的结构

查看UI界面的结构

UIButton在storyboard中的使用

使用

UIButton在代码中的使用
   //1.创建按钮对象
    //UIButton *button = [[UIButton alloc]init];
    //注意:设置按钮的类型只可以在初始化的时候设置 -> UIButtonTypeCustom
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    //2.设置按钮的了恶行,这样是行不通的(不可以这么写)
//    button.buttonType = UIButtonTypeCustom;

    //3.设置frame
    button.frame = CGRectMake(100, 100, 170, 60);

    //4.设置背景颜色
//    button.backgroundColor = [UIColor redColor];
//    [button setBackgroundColor:[UIColor redColor]];

    //5.设置文字
    //分状态栏的:
    [button setTitle:@"普通按钮" forState:UIControlStateNormal];
    [button setTitle:@"高亮按钮" forState:UIControlStateHighlighted];

    //6.设置文字的颜色
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];

    //7.设置文字的阴影颜色
    [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

    //设置内容图片
    [button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"player_btn_pause_highlight"] forState:UIControlStateHighlighted];

    //设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];

    //将按钮添加到控制器的view中
    [self.view addSubview:button];

    // 非常重要
    /**
     *  监听按钮的点击
     *  Target: 目标 (让谁做事情)
     *  action: 方法 (做什么事情-->方法)
     *  Events: 事件
     */
    //    SEL sel = @selector(clickButton:);
    [button addTarget:self action:@selector(demo:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)demo:(UIButton *)btn{
    NSLog(@"%@",btn);
}
综合实例界面搭建

Mainstoryboard


#import "ViewController.h"

@interface ViewController ()
//添加按钮
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
//删除按钮
@property (weak, nonatomic) IBOutlet UIButton *removeBtn;
//购物车
@property (weak, nonatomic) IBOutlet UIView *MainView;
//全局的下标
//@property (nonatomic,assign)NSInteger index;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //给下标复制
//    self.index = 0;
//    //裁剪多余的部分(不可取)
//    self.MainView.clipsToBounds = YES;
}
//添加到购物车
- (IBAction)addSomeThing:(UIButton *)sender {
/*********************1.定义一些常量***********************************/
    //1.总列数
    NSInteger allCols = 3;
    //2.商品的宽度和高度
    CGFloat width = 80;
    CGFloat height = 100;
    //3.求出水平间距和垂直间距
    CGFloat hMargin = (self.MainView.bounds.size.width - allCols * width) / (allCols - 1);
    CGFloat vMargin = (self.MainView.bounds.size.height - 2 * height)/1;
    //4.设置索引
    NSInteger index = self.MainView.subviews.count;
    //5.求出x,y值
    CGFloat x = (hMargin + width) * (index % allCols);
    CGFloat y = (vMargin + height) * (index / allCols);

/*********************2.创建一个商品***********************************/
    //1.创建商品的vie
    UIView *shopView = [[UIView alloc]init];
    //2.设置frame
    shopView.frame = CGRectMake(x, y, width, height);
    //3.设置背景颜色
    shopView.backgroundColor = [UIColor purpleColor];
    //4.添加到购物车
    [self.MainView addSubview:shopView];
/*********************3.设置按钮的状态***********************************/
//    if (index == 5) {
//        sender.enabled = NO;
//    }
    sender.enabled = (index != 5);
    //设置删除按钮的状态
    self.removeBtn.enabled = YES;
    //让下标+1
//    self.index +=1;
}
//从购物车中删除
- (IBAction)removeSomeThing:(UIButton *)sender {
    //1.删除最后一个商品
    UIView *lastShopView = [self.MainView.subviews lastObject];
    [lastShopView removeFromSuperview];

    //设置索引值
    //    self.index -= 1;
    //3.设置添加按钮的砖头
    self.addBtn.enabled = YES;

    //4.设置删除按钮的状态
    //    if (self.MainView.subviews.count == 0) {
    //        self.removeBtn.enabled = NO;
    //    }
    self.removeBtn.enabled = (self.MainView.subviews.count != 0);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值