iOS学习记录-UIButton的使用

首先创建一个工程,这里不再赘述。
将需要的素材放到工程里:


iOS学习记录-UIButton的使用_第1张图片

更改iphone模拟器的尺寸为4.7英寸,然后拖进一个button,对button的Type、Title、Background等属性的修改,(高亮状态下Highlighted同样修改)这样在view中建立起7个修改好属性的控件。


iOS学习记录-UIButton的使用_第2张图片

其中注意修改tag,后面的代码封装的时候需要用到。
iOS学习记录-UIButton的使用_第3张图片

完成之后就要实现每个button的功能,将控件和代码建立连接。
iOS学习记录-UIButton的使用_第4张图片

以下就是各个button功能的代码实现

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px 'PingFang SC'; color: #1d9421}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 18.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #822e0e}span.s4 {font-variant-ligatures: no-common-ligatures; color: #c32275}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #6122ae}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #539aa4}span.s9 {font-variant-ligatures: no-common-ligatures; color: #1d9421}span.s10 {font: 18.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #1d9421}span.s11 {font-variant-ligatures: no-common-ligatures; color: #0435ff}span.s12 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s13 {font: 18.0px Menlo; font-variant-ligatures: no-common-ligatures}

//
//  ViewController.m
//  Button
//
//  Created by 袁跃 on 16/4/20.
//  Copyright © 2016年 iflytek. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
- (IBAction)up:(UIButton *)sender;
- (IBAction)iconControler:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *iconIPhone;

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)up:(UIButton *)sender {
    //1、得到iconIPhone的frame
    CGRect originFrame = self.iconIPhone.frame;
    //2、修改x、y坐标的值
    switch (sender.tag) {
            //上
        case 1:
            originFrame.origin.y -= 5;
            break;
            //右
        case 2:
            originFrame.origin.x += 5;
            break;
            //xia
        case 3:
            originFrame.origin.y += 5;
            break;
            //zuo
        case 4:
            originFrame.origin.x -= 5;
            break;
        
    }
    //3.赋值
    self.iconIPhone.frame = originFrame;
}

- (IBAction)iconControler:(UIButton *)sender {
    
    //得到iconIPhone的frame
    CGRect originSize = self.iconIPhone.frame;
    //进行放大和缩小
    if (sender.tag == 1) {
        originSize.size.width+=10;
        originSize.size.height+=10;
    }else{
        originSize.size.height-=10;
        originSize.size.width-=10;
    }  
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:5];
    self.iconIPhone.frame = originFrame;
    [UIView commitAnimations];
   
}
@end
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 18.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #822e0e}span.s4 {font-variant-ligatures: no-common-ligatures; color: #c32275}span.s5 {font-variant-ligatures: no-common-ligatures; color: #6122ae}

//
//  ViewController.h
//  Button
//
//  Created by 袁跃 on 16/4/20.
//  Copyright © 2016年 iflytek. All rights reserved.
//

#import 

@interface ViewController : UIViewController

@end

运行后效果如下:


iOS学习记录-UIButton的使用_第5张图片

(不会放动图...所以效果上看不到...)
这就是最基本的button的使用

你可能感兴趣的:(iOS学习记录-UIButton的使用)