ios实现简单的查看器

用纯代码开发的过程
1.了解界面是由什么构成的
2.用代码创建界面
3.编写代码
**

首先就是搭建界面了

//界面加载完时调用
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //1.序号
    _noLable=[[UILabel alloc]initWithFrame:CGRectMake(0, 20.0, self.view.bounds.size.width, 40.0)];
   // _noLable.text=@"1/5";
    [self.view addSubview:_noLable];
    // 设置lable文字的位置
    _noLable.textAlignment=NSTextAlignmentCenter;
    //2.图片
    CGFloat imageW=200.0;
    CGFloat imageH=200.0;
    CGFloat imageX=(self.view.bounds.size.width-imageW)*0.5;
    CGFloat imageY=CGRectGetMaxY(self.noLable.frame)+20.0;
    _iconView=[[UIImageView alloc]initWithFrame:CGRectMake(imageX, imageY, imageW,imageH)];
    //[_iconView setBackgroundColor:[UIColor purpleColor]];
    _iconView.image=[UIImage imageNamed:@"biaoqingdi"];
    [self.view addSubview:_iconView];
    //3.设置图片介绍
    CGFloat descY=CGRectGetMaxY(_iconView.frame);
    _descLable =[[UILabel alloc]initWithFrame:CGRectMake(0.0, descY, self.view.bounds.size.width, 40.0)];
   // _descLable.text=@"这是什么表情啊?";
    _descLable.textAlignment=NSTextAlignmentCenter;
    [_descLable setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:_descLable];
    //4.设置左边的按钮

    _leftBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40.0, 40.0)];
    CGFloat centerY=self.iconView.center.y;
    CGFloat centerX=(self.iconView.frame.origin.x*0.5);
    _leftBtn.center=CGPointMake(centerX,centerY);

    _leftBtn.tag=-1;

    [_leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [_leftBtn setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
    [_leftBtn setBackgroundImage:[UIImage imageNamed:@"left_disable"] forState:UIControlStateDisabled];
    [_leftBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_leftBtn];

    //5.设置右边的按钮
    _rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40.0, 40.0)];
    _rightBtn.center=CGPointMake(self.view.bounds.size.width-centerX, centerY);
    [_rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [_rightBtn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
    [_rightBtn setBackgroundImage:[UIImage imageNamed:@"right_disable"] forState:UIControlStateDisabled];
    [self.view addSubview:_rightBtn];
    _rightBtn.tag=+1;
    [_rightBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self ShowPhotoInfo];

}

现在就是实现功能了
代码1:

//实现方法 下一张照片
//-(void)nextPhoto{
//    //NSLog(@"%s",__func__);
//    self.index++;
//    [self ShowPhotoInfo];
//}
///*上一张照片*/
//-(void)topPhoto{
//    self.index--;
//    [self ShowPhotoInfo];
//    // 设置序号
//    self.noLable.text = [NSString stringWithFormat:@"%d/%d", self.index +1, 5];
//    switch (self.index) {
//        case 0:
//            self.iconView.image=[UIImage imageNamed:@"biaoqingdi"];
//            self.descLable.text=@"这个表情已经无人能敌了!";
//            break;
//        case 1:
//            self.iconView.image=[UIImage imageNamed:@"bingli"];
//            self.descLable.text=@"病例";
//            break;
//        case 2:
//            self.iconView.image=[UIImage imageNamed:@"chiniupa"];
//            self.descLable.text=@"小妹妹你这吃牛扒可比杀牛费劲啊!";
//            break;
//        case 3:
//            self.iconView.image=[UIImage imageNamed:@"danteng"];
//            self.descLable.text=@"真心蛋疼啊!";
//            break;
//        case 4:
//            self.iconView.image=[UIImage imageNamed:@"wangba"];
//            self.descLable.text=@"哦哦!我科打铁美如画。。。";
//            break;
//    }
//        self.leftBtn.enabled=(self.index!=0);
//    NSLog(@"llll");
//
//}

代码2

/*代码的重构意思就是让重复的代码只出现一次*/
-(void)ShowPhotoInfo{
    // 设置序号
    self.noLable.text = [NSString stringWithFormat:@"%d/%d", self.index +1, 5];
    switch (self.index) {
        case 0:
            self.iconView.image=[UIImage imageNamed:@"biaoqingdi"];
            self.descLable.text=@"这个表情已经无人能敌了!";
            break;
        case 1:
            self.iconView.image=[UIImage imageNamed:@"bingli"];
            self.descLable.text=@"病例";
            break;
        case 2:
            self.iconView.image=[UIImage imageNamed:@"chiniupa"];
            self.descLable.text=@"小妹妹你这吃牛扒可比杀牛费劲啊!";
            break;
        case 3:
            self.iconView.image=[UIImage imageNamed:@"danteng"];
            self.descLable.text=@"真心蛋疼啊!";
            break;
        case 4:
            self.iconView.image=[UIImage imageNamed:@"wangba"];
            self.descLable.text=@"哦哦!我科打铁美如画。。。";
            break;
    }
    self.leftBtn.enabled=(self.index!=0);
    self.rightBtn.enabled=(self.index!=4);
   // NSLog(@"llll");
}

-(void) clickBtn:(UIButton *)button{

    //获取图片索引?
    self.index+=(int)button.tag;
    [self ShowPhotoInfo];
}

值得注意的
我们在些代码的时候首先不是写最好的代码,而是要先把功能给实现了,在实现功能之后再修改,整合代码。

可以采取NSLog来看数据是否输出正确,当然用断点调试就更好了。

你可能感兴趣的:(ios学习)