您的位置:首页 > 产品设计 > UI/UE

IOS开发UI—Button基础

2015-09-03 23:47 417 查看
一、简单了解

1. 按钮是运用程序中最常用的用户控件之一,UIKit使用UIButton类来表示按钮;

2. 按钮的创建方式有一些特别,它使用UIButton的buttonWithType: 类方法,并传入按钮的类型参数;

3. 一般情况下,点击某个控件后,会做出相应反应的都是按钮,钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置

4. 按钮的三种状态:

1. normal(普通状态)

默认情况(Default)

对应的枚举常量:UIControlStateNormal

2. highlighted(高亮状态)

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

对应的枚举常量:UIControlStateHighlighted

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

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

对应的枚举常量:UIControlStateDisabled

二、案例

先看视图(有点乱)



直接在控制器中来编写需要实现的功能,这里的话我是通过Main.story来拖拽控件到View中的。【注意一个视图对应一个控制器】



实现控件的方法有:

1. 点击视图中的控件,按住control键位,拖到.m文件中,设置响应的属性





2. 现在.m中写好方法,然后选中控件,拖到方法中也行,可以多个控件指向同一个方法(后面会提及,封装)

//
//  ViewController_Button.m
//  UIButton基础
//
//  Created by shumei on 15/9/1.
//  Copyright (c) 2015年 shumei. All rights reserved.
//

#import "ViewController_Button.h"

@interface ViewController_Button ()
// 上下左右按钮触发
- (IBAction)topBtn:(id)sender;
- (IBAction)rightBtn;
- (IBAction)leftBtn;
- (IBAction)downBtn;

// 放大缩小
- (IBAction)blowBtn;
- (IBAction)narrowBtn;

// 图像属性
@property (weak, nonatomic) IBOutlet UIButton *picture;

@end

@implementation ViewController_Button

- (IBAction)topBtn:(id)sender {
    NSLog(@"上");
    
    // 1.获取按钮的坐标,然后进行操作,再就是将新值赋给按钮的坐标
    // 获取原始的frame
    CGRect originFrame = self.picture.frame;
    
    // 修改Frame
    originFrame.origin.y = originFrame.origin.y - 2;
    
    // 把新的frame赋值给按钮
    self.picture.frame = originFrame;
   /**
     *  错误写法
     *  self.picture.frame.origin.y -= 2;
     *  在oc中:不允许直接修改对象的结构体属性的成员
     */ 
}

- (IBAction)rightBtn {
    NSLog(@"右");
    
    // 1.获取按钮的坐标,然后进行操作,再就是将新值赋给按钮的坐标
    
    // 获取头像的按钮坐标
    CGRect originFrame = self.picture.frame;
    
    // 进行更改
    originFrame.origin.x = originFrame.origin.x + 2;
    
    // 重新赋给frame
    self.picture.frame = originFrame;
}
- (IBAction)blowBtn {
    NSLog(@"放大");
    CGRect originFrame = self.picture.frame;
 
    originFrame.size.width += 2;
    originFrame.size.height += 2;

    self.picture.frame = originFrame;
}

- (IBAction)narrowBtn {
    NSLog(@"缩小");

    CGRect originFrame = self.picture.frame;

    originFrame.size.width -= 2;
    originFrame.size.height -= 2;
    
    // CGSize size = CGSizeMake(originFrame.size.height - 2, originFrame.size.width - 2);
    
    self.picture.frame = originFrame;
}
// 其余的两个按钮效果一样,实现方法也是一样
从上面可以知道,上下左右其实就是坐标变化而已,所以应该考虑将其封装起来

封装:

//
//  ViewController_Button.m
//  UIButton基础
//
//  Created by shumei on 15/9/1.
//  Copyright (c) 2015年 shumei. All rights reserved.
//

#import "ViewController_Button.h"

@interface ViewController_Button ()
// 图像属性
@property (weak, nonatomic) IBOutlet UIButton *picture;

#pragma mark - 封装代码
- (IBAction)move:(UIButton *)sender;

- (IBAction)scale:(UIButton *)sender;

@end

#pragma mark - 封装代码,相同部分的进行封装
// 点击上下左右的时候进行的判断
- (IBAction)move:(UIButton *)sender {
    NSLog(@"封装的代码,执行四个按钮事件!");
    
    // 获取点击的是哪一个按钮(在属性面板中已经设置了tag属性)
    // 使用sender.tag来判断用户当前点击的按钮是什么
    
    // 1.获取原始的frame
    CGRect originFrame = self.picture.frame;
    
    // 2.修改frame
    // 这里的tag在属性面板中已经进行设置了
    switch (sender.tag) {
        case 0:
            // 上
            originFrame.origin.y -= 2;
            NSLog(@"封装上");
            break;
            
        case 1:
            // 右
            originFrame.origin.x += 2;
            NSLog(@"封装右");
            break;
            
        case 2:
            // 下
            originFrame.origin.y += 2;
            NSLog(@"封装下");
            break;
            
        case 3:
            // 左
            originFrame.origin.x -= 2;
            NSLog(@"封装左");
            break;
            
        default:
            break;
    }
    
    // 3.重新赋值
    self.picture.frame = originFrame;
    
}
三、动画

1. 简易动画大致有2种方式:

1> 头尾式

[UIView beginAnimations:nil context:nil]; // 开启动画

[UIView setAnimationDuration:1]; // 设置动画执行时间

/** 需要执行动画的代码 **/

[UIView commitAnimations]; // 提交动画

2> Block式

[UIView animateWithDuration:0.5 animations:^{

/** 需要执行动画的代码 **/

}];

四、注意

1. IBAction的参数

- (IBAction)left:(UIButton *)button

(1) 在OC中,绝大多数的控件监听方法的第一个参数就是控件本身

(2) 默认连线时的参数类型是id

(3) 如果要在监听方法中,方便控件的使用,可以在连线时或者连线后,修改监听方法的参数类型

2. 修改对象的结构体成员

在OC中,不允许直接修改“对象”的“结构体属性”的“成员”,但是允许修改“对象”的“结构体属性”

修改结构体属性的成员方法如下:

(1)使用临时变量记录对象的结构体属性

(2) 修改临时变量的属性

(3)将临时变量重新设置给对象的结构体属性

3. 首尾式动画
// beginAnimations表示此后的代码要“参与到”动画中

  [UIView beginAnimations:nil context:nil];

  // setAnimationDuration用来指定动画持续时间

  [UIView setAnimationDuration:2.0];

  // 需要制定的动作

  self.headImageView.bounds = rect;

  ......

  // commitAnimations,将beginAnimation之后的所有动画提交并生成动画

  [UIView commitAnimations];
block方式:
// 方式二: block
    [UIView animateWithDuration:1.5 animations:^{
        self.iconImage.center = center;
        // self.iconImage.frame = frame;
    }];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: