[纯代码/OC]IOS开发入门到进阶笔记20150715

来源:互联网 发布:外企数据分析师 编辑:程序博客网 时间:2024/06/06 10:25

界面布局如下,上方button受下方8个button控制,其中左边4个按钮控制移动,右边4个控制放缩与旋转


实现大致流程

一、代码布局

二、功能实现

三、代码优化


一、代码布局

1.分析界面

2.按照要求逐渐完成布局,代码十分累赘,并且大量重复,显得很垃圾

3.分析布局,可以得出如下图结构,以下列结果优化代码


得出一下较为简洁的代码

/*设置图片按钮*/

-(void)setImageButton

{

   UIButton *button = [[UIButtonallocinit];

   self.image = button;

   CGRect temp = self.image.frame;

    temp.size.width =viewW * 0.3;

    temp.size.height =viewW * 0.3;

    temp.origin.x =viewW * 0.7 /2;

    temp.origin.y =viewH * 0.1;

   self.image.frame = temp;

    [self.imagesetBackgroundColor:[UIColorgrayColor]];

    [self.imagesetBackgroundImage:[UIImageimageNamed:@""]forState:UIControlStateNormal];

    [self.imagesetBackgroundImage:[UIImageimageNamed:@""]forState:UIControlStateHighlighted];

    [self.viewaddSubview:self.image];


}


/* 设置移动按钮 */

-(void)setMoveButton

{

   self.tag = 0;

   for (int i =0; i < 9; i++) {

       int row = i/3;

       int rank = i%3;

       while ((i%2)==1) {

           UIButton *directButton = [[UIButtonallocinit];

            [directButtonsetFrame:CGRectMake(viewW*(0.08+0.12*rank),viewH*0.6+viewW*0.12*row,viewW*0.12,viewW*0.12)];

            [directButtonsetBackgroundColor:[UIColorgrayColor]];

            [directButtonsetBackgroundImage:[UIImageimageNamed:@""]forState:UIControlStateNormal];

            [directButtonsetBackgroundImage:[UIImageimageNamed:@""]forState:UIControlStateHighlighted];

           self.tag++;

            directButton.tag =self.tag;

            [directButtonaddTarget:selfaction:@selector(move:)forControlEvents:UIControlEventTouchUpInside];

            [self.viewaddSubview:directButton];

            i++;

        }

    }

}


/*设置放缩和旋转按钮*/

-(void)setTransformButton

{

   for (int i =0 ; i<4; i++) {

       int rank = i%2;

       int row = i/2;

       UIButton *tranButton = [[UIButtonallocinit];

        [tranButtonsetFrame:CGRectMake(viewW*(0.5+0.2*rank),viewH*0.6+viewW*0.2*row,viewW*0.16,viewW*0.16)];

        [tranButton setBackgroundColor:[UIColorgrayColor]];

        [tranButton setBackgroundImage:[UIImageimageNamed:@""]forState:UIControlStateNormal];

        [tranButton setBackgroundImage:[UIImageimageNamed:@""]forState:UIControlStateHighlighted];

       self.tag++;

        tranButton.tag =self.tag;

        [tranButton addTarget:selfaction:@selector(transform:)forControlEvents:UIControlEventTouchUpInside];

        [self.viewaddSubview:tranButton];

    }

}




/*

*宏变量获取当前view的height:#define viewH self.view.frame.size.height。

*宏变量获取当前view的width:#define viewW self.view.frame.size.width。

*声明了成员变量@property(nonatomic,strong)UIButton *image;来接受控制。

*声明了成员变量@property(nonatomic,assign)int tag;;来分辨每个按钮,以便方法的实现。

*布局代码全用比例控制控件的frame,这样就解决了屏幕尺寸的问题。

*/


二、功能实现

1.尝试多种方法实现功能

2.选择最好的方法

3.优化代码

优化后如下

/*实现移动功能*/

-(void)move:(UIButton *)button

{

    CGPoint p =self.image.center;

    

    //开始动画,beginAnimations表示以后的代码要参与到动画中

    [UIViewbeginAnimations:nilcontext:nil] ;

    

    //setAnimationDuration设置动画的时长

    [UIViewsetAnimationDuration:1.0];

   switch (button.tag) {

        casemovingDirectTop:

            p.y -=20;

           break;

        casemovingDirectLeft:

            p.x -=20;

           break;

        casemovingDirectRight:

            p.x +=20;

           break;

        casemovingDirectBottom:

            p.y +=20;

           break;

    }

   self.image.center = p;

    //commitAnimations表示动画结束

    [UIViewcommitAnimations];

}


/* 实现旋转与放缩功能 */

-(void)transform:(UIButton *)button

{

    //开始动画,beginAnimations表示以后的代码要参与到动画中

    [UIViewbeginAnimations:nilcontext:nil] ;

    

    //setAnimationDuration设置动画的时长

    [UIViewsetAnimationDuration:2.0];

    

   switch (button.tag) {

       case enlarge:

            [self.imagesetTransform:CGAffineTransformScale(self.image.transform,1.21.2)];

           break;

       case reduce:

            [self.imagesetTransform:CGAffineTransformScale(self.image.transform,0.80.8)];

           break;

       case leftRotate:

            [self.imagesetTransform:CGAffineTransformRotate(self.image.transform, -M_PI_4)];

           break;

        caserightRotate:

            [self.imagesetTransform:CGAffineTransformRotate(self.image.transform,M_PI_4)];

           break;

    }

    //commitAnimations表示动画结束

    [UIViewcommitAnimations];

}

//在文件中使用枚举,避免代码中存在magic number

typedef enum

{

    movingDirectTop =1,

    movingDirectLeft =2,

    movingDirectRight =3,

    movingDirectBottom =4,

    enlarge =5,

    reduce =6,

    leftRotate =7,

    rightRotate =8,

} movingAndTrans;


//移动代码还有如下方法实现

1.改变y的点的值来移动

CGRect rect =self.image.frame;

rect.origin.y -=20;

self.image.frame = rect;

2.修改frame来移动

[self.imagesetTransform:CGAffineTransformTranslate(self.image.transform,0,100)];

3.以原来的原点做参考移动

[self.imagesetTransform:CGAffineTransformMakeTranslation(0,100)];


//其他放缩方法

1.修改size来放缩

CGRect rect = self.image.frame;

rect.size.height -=20;

self.image.frame = rect;

2.未测试该方法效果

[self.imagesetTransform:CGAffineTransformMakeScale(1.1,1.1)];


//其他旋转方法,未测试效果

[self.imagesetTransform:CGAffineTransformMakeRotation(M_PI_4)];


结束语

其实代码优化是在开发中不断进行的,码着码着突然灵感一来,就想到怎么优化了,后面的不需要全部写完都有思路。代码优化的feel,个人认为,一是要在多码的前提下多想,边码边想边积累。看教程看别人怎么优化代码,即使理解了,没运用上还是别人的,即使运用上了,如果不多加磨练,摸索更多的可能,那可能会形成思维惯性。

另外一个,要学会自己解决问题,不要不会就立马问人,最起码要先自己摸索一遍,百度谷歌都是很好的工具,最最最起码要有自己的思路吧?毕竟思路才是精髓。学习是一个过程,要学会如何学习,而不是如何记住东西。



0 0