9--按钮的基本使用

基本按钮 UIView 属性

Frame  // 比较常用,既能改变 button 的  X Y 值也能改变长度和宽度,以父控件为标准排序

bounds  // 也可以表示 X Y 位置和尺寸  但是以自己控件左上角为原点,也就是 X Y 永远都为0

center  中心位置 以父控件为标准

tag  // 整形属性标记

transform  // 形变属性 实现缩放 旋转 平移功能.

 

1. 使用 Frame 实现图片的平移

功能: 点击方向按钮 实现图片的平移

 

#import "ViewController.h"

@interface ViewController ()
- (IBAction)up;
- (IBAction)down;
- (IBAction)left;
- (IBAction)right;

- (IBAction)big;
- (IBAction)small;

@property (nonatomic, weak) IBOutlet UIButton *head;

@end

@implementation ViewController

#pragma mark 向上
- (IBAction)up
{
    CGRect tempFrame = self.head.frame;
    
    tempFrame.origin.y -=10;
    
    self.head.frame = tempFrame;
    
}

#pragma mark 向下
- (IBAction)down
{
    CGRect tempFrame = self.head.frame;
    
    tempFrame.origin.y +=10;
    
    self.head.frame = tempFrame;
    
}

#pragma mark 向左
- (IBAction)left
{
    CGRect tempFrame = self.head.frame;
    
    tempFrame.origin.x -=10;
    
    self.head.frame = tempFrame;
    
}

#pragma mark 向右
- (IBAction)right
{
    CGRect tempFrame = self.head.frame;
    
    tempFrame.origin.x +=10;
    
    self.head.frame = tempFrame;
    
}


#pragma mark 放大
- (IBAction)big
{
    //1. 取出原来的frame属性
    CGRect tempFrame = self.head.frame;
    
    //2.临时改变 frame 的 height和 width 的属性值
    tempFrame.size.height +=10;
    tempFrame.size.width +=10;
    
    //3. 将临时更改后的属性值赋值回去覆盖之前的值
    self.head.frame = tempFrame;
    
}

#pragma mark 缩小
- (IBAction)small
{
    CGRect tempFrame = self.head.frame;
    tempFrame.size.height -=10;
    tempFrame.size.width -=10;
    self.head.frame = tempFrame;
    
}

@end

 上述代码每一个按钮都需要写一个新的方法显得代码比较累赘 所以可以使用一个方法 move 来实现四个方向的平移,使用tag标记

 1 //
 2 //  ViewController.m
 3 //  02-按钮的基本使用(第二种实现方法)
 4 //
 5 //  Created by Stephen on 16/4/9.
 6 //  Copyright © 2016年 Stephen. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 
11 @interface ViewController ()
12 //申明一个 move 方法 通过一个方法来实现四个按钮的功能
13 - (IBAction)move:(UIButton *)btn;
14 - (IBAction)zoon:(UIButton *)btn;
15 
16 @property (nonatomic , weak ) IBOutlet UIButton *head;
17 
18 @end
19 
20 @implementation ViewController
21 
22 #pragma mark 实现方向移动
23 - (IBAction)move:(UIButton *)btn
24 {
25     
26     //1.取出原来的属性
27     CGRect tempFrame = self.head.frame;
28     
29     //2.修改临时属性
30     
31     CGFloat delta = 10;
32     switch (btn.tag) {
33         case 10:
34             tempFrame.origin.y -= delta;
35             break;
36        
37         case 20:
38             tempFrame.origin.y += delta;
39             break;
40        
41         case 30:
42             tempFrame.origin.x -= delta;
43             break;
44        
45         case 40:
46             tempFrame.origin.x += delta;
47             break;
48             
49     }
50     
51     //3. 重新赋值
52     
53     self.head.frame = tempFrame;
54     
55     
56 }
57 
58 #pragma mark 实现大小缩放
59 - (IBAction)zoon:(UIButton *)btn
60 {
61     CGRect tempFrame = self.head.frame;
62     
63     CGFloat delta = 10;
64     switch (btn.tag) {
65         case 50:
66             tempFrame.size.width += delta;
67             tempFrame.size.height +=  delta;
68             break;
69          
70         case 60:
71             tempFrame.size.width -=  delta;
72             tempFrame.size.height -=  delta;
73             break;
74     }
75     
76     
77     self.head.frame = tempFrame;
78 }
79 
80 
81 @end

 

同样的使用另外的一个属性进行实现同样的功能: 使用 bounds 属性

//
//  ViewController.m
//  03-按钮的基本使用(bounds 重建)
//
//  Created by Stephen on 16/4/10.
//  Copyright © 2016年 Stephen. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

- (IBAction)move:(UIButton *)btn;
- (IBAction)zoon:(UIButton *)btn;


@property (nonatomic , weak) IBOutlet UIButton *head;

@end

@implementation ViewController

- (IBAction)move:(UIButton *)btn
{
    //1. 获取属性值
    
    CGPoint tempCenter = self.head.center;
    
    //2. 更改临时属性值
    switch (btn.tag) {
        case 10:
            tempCenter.y -= 10;
                break;
        
        case 20:
            tempCenter.y +=10;
                break;
            
            case 30:
            tempCenter.x -=10;
            break;
            
            case 40:
            tempCenter.x += 10;
            break;
            
    }
    
    //3. 重新覆盖值
    self.head.center = tempCenter;    
    
}

- (IBAction)zoon:(UIButton *)btn
{
    CGRect tempBounds = self.head.bounds;
    
    switch (btn.tag) {
        case 50:
            tempBounds.size.height += 10;
            tempBounds.size.width  +=10;
            break;
            
        case 60:
            tempBounds.size.height -= 10;
            tempBounds.size.width -=10;
            break;
    }
    
    self.head.bounds = tempBounds;   
    
}

@end

 总结:

1.IBAction:
1> 能保证方法可以连线
2> 相当于void

2.IBOutlet:
1> 能保证属性可以连线

3.常见错误
setValue:forUndefinedKey:]: this class is not key value coding
错误原因是:连线出问题了

4.Xcode5开始的一些建议
把用于连线的一些方法和属性声明在.m文件的类扩展中

5.frame\center\bounds
1> frame:能修改位置和尺寸
2> center:能修改位置
3> bounds:能修改尺寸(x\y一般都是0)

 

附上一个: 加入了动画效果的小功能

//
//  ViewController.m
//  03-按钮的基本使用(bounds 重建)
//
//  Created by Stephen on 16/4/10.
//  Copyright © 2016年 Stephen. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

- (IBAction)move:(UIButton *)btn;
- (IBAction)zoon:(UIButton *)btn;


@property (nonatomic , weak) IBOutlet UIButton *head;

@end

@implementation ViewController

- (IBAction)move:(UIButton *)btn
{
    //开始动画
    [UIView beginAnimations:nil context:nil];
    
    //设置动画时间 默认为1/4秒
    [UIView setAnimationDuration:3.0];
    
    //1. 获取属性值
    
    CGPoint tempCenter = self.head.center;
    
    //2. 更改临时属性值
    CGFloat delta = 50;
    
    switch (btn.tag) {
        case 10:
            tempCenter.y -= delta;
                break;
        
        case 20:
            tempCenter.y +=delta;
                break;
            
            case 30:
            tempCenter.x -=delta;
            break;
            
            case 40:
            tempCenter.x += delta;
            break;
            
    }
    
    //3. 重新覆盖值
    self.head.center = tempCenter;    
    
    //提醒系统结束动画
    [UIView commitAnimations];
}

- (IBAction)zoon:(UIButton *)btn
{
    [UIView beginAnimations:nil context:nil];
    
    [UIView setAnimationDuration:3.0];
    
    CGRect tempBounds = self.head.bounds;
    CGFloat number = 30;
    
    switch (btn.tag) {
        case 50:
            tempBounds.size.height += number;
            tempBounds.size.width  +=number;
            break;
            
        case 60:
            tempBounds.size.height -= number;
            tempBounds.size.width -=number;
            break;
    }
    
    self.head.bounds = tempBounds;   
        [UIView commitAnimations];
    
}

@end

 

posted @ 2016-04-10 18:50  淡定的人参果  阅读(136)  评论(0编辑  收藏  举报