图片浏览器

本文介绍了一个简单的iOS图片轮播视图控制器实现方法,包括如何使用懒加载技术加载plist文件中的图片资源,以及如何通过索引控制图片的切换,并实现前后翻页功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSArray *pic;

//自己写一个索引,来控制当前显示的是第几张图片
//这个属性一开始没有赋值就是0
@property (nonatomic, assign) int index;
- (IBAction)next;
@property (weak, nonatomic) IBOutlet UILabel *lblIndex;
@property (weak, nonatomic) IBOutlet UIImageView *imgViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *lblTitle;
@property (weak, nonatomic) IBOutlet UIButton *btnNaxt;
- (IBAction)pre;
@property (weak, nonatomic) IBOutlet UIButton *btnpre;

@end

@implementation ViewController

//重写pic属性的get方法
//------------懒加载数据------------
-(NSArray *)pic
{
    if (_pic == nil) {
        //写代码加载pic.plist文件中的数据到_pic
        //1.获取pic.plist文件的路径
        //获取pic.plist文件的路径赋值给path变量
        //[NSBundle mainBundle]表示获取这个app安装到手机时的根目录
        //然后在app的安装的根目录下搜索pic.plist文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"pic.plist" ofType:nil];
        //读取文件
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        _pic = array;
        //NSLog(@"count: %ld", array.count);
        //NSLog(@"%@", array);
    }
    return _pic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//下一张图片
- (IBAction)next {
    //1.让索引++
    self.index++;

    //2.从数组中获取当前这张图片的数据
    NSDictionary *dict = self.pic[self.index];

    //3.把获取到的数据设置给界面上的控件
    self.lblIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index+1, self.pic.count];
    //通过image属性来设置图片框里面的图片
    self.imgViewIcon.image = [UIImage imageNamed:dict[@"icon"]];
    //设置张图片的标题
    self.lblTitle.text = dict[@"title"];

    //设置“下一张”按钮是否可点击
    //方法一:
    self.btnNaxt.enabled = (self.index != (self.pic.count - 1));
    self.btnpre.enabled = (self.index != 0);

    /*
     方法二:
    if (self.index == (self.pic.count -1)) {
        self.btnNaxt.enabled = NO;
    }else{
        self.btnNaxt.enabled = YES;
    }
     */
}
//显示上一张
- (IBAction)pre {
    //1.让索引++
    self.index--;

    //2.从数组中获取当前这张图片的数据
    NSDictionary *dict = self.pic[self.index];

    //3.把获取到的数据设置给界面上的控件
    self.lblIndex.text = [NSString stringWithFormat:@"%d/%ld", self.index+1, self.pic.count];
    //通过image属性来设置图片框里面的图片
    self.imgViewIcon.image = [UIImage imageNamed:dict[@"icon"]];
    //设置张图片的标题
    self.lblTitle.text = dict[@"title"];

    //设置“显示上一张”按钮是否可点击
    //方法一:
    self.btnpre.enabled = (self.index != 0);
    self.btnNaxt.enabled = (self.index != (self.pic.count - 1));

    /*
     方法二:
     if (self.index == 0) {
     self.btnpre.enabled = NO;
     }else{
     self.btnpre.enabled = YES;
     }
    */
}
@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值