图片浏览器

来源:互联网 发布:入骨相思知不知txt番外 编辑:程序博客网 时间:2024/05/15 23:45

效果图


纯代码编写:

//

//  ViewController.h

//  图片浏览器-1

//

//  Created by Mac on 15-9-19.

//  Copyright (c) 2015 Mac. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewController :UIViewController

@property (nonatomic,strong)UILabel *titleLable;//标题

@property (nonatomic,strong)UIImageView *imageView;//图片

@property (nonatomic,strong)UIButton *leftButton;//左按钮

@property (nonatomic,strong)UIButton *rightButton;//右按钮

@property (nonatomic,strong)NSArray *arr;

@end



.m文件

//

//  ViewController.m

//  图片浏览器-1

//

//  Created by Mac on 15-9-19.

//  Copyright (c) 2015 Mac. All rights reserved.

//


#import "ViewController.h"

#define kScreenW [UIScreen mainScreen].bounds.size.width

@interface ViewController ()


@property (nonatomic,assign)int index;//索引值


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    [selfloadData];

    [selftitleLableInfomation];

    [selfpicture];

    [selfbutton];

    

    //默认显示数据

   self.index= -1;

   self.index++;

    [selfsetData];

}

- (void)loadData{

    //读取数据源


    self.arr = [[NSArrayalloc] initWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"imageData.plist"ofType:nil]];

}


//标题显示

- (void)titleLableInfomation{

        self.titleLable = [[UILabelalloc] initWithFrame:CGRectMake(kScreenW/2 -20,80 ,40, 40)];

    [self.viewaddSubview:self.titleLable];

}

//显示图片

- (void)picture{


    self.imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(kScreenW/2 -100, 130,200 ,200)];

    [self.viewaddSubview:self.imageView];

}


- (void)button{


    //左边按钮

    self.leftButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

   self.leftButton.frame =CGRectMake(15,200, 40, 40);

    [self.leftButtonsetImage:[UIImageimageNamed:@"left_highlighted"]forState:UIControlStateNormal];

    

    [self.viewaddSubview:self.leftButton];

    

    [self.leftButtonaddTarget:selfaction:@selector(leftAction)forControlEvents:UIControlEventTouchUpInside];


    //右边按钮

    self.rightButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

   self.rightButton.frame =CGRectMake(315,200, 40, 40);

    [self.rightButtonsetImage:[UIImageimageNamed:@"right_highlighted"]forState:UIControlStateNormal];

    [self.viewaddSubview:self.rightButton];

    

    [self.rightButtonaddTarget:selfaction:@selector(rightAction)forControlEvents:UIControlEventTouchUpInside];

}

//事件响应

- (void)rightAction{

   self.index++;

    //调用数据

    [selfsetData];

}


- (void)leftAction{

    

   self.index--;

    [selfsetData];


}

//设置数据

- (void)setData{


    //设置数据源

   self.titleLable.text = [NSStringstringWithFormat:@"%d/%ld",self.index,self.arr.count];

   NSDictionary *dic = _arr[self.index];

   self.imageView.image =[UIImageimageNamed:dic[@"icon"]];

   self.leftButton.enabled = (self.index !=0);

   self.rightButton.enabled = (self.index !=self.arr.count -1);


}





0 0