回答編集履歴

1

修正 

2016/09/13 11:41

投稿

_Kentarou
_Kentarou

スコア8490

test CHANGED
@@ -15,3 +15,143 @@
15
15
  }
16
16
 
17
17
  ```
18
+
19
+
20
+
21
+
22
+
23
+ ★ 検証したコード
24
+
25
+
26
+
27
+ ```swift
28
+
29
+ import UIKit
30
+
31
+
32
+
33
+ class ViewController: UIViewController, UIScrollViewDelegate {
34
+
35
+
36
+
37
+ @IBOutlet weak var scrollView: UIScrollView!
38
+
39
+ @IBOutlet weak var headerView: UIView!
40
+
41
+
42
+
43
+ var segmentedControl: HMSegmentedControl!
44
+
45
+ var segmentControls: HMSegmentedControl!
46
+
47
+
48
+
49
+ var statusbarHeight: CGFloat = 20
50
+
51
+
52
+
53
+ var isTabSelected = false
54
+
55
+
56
+
57
+
58
+
59
+ override func viewDidLoad() {
60
+
61
+ super.viewDidLoad()
62
+
63
+
64
+
65
+ segmentedControl = HMSegmentedControl(frame: CGRectMake(0, statusbarHeight, self.view.frame.size.width, self.headerView.frame.size.height))
66
+
67
+ segmentedControl.selectionIndicatorColor = UIColor(red: 1.18, green: 1.80, blue: 1.53, alpha: 0.8)
68
+
69
+
70
+
71
+ segmentControls = HMSegmentedControl(frame: CGRectMake(0, statusbarHeight, self.view.frame.size.width, self.headerView.frame.size.height))
72
+
73
+ self.view.addSubview(segmentControls)
74
+
75
+
76
+
77
+ segmentControls.sectionTitles = ["ITEM ONE", "ITEM TWO", "ITEM THREE"]
78
+
79
+ // segmentControls.sectionImages = [UIImage(named: "star")!, UIImage(named: "home")!, UIImage(named: "search")!]
80
+
81
+ // segmentControls.sectionSelectedImages = [UIImage(named: "starSelected")!, UIImage(named: "homeSelected")!, UIImage(named: "searchSelected")!]
82
+
83
+ segmentControls.selectedSegmentIndex = 1
84
+
85
+
86
+
87
+ let blockVariable :IndexChangeBlock = {(index:Int) -> Void in
88
+
89
+
90
+
91
+ self.isTabSelected = true
92
+
93
+
94
+
95
+ let frame = CGRectMake(self.scrollView.frame.size.width * CGFloat(index), 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)
96
+
97
+ self.scrollView.scrollRectToVisible(frame, animated: true)
98
+
99
+ print(index)
100
+
101
+ }
102
+
103
+
104
+
105
+ segmentControls.indexChangeBlock = blockVariable
106
+
107
+
108
+
109
+ scrollView.pagingEnabled = true
110
+
111
+ scrollView.showsHorizontalScrollIndicator = false
112
+
113
+ scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.scrollView.frame.size.height)
114
+
115
+ scrollView.delegate = self
116
+
117
+
118
+
119
+ let page1view = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, self.scrollView.frame.size.height))
120
+
121
+ page1view.backgroundColor = UIColor.blueColor()
122
+
123
+ self.scrollView.addSubview(page1view)
124
+
125
+
126
+
127
+ let page2view = UIView(frame: CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.scrollView.frame.size.height))
128
+
129
+ page2view.backgroundColor = UIColor.redColor()
130
+
131
+ self.scrollView.addSubview(page2view)
132
+
133
+
134
+
135
+ let page3view = UIView(frame: CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.scrollView.frame.size.height))
136
+
137
+ page3view.backgroundColor = UIColor.greenColor()
138
+
139
+ self.scrollView.addSubview(page3view)
140
+
141
+ }
142
+
143
+
144
+
145
+ func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
146
+
147
+ segmentControls.selectedSegmentIndex = Int(scrollView.contentOffset.x / scrollView.frame.maxX)
148
+
149
+
150
+
151
+ }
152
+
153
+ }
154
+
155
+
156
+
157
+ ```