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

UI基础-03 按钮控制图片移动之transform

2015-09-06 23:37 465 查看
//
//  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,
    kLeftRotate,
    kRightRotate
}kMove;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
//点击上下左右按钮时候,图片移动
-(IBAction)moveButtons:(UIButton*)sender{
    CGAffineTransform transform = self.imageButton.transform;
    switch (sender.tag) {
        case kUp:
            transform = CGAffineTransformTranslate(transform, 0, -10);
            break;
        case kLeft:
            transform = CGAffineTransformTranslate(transform, -10, 0);
            break;
        case kDown:
            transform = CGAffineTransformTranslate(transform, 0, 10);

            break;
        case kRight:
            transform = CGAffineTransformTranslate(transform, 10, 0);

            break;
            
        default:
       break;
            
        
    }
    
    
//    设置移动动画效果,  UIView
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    self.imageButton.transform = transform;
    [self changeButtonState];
//    动画结束
    [UIView commitAnimations];
}
//图片旋转
- (IBAction)rotateButtons:(UIButton *)sender {
    CGAffineTransform transform = self.imageButton.transform;
    switch (sender.tag) {
        case kLeftRotate:
            transform =CGAffineTransformRotate(transform, -M_2_PI);
            break;
            
        case kRightRotate:
            transform =CGAffineTransformRotate(transform, M_2_PI);
            break;
            
        default:
            break;
    }
    [UIView  beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    self.imageButton.transform = transform;
    [UIView commitAnimations];
    [self changeButtonState];
    
}
//图片缩放
- (IBAction)scaleButtons:(UIButton *)sender {
    CGAffineTransform transform = self.imageButton.transform;
    switch (sender.tag) {
        case kMinify:
            transform =CGAffineTransformScale(transform, 0.9, 0.9);
            break;
            
        case kPlus:
            transform =CGAffineTransformScale(transform, 1.1, 1.1);
            break;
            
        default:
            break;
    }
    [UIView  beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    self.imageButton.transform = transform;
    [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 取消,不然图片按钮无法方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: