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写分页demo全部内容,希望文章能够帮你解决第一次用swift写分页demo所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)