引言
UIScrollView在实际的项目开发中使用的比较多,可以很好展示图片
说下思路,根据APP的版本来决定是否显示新特性界面,如果版本号和当前版本号不同就显示新特性界面,否则直接进入主界面
- 在项目中先创建2个控制器,MainViewController和NewFeatureViewController,定义两个变量放当前的版本号和存储的版本号
var currontVersonCode : String? = ""
var lastVersonCode :String? = ""
//kCFBundleVersionKey 是info.plist文件表示版本号的Key
var key = kCFBundleVersionKey as String
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//创建窗口
self.window = UIWindow(frame :UIScreen.mainScreen().bounds)
Main = MainViewController()
NewFeature = NewFeatureViewController()
//去沙盒取版本号
lastVersonCode = NSUserDefaults.standardUserDefaults().stringForKey(key)
//加载程序中的info.plist文件
currontVersonCode = (NSBundle.mainBundle().infoDictionary![key]) as? String
if lastVersonCode == currontVersonCode{
UIApplication.sharedApplication().statusBarHidden = false
self.window?.rootViewController = Main
}else{
//保存当前版本号
self.window?.rootViewController = NewFeature
NSUserDefaults.standardUserDefaults().setObject(currontVersonCode as? AnyObject, forKey: key)
//立即保存
NSUserDefaults.standardUserDefaults().synchronize()
}
return true
}
- 新特性界面实现
来到我们创建的NewFeatureViewController中,思路是创建一个UIImage设置一个图片为背景,然后创建UIScrollView在UIImageView上,利用for循环将图片一张一张添加到UIScrollView中,第二张图片的X为第一张图片的宽度,这样就不难理解了
import UIKit
let kCount = 4
class NewFeatureViewController: UIViewController,UIScrollViewDelegate{
var Main :MainViewController!
var imageView :UIImageView!
var scrollView :UIScrollView!
override func loadView() {
imageView = UIImageView()
imageView.frame = UIScreen.mainScreen().bounds
print(UIScreen.mainScreen().bounds)
imageView?.image = UIImage(named: "new_feature_background.png")
self.view = imageView
self.imageView.userInteractionEnabled = true
}
override func viewDidLoad() {
super.viewDidLoad()
let viewSize = self.view.bounds
//创建scrollView
scrollView = UIScrollView()
scrollView.delegate = self
scrollView.frame = self.view.bounds
print(self.view.bounds.width)
scrollView.contentSize = CGSizeMake( self.view.bounds.width * CGFloat(kCount), 0)
scrollView.showsHorizontalScrollIndicator = false
scrollView.pagingEnabled = true
for i in 0..<kCount {
self.addImageViewAtIndex(i)
}
imageView.addSubview(scrollView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//添加图片
func addImageViewAtIndex(Index:Int)
{
let viewSize = self.view.bounds.size
let imageView :UIImageView = UIImageView()
imageView.frame = CGRectMake(viewSize.width * CGFloat(Index), 0,viewSize.width, viewSize.height)
imageView.image = UIImage(named: "new_feature_\(Index+1).png")
scrollView.addSubview(imageView)
}
}
- 添加PageController
定义一个PageController
var pageView : UIPageControl!
把pageController添加到UIScrollView里去
//创建pageControl
pageView = UIPageControl()
pageView.center = CGPointMake(viewSize.width * 0.5, viewSize.height * 0.95)
pageView.bounds = CGRectMake(0, 0, 100, 0)
pageView.numberOfPages = kCount
pageView.currentPageIndicatorTintColor = UIColor.blackColor()
pageView.pageIndicatorTintColor = UIColor.lightGrayColor()
imageView.addSubview(pageView)
UIPageControl同步显示
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
pageView.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.size.width)
}
看下效果图
-
在UIScrollView上添加按钮
分享按钮普通状态和选中状态分别为不同的图片
立即体验按钮普通状态和按住状态分别为不同的图片
func addButton()
{
let viewSize = self.view.bounds.size
let loginBtn : UIButton = UIButton()
let shareBtn : UIButton = UIButton()
var loginNormalImage :UIImage = UIImage()
var loginHeigtImage : UIImage = UIImage()
var shareNormalImage :UIImage = UIImage()
var shareHeigtImage : UIImage = UIImage()
loginNormalImage = UIImage(named:"new_feature_finish_button.png" )!
loginHeigtImage = UIImage(named: "new_feature_finish_button_highlighted.png")!
shareNormalImage = UIImage(named: "new_feature_share_true")!
shareHeigtImage = UIImage(named: "new_feature_share_false")!
loginBtn.center = CGPointMake(viewSize.width * 3.5, viewSize.height * 0.85)
loginBtn.bounds = CGRectMake(0, 0, loginNormalImage.size.width-20, loginNormalImage.size.height)
loginBtn.setImage(loginNormalImage, forState: UIControlState.Normal)
loginBtn.setImage(loginHeigtImage, forState: UIControlState.Highlighted)
loginBtn.addTarget(self, action: Selector("start"), forControlEvents: .TouchUpInside)
scrollView.addSubview(loginBtn)
shareBtn.center = CGPointMake(viewSize.width * 3.5, viewSize.height * 0.75)
shareBtn.bounds = CGRectMake(0, 0, shareNormalImage.size.width, shareNormalImage.size.height)
shareBtn.setImage(shareNormalImage, forState: UIControlState.Normal)
shareBtn.setImage(shareHeigtImage, forState: UIControlState.Selected)
shareBtn.addTarget(self, action: Selector("share:"), forControlEvents: .TouchUpInside)
scrollView.addSubview(shareBtn)
}
设置完成之后将按钮添加到UIScrollView中,并在添加图片的for循环中设置判断语句,当在第四张图的时候调用addButton方法
if i == 3{
self.addButton()
}
对于分享按钮反复点击切换不同状态,加上下面代码就可以实现了
func share(btn:UIButton) {
btn.selected = !btn.selected
}
总结
对于UIScrollView使用还是比较简单的,我觉得还可以扩展一下,例如UIScrollView的无限循环,还有淘宝等首页的自动换页。最后希望能给大家带来帮助,一起进步。