1+ //
2+ // TaoBaoRefreshHeader.swift
3+ // PullToRefreshKit
4+ //
5+ // Created by huangwenchen on 16/7/14.
6+ // Copyright © 2016年 Leo. All rights reserved.
7+ //
8+
9+ import Foundation
10+ import UIKit
11+
12+ class TaoBaoRefreshHeader : UIView , RefreshableHeader {
13+ private let circleLayer = CAShapeLayer ( )
14+ private let arrowLayer = CAShapeLayer ( )
15+ private let textLabel = UILabel ( )
16+
17+ override init ( frame: CGRect ) {
18+ super. init ( frame: frame)
19+ setUpCircleLayer ( )
20+ setUpArrowLayer ( )
21+ textLabel. frame = CGRectMake ( CGRectGetWidth ( self . bounds) / 2 - 30 , CGRectGetHeight ( self . bounds) / 2 - 20 , 120 , 40 )
22+ textLabel. textAlignment = . Center
23+ textLabel. font = UIFont . systemFontOfSize ( 14 )
24+ textLabel. text = " 下拉即可刷新... "
25+ self . addSubview ( textLabel)
26+ }
27+ func setUpArrowLayer( ) {
28+ let bezierPath = UIBezierPath ( )
29+ bezierPath. moveToPoint ( CGPointMake ( 20 , 10 ) )
30+ bezierPath. addLineToPoint ( CGPointMake ( 20 , 30 ) )
31+ bezierPath. addLineToPoint ( CGPointMake ( 25 , 25 ) )
32+ bezierPath. moveToPoint ( CGPointMake ( 20 , 30 ) )
33+ bezierPath. addLineToPoint ( CGPointMake ( 15 , 25 ) )
34+ self . arrowLayer. path = bezierPath. CGPath
35+ self . arrowLayer. strokeColor = UIColor . blackColor ( ) . CGColor
36+ self . arrowLayer. fillColor = UIColor . clearColor ( ) . CGColor
37+ self . arrowLayer. lineWidth = 1.0
38+ self . arrowLayer. lineCap = kCALineCapRound
39+ self . arrowLayer. bounds = CGRectMake ( 0 , 0 , 40 , 40 )
40+ self . arrowLayer. position = CGPointMake ( CGRectGetWidth ( self . bounds) / 2.0 - 60 , CGRectGetHeight ( self . bounds) / 2.0 )
41+ self . arrowLayer. anchorPoint = CGPointMake ( 0.5 , 0.5 )
42+ self . layer. addSublayer ( self . arrowLayer)
43+ }
44+ func setUpCircleLayer( ) {
45+ let bezierPath = UIBezierPath ( arcCenter: CGPointMake ( 20 , 20 ) ,
46+ radius: 15.0 ,
47+ startAngle: CGFloat ( - M_PI/ 2 ) ,
48+ endAngle: CGFloat ( M_PI_2 * 3 ) ,
49+ clockwise: true )
50+ self . circleLayer. path = bezierPath. CGPath
51+ self . circleLayer. strokeColor = UIColor . blackColor ( ) . CGColor
52+ self . circleLayer. fillColor = UIColor . clearColor ( ) . CGColor
53+ self . circleLayer. strokeStart = 0.05
54+ self . circleLayer. strokeEnd = 0.05
55+ self . circleLayer. lineWidth = 1.0
56+ self . circleLayer. lineCap = kCALineCapRound
57+ self . circleLayer. bounds = CGRectMake ( 0 , 0 , 40 , 40 )
58+ self . circleLayer. position = CGPointMake ( CGRectGetWidth ( self . bounds) / 2.0 - 60 , CGRectGetHeight ( self . bounds) / 2.0 )
59+ self . circleLayer. anchorPoint = CGPointMake ( 0.5 , 0.5 )
60+ self . layer. addSublayer ( self . circleLayer)
61+ }
62+ required init ? ( coder aDecoder: NSCoder ) {
63+ fatalError ( " init(coder:) has not been implemented " )
64+ }
65+
66+ // MARK: - RefreshableHeader -
67+ func distanceToRefresh( ) -> CGFloat {
68+ return 60
69+ }
70+ func didBeginRefreshing( ) {
71+
72+ }
73+ func percentageChangedDuringDragging( percent: CGFloat ) {
74+ let adjustPercent = max ( min ( 1.0 , percent) , 0.0 )
75+ self . circleLayer. strokeEnd = 0.05 + ( 0.95 - 0.05 ) * adjustPercent
76+ if adjustPercent == 1.0 {
77+ textLabel. text = " 释放即可刷新... "
78+ } else {
79+ textLabel. text = " 下拉即可刷新... "
80+ }
81+ }
82+ func willBeginRefreshing( ) {
83+ self . circleLayer. strokeEnd = 0.95
84+ let rotateAnimation = CABasicAnimation ( keyPath: " transform.rotation.z " )
85+ rotateAnimation. toValue = NSNumber ( double: M_PI * 2.0 )
86+ rotateAnimation. duration = 0.6
87+ rotateAnimation. cumulative = true
88+ rotateAnimation. repeatCount = 10000000
89+ self . circleLayer. addAnimation ( rotateAnimation, forKey: " rotate " )
90+ self . arrowLayer. hidden = true
91+ textLabel. text = " 刷新中... "
92+ }
93+ func willEndRefreshing( result: RefreshResult ) {
94+ self . circleLayer. strokeEnd = 0.05
95+ self . circleLayer. removeAllAnimations ( )
96+ }
97+ func didEndRefreshing( result: RefreshResult ) {
98+ self . circleLayer. strokeEnd = 0.05
99+ self . arrowLayer. hidden = false
100+ textLabel. text = " 下拉即可刷新 "
101+ }
102+ }
0 commit comments