码迷,mamicode.com
首页 > 其他好文 > 详细

按钮操作Demo

时间:2015-02-06 18:41:55      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

//

//  ViewController.m

//  按钮操作

 

#import "ViewController.h"

 

/**

 使用git

 

 1.创建项目时,勾选Git

 2.开发告一段落,选择"source Control",并编写注释

 

 */

 

 

// 枚举类型实质上就是一个整数,作用就是用来代替魔法数字

// 枚举类型中,指定了第一个整数之后,后面的数字会递增

typedef enum

{

    kMovingDirTop = 10,

    kMovingDirBottom,

    kMovingDirLeft,

    kMovingDirRight

} kMovingDir;

 

#define kMovingDelta 60;

 

@interface ViewController ()

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

@end

 

@implementation ViewController

 

// 在OC中绝大多数的控件的监听方法的第一个参数就是控件本身

//- (IBAction)left:(UIButton *)button {

//    NSLog(@"___");

//}

 

- (IBAction)move:(UIButton *)button {

    // 通过frame修改head位置

    

    // 在OC中不允许直接修改"对象"的结构体属性的"成员"

    // 允许修改对象的结构体属性

    

    // 1.取出结构体属性

    // CGRect rect = self.headImageView.frame;

    CGPoint p = self.headImageView.center;

    // magic number魔法数字,其他程序员看到代码的时候,不知道是什么意思

    switch (button.tag) {

        case kMovingDirTop:

            // 2.修改结构体成员

            // rect.origin.y -= kMovingDelta;

            p.y -= kMovingDelta;

            break;

        case kMovingDirBottom:

            // 2.修改结构体成员

            // rect.origin.y += kMovingDelta;

            p.y += kMovingDelta;

            break;

        case kMovingDirLeft:

            // 2.修改结构体成员

            // rect.origin.x -= kMovingDelta;

            p.x -= kMovingDelta;

            break;

        case kMovingDirRight:

            // 2.修改结构体成员

            // rect.origin.x += kMovingDelta;

            p.x += kMovingDelta;

            break;

    }

    // 设置对象的结构体属性

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:1.0];

    // self.headImageView.frame = rect;

    self.headImageView.center = p;

    [UIView commitAnimations];

//    self.headImageView.frame.origin.y = self.headImageView.frame.origin.y - 20;

}

 

- (IBAction)zoom:(UIButton *)button {

    CGRect rect = self.headImageView.bounds;

    

    // 在C语言中,关于BOOL的判断:非零即真

    if (button.tag) {

        rect.size.width += 50;

        rect.size.height += 50;

    } else {

        rect.size.width -= 50;

        rect.size.height -= 50;

    }

    // 首尾动画

    // beginAnimations表示此后的代码要参与到动画中

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:3.0];

    self.headImageView.bounds = rect;

    self.headImageView.alpha = 0;

    // commitAnimations将beginAnimations之后的所有动画提交并生成动画

    [UIView commitAnimations];

}

 

 

 

 

@end

 

按钮操作Demo

标签:

原文地址:http://www.cnblogs.com/Holy-Mac/p/4277725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!