第一次用swift写分页demo

前端之家收集整理的这篇文章主要介绍了第一次用swift写分页demo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://git.oschina.net/lotusome/PageViewDemo

第一次用swift分页demo

http://git.oschina.net/lotusome/PageViewDemo

主要用到两个文件

//
// PageView.swift
// PhotoDemo
//
// Created by qings on 16/1/24.
// Copyright © 2016年 pukaila. All rights reserved.
//


import Foundation
import UIKit


class PageView : UIView {

var nextButton:UIButton!
var prevButton:UIButton!
var nextButtonWidth:Int!
var prevButtonWidth:Int!
var prevBtnCallback:((page:Int)->(Void))?
var nextBtnCallback:((page:Int)->(Void))?
var pageNumCallback:((page:Int)->(Void))?
var currentPageNum:Int!
var oldPageNum:Int!
var pageScrollView:PageScrollView!
var pageScrollViewPageSize:Int!
var pageScrollViewPageNumHeight:Int!
var pageScrollViewPageNumWidth:Int!
var pageScrollViewPageNumSpacing:Int!

var callback:((page:Int)->(Void))?
override init(frame: CGRect) {
super.init(frame:frame)
}

func doInit(){
initView()
initEven()
}

func initView() {
oldPageNum = currentPageNum
let x = nextButtonWidth
let y = 0
let w = Int(frame.width) - nextButtonWidth - nextButtonWidth
let h = Int(frame.height)
pageScrollView = PageScrollView(frame: CGRectMake(CGFloat(x),CGFloat(y),CGFloat(w),CGFloat(h)))
pageScrollView.pageSize = pageScrollViewPageSize
pageScrollView.pageHeight = pageScrollViewPageNumHeight
pageScrollView.pageWidth = pageScrollViewPageNumWidth
pageScrollView.spacing = pageScrollViewPageNumSpacing
pageScrollView.backgroundColor = UIColor.grayColor()
pageScrollView.showsHorizontalScrollIndicator = true
pageScrollView.currentPageNum = currentPageNum
pageScrollView.addPageNumItem()
pageScrollView.callback={(page)->Void in
self.oldPageNum = self.currentPageNum
self.currentPageNum = page
self.pageScrollView.setColor(self.oldPageNum,color: self.pageScrollView.normalColor)
self.pageScrollView.setColor(self.currentPageNum,color: self.pageScrollView.currentColor)
self.callback!(page:self.currentPageNum)
self.moveToCenterByPageNum(self.currentPageNum)
}
self.addSubview(pageScrollView)

prevButton = initButton(CGRectMake(CGFloat(0),CGFloat(0),CGFloat(nextButtonWidth),CGFloat(h)))
prevButton.setTitle("<",forState: UIControlState.Normal)
prevButton.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
self.addSubview(prevButton)

nextButton = initButton(CGRectMake(CGFloat(nextButtonWidth+w),CGFloat(h)))
nextButton.setTitle(">",forState: UIControlState.Normal)
nextButton.setTitleColor(UIColor.blackColor(),forState: UIControlState.Normal)
self.addSubview(nextButton)

self.backgroundColor = UIColor.whiteColor()
}

func initEven() {
prevButton.addTarget(self,action: "prevBtnClick:",forControlEvents: UIControlEvents.TouchUpInside)
nextButton.addTarget(self,action: "nextBtnClick:",forControlEvents: UIControlEvents.TouchUpInside)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func initButton(frame: CGRect)->UIButton {
let button:UIButton = UIButton(frame: frame)
button.backgroundColor = UIColor.redColor()
return button
}

func prevBtnClick(sender:AnyObject) {
let btn: UIButton = sender as! UIButton
if (currentPageNum > 1) {
oldPageNum = currentPageNum
currentPageNum = currentPageNum-1;
pageScrollView.setColor(oldPageNum,color: pageScrollView.normalColor)
pageScrollView.setColor(currentPageNum,color: pageScrollView.currentColor)
prevBtnCallback!(page: currentPageNum)
self.moveToCenterByPageNum(currentPageNum)
}
}
func nextBtnClick(sender:AnyObject) {
let btn: UIButton = sender as! UIButton
if (currentPageNum < pageScrollViewPageSize) {
oldPageNum = currentPageNum
currentPageNum = currentPageNum+1;
pageScrollView.setColor(oldPageNum,color: pageScrollView.currentColor)
nextBtnCallback!(page: currentPageNum)
self.moveToCenterByPageNum(currentPageNum)
}
}

func moveToCenterByPageNum(currentPageNum:Int)->Void {
let halfWidth = CGFloat(pageScrollView.frame.width/2)
let tempWidth = CGFloat(currentPageNum * (pageScrollViewPageNumWidth + pageScrollViewPageNumSpacing) - (pageScrollViewPageNumWidth + pageScrollViewPageNumSpacing)/2)
var newX = tempWidth - halfWidth
if (newX > 0 && pageScrollView.contentSize.width > self.frame.width){
if ((pageScrollView.contentSize.width - tempWidth) >= halfWidth) {
pageScrollView.setContentOffset(CGPointMake(newX,0),animated: true)
} else {
newX = pageScrollView.contentSize.width-pageScrollView.frame.width
pageScrollView.setContentOffset(CGPointMake(newX,animated: true)
}
} else {
pageScrollView.setContentOffset(CGPointMake(0,animated: true)
}
}
}

//
// PageView.swift
// PhotoDemo
//
// Created by qings on 16/1/24.
// Copyright © 2016年 pukaila. All rights reserved.
//


import Foundation
import UIKit


class PageScrollView : UIScrollView {
var screenObject:CGRect=UIScreen.mainScreen().bounds;
var pageSize : Int!
var pageHeight: Int!
var pageWidth: Int!
var spacing: Int!
var currentPageNum: Int!
var callback:((page:Int)->(Void))!
var currentColor:UIColor = UIColor.orangeColor()
var normalColor:UIColor = UIColor.greenColor()


func getPageNum(i:Int)->UIView {
let pageNumView : UIView = UIView()
let x:CGFloat = CGFloat((i-1) * pageWidth) + CGFloat((i-1)*spacing)
let y:CGFloat = 0
let width:CGFloat = CGFloat(pageWidth)
let height:CGFloat = CGFloat(pageHeight)
pageNumView.frame = CGRectMake(x,y,width,height)
pageNumView.backgroundColor = normalColor

let label:UILabel = UILabel(frame: CGRectMake(0,height))
label.text = String(i)
label.textAlignment = NSTextAlignment.Center
let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: "pageNumClick:")
pageNumView.addGestureRecognizer(tap)
pageNumView.tag = i
pageNumView.addSubview(label)
return pageNumView
}

func pageNumClick(sender:AnyObject){
let tap:UITapGestureRecognizer = sender as! UITapGestureRecognizer
callback(page: sender.view!!.tag)
}

func setColor(page:Int,color:UIColor)->Void {
self.subviews[page-1].backgroundColor = color
}

override init(frame: CGRect) {
super.init(frame:frame)
}

func addPageNumItem() {
let width = (self.pageWidth+spacing)*self.pageSize - spacing
let height = self.pageHeight
self.contentSize=CGSizeMake(CGFloat(width),CGFloat(height))
for index in 1...self.pageSize {
self.addSubview(getPageNum(index))
}
self.setColor(self.currentPageNum,color: currentColor)
}


required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

调用

// // ViewController.swift // PageViewDemo // // Created by qings on 16/1/24. // Copyright © 2016年 pukaila. All rights reserved. // import UIKit class ViewController: UIViewController { var screenObject:CGRect=UIScreen.mainScreen().bounds var label:UILabel! override func viewDidLoad() { super.viewDidLoad() intView() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func intView() { createPageView() createContentView() } func createContentView() { label = UILabel(frame: CGRectZero) label.text = "1" label.font = UIFont(name: "AmericanTypewriter-Bold",size: CGFloat(120)) label.textAlignment = NSTextAlignment.Center self.view.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false let constraintX:NSLayoutConstraint = NSLayoutConstraint( item: label,attribute: .CenterX,relatedBy: .Equal,toItem: self.view,multiplier: 1.0,constant: 0.0) self.view.addConstraint(constraintX) let constraintY:NSLayoutConstraint = NSLayoutConstraint( item: label,attribute: .CenterY,constant: 0.0) self.view.addConstraint(constraintY) } func createPageView() { let height = 40 let width = screenObject.width let x = 0 let y = screenObject.height - CGFloat(height) let pageView : PageView = PageView(frame: CGRectMake(CGFloat(x),CGFloat(width),CGFloat(height))) pageView.currentPageNum = 1 pageView.pageScrollViewPageSize = 6 pageView.pageScrollViewPageNumHeight = 40 pageView.pageScrollViewPageNumWidth = 40 pageView.pageScrollViewPageNumSpacing = 1 pageView.nextButtonWidth = 44 pageView.prevButtonWidth = 44 pageView.doInit() pageView.callback={(page)->Void in self.updatePage(page) } pageView.prevBtnCallback={(page)->Void in self.updatePage(page) } pageView.nextBtnCallback={(page)->Void in self.updatePage(page) } self.view.addSubview(pageView) } func updatePage(page:Int)->Void { self.label.text = String(page) } }

原文链接:/swift/324557.html

猜你在找的Swift相关文章