import UIKit
class PageViewController: UIViewController {
@IBOutlet weak var myScrView: UIScrollView!
@IBOutlet weak var pagectr: UIPageControl!
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
myScrView.tag = 1
myScrView.autoresizingMask = UIViewAutoresizing.None
self.view.addSubview(myScrView)
self.setupScrollView(myScrView)
pagectr.tag = 12
pagectr.numberOfPages = 9
pagectr.autoresizingMask = UIViewAutoresizing.None
self.view.addSubview(pagectr)
}
func setupScrollView(scrMain:UIScrollView) -> Void {
var imgesArray : NSMutableArray = []
for position in 1...9 {
var strImage : String = "\(position).jpeg"
var imges = UIImage(named: strImage)
imgesArray.addObject(imges!)
imageView.contentMode = UIViewContentMode.ScaleToFill
imageView.image = imges
imageView.tag = position+1
scrMain.addSubview(imageView)
println(imageView)
}
scrMain.contentSize = CGSizeMake(scrMain.frame.size.width*10, scrMain.frame.size.height)
NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: "scrollingTimer", userInfo:nil, repeats: true)
}
func scrollingTimer(){
let scrMain: UIScrollView = self.view.viewWithTag(1) as UIScrollView
let pgctr: UIPageControl = self.view.viewWithTag(12) as UIPageControl
let contentOffset = scrMain.contentOffset.x as CGFloat
var nextPage = (CGFloat)(contentOffset/scrMain.frame.size.width) + 1
if(nextPage != 9){
scrMain.scrollRectToVisible(CGRectMake(nextPage*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height), animated: true)
pgctr.currentPage = Int(nextPage)
}
else{
scrMain.scrollRectToVisible(CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height), animated: true)
pgctr.currentPage = 1
}
}
}
我想在ios中使用swift制作一个图片幻灯片应用程序请帮助我只显示一个图像。我得到一个空白的屏幕,几乎就像动画没有跟上,然后我等了一会儿再次滑动图像视图加速到位并再次正常工作。知道我做错了什么吗?在使用动态数量的图像(这里是硬编码的)实现像这样的图像轮播时,最佳做法是什么?