//
// ViewController.swift
// 图片浏览器
//
// Created by admin on 15/12/24.
// Copyright © 2015年 jin. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var preButton: UIButton!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var titleLable: UILabel!
@IBOutlet weak var imageDescription: UILabel!
//图片名称数组,
lazy var imageArr:[NSDictionary] =
{
//获取 plist文件路径
var filePath:String = (NSBundle.mainBundle().pathForResource("imageData", ofType: "plist"))!
//从文件中读取配置信息
var imageArr:NSArray! = NSArray(contentsOfFile: filePath)!
//转换类型并返回
return imageArr as! [NSDictionary]
}()
//当前显示图片的下标
static var imageIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
// print(self.imageArr[0]["image"]!)
updateImageView()
// imageView.image = UIImage(imageLiteral: "2")
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//上一张图片
@IBAction func preImage(sender: AnyObject) {
ViewController.imageIndex--
updateImageView()
}
//下一张图片
@IBAction func nextImage(sender: AnyObject) {
ViewController.imageIndex++
updateImageView()
}
//更新页面
func updateImageView()
{
//更新标题
titleLable.text = "\(ViewController.imageIndex+1)/\(imageArr.count)"
//设置上一页下一页按钮的状态
preButton.enabled = ViewController.imageIndex != 0
nextButton.enabled = ViewController.imageIndex < (imageArr.count - 1)
//做个动画效果
UIView.animateWithDuration(1, animations: {
//
if self.imageArr.count > ViewController.imageIndex
{
//设置 imageView 透明度,为了动画效果
self.imageView.alpha = 0
//设置 imageView 的图片
self.imageView.image = UIImage(imageLiteral: self.imageArr[ViewController.imageIndex]["image"] as! String)
//设置 当前图片的描述
self.imageDescription.text = self.imageArr[ViewController.imageIndex]["description"] as? String
self.imageView.alpha = 1
}
})
}
}