//
// ViewController.m
// UI基础-02按钮控制和禁用
//
// Created by NorthCity on 15/9/6.
// Copyright (c) 2015年 Tcg. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *imageButton;
//因为要设置按钮状态,所以要把按钮设为实例变量
@property (weak, nonatomic) IBOutlet UIButton *upButton;
@property (weak, nonatomic) IBOutlet UIButton *leftButton;
@property (weak, nonatomic) IBOutlet UIButton *downButton;
@property (weak, nonatomic) IBOutlet UIButton *rightButton;
@property (weak, nonatomic) IBOutlet UIButton *minifyButton;
@property (weak, nonatomic) IBOutlet UIButton *plusButton;
@end
@implementation ViewController
//定义枚举 匹配 tag 的值
typedef enum{
kUp = 1,
kLeft,
kDown,
kRight,
kMinify,
kPlus,
}kMove;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//点击上下左右按钮时候,图片移动
-(IBAction)moveButtons:(UIButton*)sender{
CGRect frame = self.imageButton.frame;
switch (sender.tag) {
case kUp:
frame.origin.y -= 10;
break;
case kLeft:
frame.origin.x -= 10;
break;
case kDown:
frame.origin.y += 10;
break;
case kRight:
frame.origin.x += 10;
break;
default:
break;
}
// 设置移动动画效果, UIView
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
self.imageButton.frame =frame;
[self changeButtonState];
// 动画结束
[UIView commitAnimations];
}
- (IBAction)scaleButtons:(UIButton *)sender {
CGRect frame = self.imageButton.frame;
switch (sender.tag) {
case kMinify:
frame.size.height -= 10;
frame.size.width -= 10;
break;
case kPlus:
frame.size.height += 10;
frame.size.width += 10;
break;
default:
break;
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
self.imageButton.frame = frame;
[UIView commitAnimations];
[self changeButtonState];
}
// 判定图片是否到达边界,改变按钮状态
-(void)changeButtonState{
self.upButton.enabled = self.imageButton.frame.origin.y>0;
self.leftButton.enabled = self.imageButton.frame.origin.x>0;
self.downButton.enabled = self.view.frame.size.height-self.imageButton.frame.origin.y - self.imageButton.frame.size.height >0;
self.rightButton.enabled = self.view.frame.size.width - self.imageButton.frame.origin.x - self.imageButton.frame.size.width>0;
self.plusButton.enabled =self.upButton.enabled &&self.leftButton.enabled&&self.downButton.enabled&&self.rightButton.enabled;
}
@end
//最后有一点注意记得把Auto Layout 取消,不然图片按钮无法方法
//也可以用center 和Bounds 替换frame, 注意frame和bounds的区别 frame是以父视图为参照体系, bounds是自己为参照,所以 origin不同,size相同