黑马程序员—UI学记笔记—— 按钮

来源:互联网 发布:计算器软件下载 编辑:程序博客网 时间:2024/05/17 07:17

黑马程序员—UI学记笔记—— 按钮

-------Java培训Android培训iOS培训.Net培训、期待与您交流! -------


自己看视频写的一个简单按钮程序 大晚上发博客  都是为了去黑马  不容易啊睡觉






分享下经验

1,如果代码中要用到数字尽量使用枚举和宏定义 杜绝魔法数字

2,ViewDidLoad 要记得初始化父类 让父类把自己的事情干完

<span style="font-size:14px;"> [super viewDidLoad];</span>

3,设置颜色如果需要在后设置控件状态 不能使用点语法

<span style="font-size:14px;">btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];</span>

4,有make是相对位移形变  没make的是对上次的累加

<span style="font-size:14px;">//设置CGAffineTransformMakeTranslation(x, y)位移形变的属性值    //...Make相对于按钮的初始位置即按钮所在位置的位移形变    //没有make的transform位移形变是对上次形变的累加    self.iconButton.transform=CGAffineTransformTranslate(self.iconButton.transform, tx, ty);        </span>

5,理清思路,多敲代码


以下是具体代码实现   代码还可以精简 还需努力

@interface ViewController ()//私有扩展@property (weak, nonatomic) IBOutlet UIButton *iconButton;//定义枚举一般以小写的k开头 不要使用魔法数字(就是胡乱定义的数字)//枚举如果第一个变量被赋值 后续的变量的值依次递增typedef enum{    kMovingDirTop= 1,    kMovingDirLeft,    kMovingDirRight,    kMovingDirBottom,}kMovingDir;#define kMovingDelta 20.0f //用于origin后的计算@property(nonatomic,assign)CGFloat delate;@end@implementation ViewController-(void)viewDidLoad{    [super viewDidLoad];//千万要初始化父类            //初始化按钮 使用alloc init创建按钮为custom类型 即所有操作自己完成 系统不管        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];            //其他创建方式    UIButton *btn1=[UIButton buttonWithType:UIButtonTypeContactAdd];    UIButton *btn2=[UIButton buttonWithType:UIButtonTypeInfoLight];          btn1.center=CGPointMake(30, 40);    btn2.center=CGPointMake(70, 40);       [self.view addSubview:btn1];    [self.view addSubview:btn2];               //给iconButton赋值连线        self.iconButton=btn;                //设定颜色        btn.backgroundColor=[UIColor redColor];            //设定背景颜色 注意forState 要设置状态        [btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];        [btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];            //设定文字        [btn setTitle:@"别摸我" forState:UIControlStateNormal];        [btn setTitle:@"让你摸我了" forState:UIControlStateHighlighted];            //设定颜色        [btn setTitleColor:[UIColor blueColor] forState:            UIControlStateNormal];        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];            //对齐方式  垂直底部居中btn.contentVerticalAlignment=UIControlContentVerticalAlignmentBottom;                    //将按钮加载到视图         [self.view addSubview:btn];}#pragma mark -操作- (IBAction)move:(UIButton *)button {    //    CGRect frame=self.iconButton.frame;//    CGFloat tx=0,ty=0;//    switch (button.tag){//            //                    case kMovingDirTop://                        ty=-20;//                        break;//                    case kMovingDirLeft://                        tx=-20;//                        break;//                    case kMovingDirRight://                        tx=20;//                        break;//                    case kMovingDirBottom://                        ty=20;//                        break;//                   //                }//                //                self.iconButton.frame=frame;        //简便方法    CGFloat tx=0,ty=0;    if (button.tag==kMovingDirTop ||button.tag==kMovingDirBottom){                ty=(button.tag==kMovingDirTop) ? -kMovingDelta:kMovingDelta;                    }    if (button.tag==kMovingDirLeft ||button.tag==kMovingDirRight){                tx=(button.tag==kMovingDirLeft)?-kMovingDelta:kMovingDelta;            }                //声明变量每次累减赋值给tangsform    //self.delate-=20;    //设置CGAffineTransformMakeTranslation(x, y)位移形变的属性值    //...Make相对于按钮的初始位置即按钮所在位置的位移形变//self.iconButton.transform=CGAffineTransformMakeTranslation(0, self.delate);    //没有make的transform位移形变是对上次形变的累加    self.iconButton.transform=CGAffineTransformTranslate(self.iconButton.transform, tx, ty);            NSLog(@"%@",NSStringFromCGAffineTransform(self.iconButton.transform));    //    CGRect frame=self.iconButton.frame;//    switch (button.tag) {//        case kMovingDirTop://            frame.origin.y-=kMovingDelta;//            break;//        case kMovingDirLeft://            frame.origin.x-=kMovingDelta;//            break;//        case kMovingDirRight://            frame.origin.x+=kMovingDelta;//            break;//        case kMovingDirBottom://            frame.origin.y+=kMovingDelta;//            break;//       //    }//    //    self.iconButton.frame=frame;    }-(IBAction)ZoomWithbounds:(UIButton *)button{        //放大缩小    CGFloat scale=(button.tag)? 2.5:0.5;        [UIView beginAnimations:nil context:nil];    [UIView setAnimationDuration:2.0];            self.iconButton.transform=CGAffineTransformScale(self.iconButton.transform, scale, scale);    [UIView commitAnimations];//    CGRect bounds=self.iconButton.bounds;//    if (button.tag) {//        //        bounds.size.width+=kMovingDelta;//        bounds.size.height+=kMovingDelta;//        //    }else{//    //        bounds.size.width-=kMovingDelta;//        bounds.size.height-=kMovingDelta;//    //    }//    ////    self.iconButton.bounds=bounds;}

-------Java培训Android培训iOS培训.Net培训、期待与您交流! -------


0 0
原创粉丝点击