The Swfit of iOS development uses a custom Uirefreshcontrol Drop-down refresh interface

Source: Internet
Author: User
Tags current time

The default Uirefreshcontrol Drop-down Refresh interface is a daisy progress bar + A description of text, slightly monotonous. In fact, we can use our own created interface view, to facilitate our implementation of various effects. For example, add a dynamic picture, add an animation effect or something.

1, the following shows how to use a custom Drop-down refresh interface, as shown below:
(1) with the dropdown, the transparency of the interface starts to show slowly from 0.
(2) When the start of the refresh, the text will have a happy light effect (font size, and will change color)
2, first use Xib to create an interface (REFRESHVIEW.XIB)
(1) Its properties are set as follows
(2) Add 6 label inside to display descriptive text (because the text to play the animation, so to separate)
(3) Add constraints to each label individually
I'm used to giving the middle one a bit, and the other text labels are arranged according to the middle. Because it is 6 labels, first set the 3rd label ("Brush") constraints, wide and high fixed, vertical center, horizontal-19 center.
Set 2nd label ("Pull") constraint, wide and high fixed, horizontal distance to 10, centered vertically
Other labels by analogy (spacing is 10)
3, Home Code
(1) To better see the effect, analog network requests, here using Nstimer delay 3 seconds to generate data
(2) Be sure to set the Uirefreshcontrol backgroundcolor and Tintcolor into Uicolor.clearcolor (). Otherwise, the custom refresh interface height is not automatically stretched and becomes a fixed height
The code is as follows Copy Code


Import Uikit

Class Viewcontroller:uiviewcontroller,uitableviewdelegate,uitableviewdatasource {

News list
@IBOutlet weak var newstableview:uitableview!

News Array Collection
var dataarray:[hanggearticle] = [Hanggearticle] ()

Pull Refresh Controller
var Refreshcontrol = Uirefreshcontrol ()

var customview:uiview!

var labelsarray:array<uilabel> = []

var currentcolorindex = 0

var currentlabelindex = 0

var timer:nstimer!

Override Func Viewdidload () {
Super.viewdidload ()

Add Refresh
Refreshcontrol.addtarget (Self, Action: "RefreshData",
forControlEvents:UIControlEvents.ValueChanged)

Both background and tint colors are cleared to ensure that the custom dropdown view is highly adaptive
Refreshcontrol.backgroundcolor = Uicolor.clearcolor ()
Refreshcontrol.tintcolor = Uicolor.clearcolor ()

Newstableview.addsubview (Refreshcontrol)
LoadData ()

Load a custom Refresh interface
Loadcustomrefreshview ()
}


Customizing the Refresh Interface
Func Loadcustomrefreshview () {
Let refreshcontents = Nsbundle.mainbundle (). loadnibnamed ("Refreshview",
Owner:self, Options:nil)

CustomView = Refreshcontents[0] as! UIView
Customview.frame = Refreshcontrol.bounds

Customview.alpha = 0.0

for Var i=0; i<customview.subviews.count; ++i {
Labelsarray.append (Customview.viewwithtag (i + 1) as! Uilabel)
}

Refreshcontrol.addsubview (CustomView)
}

Scrolling view starts dragging
Func scrollviewwillbegindragging (Scrollview:uiscrollview) {
If!refreshcontrol.refreshing {
Self.labelsarray[0].text = "Down"
Self.labelsarray[1].text = "Pull"
Self.labelsarray[2].text = "Brush"
Self.labelsarray[3].text = "new"
Self.labelsarray[4].text = "Number"
Self.labelsarray[5].text = "according"
}
}

View drag
Func Scrollviewdidscroll (Scrollview:uiscrollview) {
Load interface Transparency Change
Let SG = (SCROLLVIEW.CONTENTOFFSET.Y *-1)/60.0
Customview.alpha = SG
}

Refreshing data
Func RefreshData () {
Self.labelsarray[0].text = "Number"
Self.labelsarray[1].text = "according"
Self.labelsarray[2].text = "Plus"
Self.labelsarray[3].text = "Download"
Self.labelsarray[4].text = "Medium"
Self.labelsarray[5].text = "..."

Play animation
Playanimaterefresh ()

Simulate loading data
Timer = nstimer.scheduledtimerwithtimeinterval (3.0, Target:self,
Selector: "LoadData", Userinfo:nil, Repeats:true)
}

Play text animation
Func Playanimaterefresh () {
Text amplification, color animation
Uiview.animatewithduration (0.15, delay:0.0,
Options:. Curvelinear, animations: {()-> Void in

Self.labelsarray[self.currentlabelindex].transform =
Cgaffinetransformmakescale (1.5, 1.5)
Self.labelsarray[self.currentlabelindex].textcolor =
Self.getnextcolor ()

}, Completion: {(finished)-> Void in

Text Style restore animation
Uiview.animatewithduration (0.1, delay:0.0,
Options:. Curvelinear, animations: {()-> Void in

Self.labelsarray[self.currentlabelindex].transform =
Cgaffinetransformidentity
Self.labelsarray[self.currentlabelindex].textcolor =
Uicolor.blackcolor ()

}, Completion: {(finished)-> Void in
++self.currentlabelindex

if Self.currentlabelindex = = self.labelsarray.count-1 {
Self.currentlabelindex = 0
}

Continue to play the animation when the load is not finished
If self.refreshControl.refreshing {
Self.playanimaterefresh ()
}else{
Self.currentlabelindex = 0
}
})
})
}

Timer time to, load data
Func LoadData () {
removing old data
Self.dataArray.removeAll ()
Randomly add 5 new data (time is current time)
For _ in 0..<5 {
Let atricle = hanggearticle (title: "News title \ (Int (Arc4random ()%1000))",
Createdate:nsdate ())
Self.dataArray.append (atricle)
}
Self.newsTableView.reloadData ()
Self.refreshControl.endRefreshing ()

A timer? Invalidate ()
Timer = Nil
}

Return number of records
Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {
return dataarray.count;
}

Return cell contents
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)
-> UITableViewCell {
Let cell = UITableViewCell (Style:UITableViewCellStyle.Subtitle,
Reuseidentifier: "MyCell")

Set cell title
Let atricle:hanggearticle = Dataarray[indexpath.row] as Hanggearticle
Cell.textlabel? Text = Atricle.title

Set cell subtitle
Let Dateformatter = NSDateFormatter ()
Dateformatter.dateformat = "Yyyy-mm-dd HH:mm:ss"
Let str = dateformatter.stringfromdate (atricle.createdate)
Cell.detailtextlabel? Text = str

return cell;
}

Get Next color
Func getnextcolor ()-> Uicolor {
var colorsarray:array<uicolor> = [Uicolor.magentacolor (),
Uicolor.browncolor (), Uicolor.yellowcolor (), Uicolor.redcolor (),
Uicolor.greencolor (), Uicolor.bluecolor (), Uicolor.orangecolor ()]

if Currentcolorindex = = Colorsarray.count {
Currentcolorindex = 0
}

Let Returncolor = Colorsarray[currentcolorindex]
++currentcolorindex

Return Returncolor
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

News structure Body
struct Hanggearticle {
var title:string
var createdate:nsdate
}

All right, here's what we're going to give you. About Swfit Use the custom Uirefreshcontrol dropdown to refresh the entire content of the interface, I hope the content is helpful to everyone.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.