Skip to content

Commit 9ea9fc1

Browse files
author
Marco Abundo
committed
Converted to Swift
1 parent 01dead3 commit 9ea9fc1

18 files changed

Lines changed: 226 additions & 525 deletions

SLTDoubleTapSegmentedControl.xcodeproj/project.pbxproj

Lines changed: 50 additions & 182 deletions
Large diffs are not rendered by default.

SLTDoubleTapSegmentedControl/Images.xcassets/AppIcon.appiconset/Contents.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
{
22
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "20x20",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "20x20",
11+
"scale" : "3x"
12+
},
313
{
414
"idiom" : "iphone",
515
"size" : "29x29",
@@ -30,6 +40,16 @@
3040
"size" : "60x60",
3141
"scale" : "3x"
3242
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "20x20",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "20x20",
51+
"scale" : "2x"
52+
},
3353
{
3454
"idiom" : "ipad",
3555
"size" : "29x29",
@@ -59,6 +79,16 @@
5979
"idiom" : "ipad",
6080
"size" : "76x76",
6181
"scale" : "2x"
82+
},
83+
{
84+
"idiom" : "ipad",
85+
"size" : "83.5x83.5",
86+
"scale" : "2x"
87+
},
88+
{
89+
"idiom" : "ios-marketing",
90+
"size" : "1024x1024",
91+
"scale" : "1x"
6292
}
6393
],
6494
"info" : {

SLTDoubleTapSegmentedControl/SLTAppDelegate.h

Lines changed: 0 additions & 21 deletions
This file was deleted.

SLTDoubleTapSegmentedControl/SLTAppDelegate.m

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2018 shrtlist
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import UIKit
18+
19+
@UIApplicationMain
20+
class SLTAppDelegate: UIResponder, UIApplicationDelegate {
21+
22+
var window: UIWindow?
23+
24+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
25+
// Override point for customization after application launch.
26+
27+
window = UIWindow(frame: UIScreen.main.bounds)
28+
29+
let viewController = SLTViewController(nibName: "SLTViewController", bundle: nil)
30+
31+
window?.rootViewController = viewController
32+
window?.makeKeyAndVisible()
33+
34+
return true
35+
}
36+
}

SLTDoubleTapSegmentedControl/SLTDoubleTapSegmentedControl-Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.0</string>
20+
<string>1.1</string>
2121
<key>CFBundleSignature</key>
2222
<string>????</string>
2323
<key>CFBundleVersion</key>

SLTDoubleTapSegmentedControl/SLTDoubleTapSegmentedControl-Prefix.pch

Lines changed: 0 additions & 16 deletions
This file was deleted.

SLTDoubleTapSegmentedControl/SLTDoubleTapSegmentedControl.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

SLTDoubleTapSegmentedControl/SLTDoubleTapSegmentedControl.m

Lines changed: 0 additions & 119 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2018 shrtlist
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Double-tap segmented control. Each segment functions as a discrete double-tap button.
18+
// Credit: http://stackoverflow.com/questions/17652773/how-to-deselect-a-segment-in-segmented-control-button-permanently-till-its-click?lq=1
19+
20+
import UIKit
21+
22+
class SLTDoubleTapSegmentedControl: UISegmentedControl {
23+
24+
// MARK: Public method
25+
26+
func setTintColor(tintColor: UIColor, forSegmentAtIndex index: Int) {
27+
guard let title = titleForSegment(at: index) else { return }
28+
29+
let font = UIFont.systemFont(ofSize: 12)
30+
31+
let attributes = [NSAttributedStringKey.font: font, NSAttributedStringKey.foregroundColor: tintColor]
32+
33+
let attributedString = NSAttributedString(string: title, attributes: attributes)
34+
35+
let image = imageFromAttributedString(text: attributedString)
36+
37+
setImage(image, forSegmentAt: index)
38+
}
39+
40+
// MARK: Private methods
41+
42+
private func imageFromAttributedString(text: NSAttributedString) -> UIImage? {
43+
UIGraphicsBeginImageContextWithOptions(text.size(), false, 0.0)
44+
45+
// draw in context
46+
text.draw(at: CGPoint(x: 0, y: 0))
47+
48+
// transfer image
49+
guard let image = UIGraphicsGetImageFromCurrentImageContext()?.withRenderingMode(.alwaysOriginal) else {
50+
UIGraphicsEndImageContext()
51+
return nil
52+
}
53+
54+
UIGraphicsEndImageContext()
55+
56+
return image
57+
}
58+
59+
// MARK: Custom touch handling
60+
61+
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
62+
let previousSelectedSegmentIndex = selectedSegmentIndex
63+
64+
super.touchesEnded(touches, with: event)
65+
66+
if previousSelectedSegmentIndex == selectedSegmentIndex {
67+
// Clear the segmented control
68+
selectedSegmentIndex = UISegmentedControlNoSegment
69+
70+
// if the selectedSegmentIndex before the selection process is equal to the selectedSegmentIndex
71+
// after the selection process the superclass won't send a UIControlEventValueChanged event.
72+
// So we have to do this ourselves.
73+
sendActions(for: .valueChanged)
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)