//
// ImageViewViewController.swift
// UIControlDemo
//
// Created by on 14/12/1.
// Copyright (c) 2014年 马大哈. All rights reserved.
//
import UIKit
class ImageViewViewController: BaseViewController {
var staticImageView:UIImageView?
var dynamicImageView:UIImageView?
var indexImage = 0
var timer:NSTimer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "UIImageView"
staticImageView = UIImageView(frame: CGRectMake(0, 330, 100 , 100))
staticImageView?.image = UIImage(named: "test_DiamondInlay_goods_0.jpg")
staticImageView?.contentMode = UIViewContentMode.ScaleAspectFit
self.view.addSubview(staticImageView!)
dynamicImageView = UIImageView(frame: CGRectMake(10, 300, 290 , 290))
dynamicImageView?.contentMode = UIViewContentMode.ScaleAspectFit
self.view.addSubview(dynamicImageView!)
let imagePath = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("Test3D")
let imageArray = NSFileManager.defaultManager().subpathsAtPath(imagePath)
var imageReadArray:Array = [UIImage]()
if imageArray?.count > 0{
for imageName in imageArray!{
if imageName.rangeOfString(".png").location != NSNotFound || imageName.rangeOfString(".jpg").location != NSNotFound{
var imageFilePath = imagePath.stringByAppendingPathComponent(imageName as String)
/*
var dd = imagePath.stringByAppendingPathComponent(imageName)
这里不可以直接写入 imageName 会报错,编辑器无法推断imageName是不是String类型
必须要强制转化
*/
imageReadArray.append(UIImage(contentsOfFile: imageFilePath)!)
}
}
}
dynamicImageView?.animationImages = imageReadArray
dynamicImageView?.animationDuration = 5.0
}
override func viewDidAppear(animated: Bool) {
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "changeImageView", userInfo: nil, repeats: true)
dynamicImageView?.startAnimating()
}
override func viewWillAppear(animated: Bool) {
}
override func viewWillDisappear(animated: Bool) {
timer!.invalidate()
dynamicImageView?.stopAnimating()
}
func changeImageView(){
indexImage++
if indexImage > 7{
indexImage = 0
}
var imageName = "test_DiamondInlay_goods_(indexImage).jpg"
staticImageView?.image = UIImage(named: imageName)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}