UI基础-02 按钮控制图片移动

//
//  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相同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值