按钮操作dome(UIButton的使用)

上一讲 我们说了 下面的几点

1、键盘的退出键盘的样式

2、手动创建代码

3、CGRectGetMaxX() 获取最大的X值 参数(传一个空间的frame进去)

不知道大家掌握的怎么样的 (哒哒:反正我没有掌握,)

今天给大家带来的是 一个小的程序按钮操作具体效果如下

(哒哒:我觉得好难啦!!!)

我基本把注释写到代码里面的 代码如下

//// ViewController.m// 02-按钮操作(代码实现)//// Created by sunda on 15/6/29.// Copyright (c) 2015年 sunda. All rights reserved.//#import "ViewController.h"typedef enum{kMovingDirTop = 10,kMovingDirBottom,kMovingDirLeft,kMovingDirRight,} kMovingDir;#define kMovingDelta 50@interface ViewController ()@property (nonatomic,strong)UIButton *headButton;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//=====================创建头像========================//self.headButton = [[UIButton alloc] init];self.headButton.frame = CGRectMake(150, 100, 100, 100);//设置图片[self.headButton setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];[self.headButton setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];//设置文字[self.headButton setTitle:@"点我啦" forState:UIControlStateNormal];[self.headButton setTitle:@"摸我干嘛" forState:UIControlStateHighlighted];//设置文字的颜色[self.headButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];[self.headButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];//设置文字的位置self.headButton.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;[self.view addSubview:self.headButton];//======================设置上按钮=====================//UIButton *topButton = [[UIButton alloc] init];topButton.frame = CGRectMake(100, 400, 50, 50);[topButton setBackgroundImage:[UIImage imageNamed:@"top_normal"] forState:UIControlStateNormal];[topButton setBackgroundImage:[UIImage imageNamed:@"top_highlighted"] forState:UIControlStateHighlighted];//设置tagtopButton.tag = kMovingDirTop;[self.view addSubview:topButton];[topButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];//=====================设置下按钮=====================//UIButton *buttomButton =[[UIButton alloc] init];buttomButton.frame = CGRectMake(100, 500, 50, 50);[buttomButton setBackgroundImage:[UIImage imageNamed:@"bottom_normal"] forState:UIControlStateNormal];[buttomButton setBackgroundImage:[UIImage imageNamed:@"bottom_highlighted"] forState:UIControlStateHighlighted];buttomButton.tag = kMovingDirBottom;[self.view addSubview:buttomButton];[buttomButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];//=================== 设置左按钮=====================//UIButton *leftButton = [[UIButton alloc] init];leftButton.frame = CGRectMake(50, 450, 50, 50);[leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];[leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];leftButton.tag = kMovingDirLeft;[self.view addSubview:leftButton];[leftButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];//=================== 设置右按钮=====================//UIButton *rightButton = [[UIButton alloc] init];rightButton.frame = CGRectMake(150, 450, 50, 50);[rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];[rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];rightButton.tag = kMovingDirRight;[self.view addSubview:rightButton];[rightButton addTarget:self action:@selector(move:) forControlEvents:UIControlEventTouchUpInside];//================= 设置发大按钮=====================//UIButton *plusButton = [[UIButton alloc] init];plusButton.frame = CGRectMake(210, 400, 50, 50);[plusButton setBackgroundImage:[UIImage imageNamed:@"plus_normal"] forState:UIControlStateNormal];[plusButton setBackgroundImage:[UIImage imageNamed:@"plus_highlighted"] forState:UIControlStateHighlighted];plusButton.tag = 1;[self.view addSubview:plusButton];[plusButton addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside];//=================== 设置缩小按钮===================//UIButton *minusButton = [[UIButton alloc] init];minusButton.frame = CGRectMake(300, 400, 50, 50);[minusButton setBackgroundImage:[UIImage imageNamed:@"minus_normal"] forState:UIControlStateNormal];[minusButton setBackgroundImage:[UIImage imageNamed:@"minus_highlighted"] forState:UIControlStateHighlighted];[self.view addSubview:minusButton];[minusButton addTarget:self action:@selector(zoom:) forControlEvents:UIControlEventTouchUpInside];//=================设置顺时针按钮===================//UIButton *left_rotate = [[UIButton alloc] init];left_rotate.frame = CGRectMake(210, 500, 50, 50);[left_rotate setBackgroundImage:[UIImage imageNamed:@"left_rotate_normal"] forState:UIControlStateNormal];[left_rotate setBackgroundImage:[UIImage imageNamed:@"left_rotate_highlighted"] forState:UIControlStateHighlighted];[self.view addSubview:left_rotate];//监听方法[left_rotate addTarget:self action:@selector(Rotation:) forControlEvents:UIControlEventTouchUpInside];//================设置逆时针按钮==================//UIButton *minus = [[UIButton alloc] init];minus.frame = CGRectMake(300, 500, 50, 50);[minus setBackgroundImage:[UIImage imageNamed:@"right_rotate_normal"] forState:UIControlStateNormal];[minus setBackgroundImage:[UIImage imageNamed:@"right_rotate_highlighted"] forState:UIControlStateHighlighted];[self.view addSubview:minus];//监听方法[minus addTarget:self action:@selector(Rotation:) forControlEvents:UIControlEventTouchUpInside];}/** * 旋转 */- (void)Rotation:(UIButton *)button{//CGAffineTransformScale 缩发//self.headButton.transform = CGAffineTransformScale(self.headButton.transform, 0.8, 0.8);//CGAffineTransformTranslate 移动//self.headButton.transform = CGAffineTransformTranslate(self.headButton.transform, 0, 20);//CGAffineTransformRotate 旋转self.headButton.transform = CGAffineTransformRotate(self.headButton.transform, M_PI_4);}/** * 移动 */- (void)move:(UIButton *)button{//第一种方式// CGRect rect = self.headButton.frame;// switch (button.tag) {//case kMovingDirTop://rect.origin.y -= kMovingDelta;//break;//case kMovingDirBottom://rect.origin.y += kMovingDelta;//break;//case kMovingDirLeft://rect.origin.x -= kMovingDelta;//break;//case kMovingDirRight://rect.origin.x += kMovingDelta;//break;// }//第二种方式CGPoint p = self.headButton.center;switch (button.tag) {case kMovingDirTop:p.y -= kMovingDelta;break;case kMovingDirBottom:p.y += kMovingDelta;break;case kMovingDirLeft:p.x -= kMovingDelta;break;case kMovingDirRight:p.x += kMovingDelta;break;}/*首尾式动画========================================// beginAnimations表示此后的代码要“参与到”动画中[UIView beginAnimations:nil context:nil];// setAnimationDuration用来指定动画持续时间[UIView setAnimationDuration:2.0];self.headImageView.bounds = rect;……// commitAnimations,将beginAnimation之后的所有动画提交并生成动画[UIView commitAnimations];*/[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:2.0];self.headButton.center = p;[UIView commitAnimations];NSLog(@"111");}/** * 发大 缩小 */- (void)zoom:(UIButton *)button{CGRect rect = self.headButton.bounds;if (button.tag) {rect.size.width += kMovingDelta;rect.size.height += kMovingDelta;}else{rect.size.width -= kMovingDelta;rect.size.height -= kMovingDelta;}[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:2.0];self.headButton.bounds = rect;[UIView commitAnimations];}@end

你挤进地铁时,西藏的山鹰一直盘旋云端,

按钮操作dome(UIButton的使用)

相关文章:

你感兴趣的文章:

标签云: