按钮操作

//
//  ViewController.h
//  01-按钮操作
//
//  Created by kevin on 13-11-22.
//  Copyright (c) 2013年 kevin. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MJViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *btn;

// 行走
- (IBAction)run:(id)sender;

// 缩放
- (IBAction)scale:(id)sender;
// 旋转
- (IBAction)rotate:(id)sender;
@end





//
//  ViewController.m
//  button-2-run-scale-rotate
//
//  Created by kevin on 13-11-23.
//  Copyright (c) 2013年 kevin. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


# pragma 按钮的上下左右
- (IBAction)run:(id)sender {
    // 设置按钮的动画
    [UIView beginAnimations:nil context:nil];
    // 设置动画花费时间
    [UIView setAnimationDuration:1.0];
    
    // 先取出frame的值(由于我们不能直接修改对象结构体成员)
    CGRect templeframe = _focus.frame; // 这里我犯了一个错误就是讲templeframe写成了(* templeframe),记住他是结构体不是指针类型
    
    // 然后怕判断出四个上下左右的tag值(我分别顺时针写的1,2,3,4)
    //首先取出按钮的tag值
    int tag = [sender tag];
    
    // 下面的50是常量,其实可以在外部定义一下例如下面两种
    //#define kDelta 50
    //const int delta = 50;
    switch (tag) {
        case 1:
            templeframe.origin.y -= 50;
            break;
            
        case 2:
            templeframe.origin.x += 50;
            break;
            
        case 3:
            templeframe.origin.y += 50;
             break;
            
        case 4:
            templeframe.origin.x -= 50;
            break;
            
        default:
            break;
    }
    
    // 重新赋值给按钮的frame
    _focus.frame = templeframe;
    
    // 动画结尾
    [UIView commitAnimations];
    
}


# pragma mark 形状改变——放大缩小
- (IBAction)scale:(id)sender {
    // 动画开头一部分
    [UIView beginAnimations:nil context:nil];
    
    // 设置动画时间
    [UIView setAnimationDuration:1.0];
    
    //取出tag的值,判断是放大(我设置的为20)还是缩小(21)
    int tag = [sender tag];
    if (20 == tag) {
        // 设置变大
        _focus.transform = CGAffineTransformScale(_focus.transform, 1.2, 1.2);//这里我也写错了,记错tag的数字了
    }else if (21 == tag){//变小
        _focus.transform = CGAffineTransformScale(_focus.transform, 0.8, 0.8);
    }
    /* 
     * 上面也可以写成
     * CGFloat scale = [sender tag] == 20 ? 1.2 : 0.8;
     
     * 修改按钮形变属性
     * _btn.transform = CGAffineTransformScale(_btn.transform, scale, scale);
     */
    
    //设置动画结尾
    [UIView commitAnimations];
}

# pragma mark 左转,右转
- (IBAction)rotate:(id)sender {
    
    // 动画开始
    [UIView beginAnimations:nil context:nil];
    
    // 动画时间
    [UIView setAnimationDuration:1.0];
    
    // 取出tag
    int tag = [sender tag];
    
    // 判断是左转还是右转‘
    if (10 == tag) {
        _focus.transform = CGAffineTransformRotate(_focus.transform, M_PI_4 * -1);
    }
    else if (11 == tag){
        _focus.transform = CGAffineTransformRotate(_focus.transform, M_PI_4 * 1);
    }
}
@end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值