From 6fb85627a127294f98b76a67f5cc83035c10696e Mon Sep 17 00:00:00 2001 From: Graham Preston Date: Sun, 24 Sep 2017 14:23:47 -0400 Subject: [PATCH] Updated to Swift 4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I also edited the panHandlers velocity so that it is more coherent with Apple’s UIPanControl --- GBPageControl.xcodeproj/project.pbxproj | 40 ++++++++++++++--- GBPageControl/PageControl.swift | 45 ++++++++++--------- GBPageControlExample/AppDelegate.swift | 33 +++++++------- .../AppIcon.appiconset/Contents.json | 15 +++++++ GBPageControlExample/GameScene.swift | 16 +++---- GBPageControlExample/GameViewController.swift | 8 ++-- GBPageControlExample/Info.plist | 4 +- 7 files changed, 102 insertions(+), 59 deletions(-) diff --git a/GBPageControl.xcodeproj/project.pbxproj b/GBPageControl.xcodeproj/project.pbxproj index 7f3f5bc..b4dbc2c 100644 --- a/GBPageControl.xcodeproj/project.pbxproj +++ b/GBPageControl.xcodeproj/project.pbxproj @@ -177,7 +177,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0710; - LastUpgradeCheck = 0710; + LastUpgradeCheck = 0900; ORGANIZATIONNAME = "Jason Hurt"; TargetAttributes = { 00C05C551C1C839300D4B456 = { @@ -185,6 +185,7 @@ }; 00C05C661C1C854900D4B456 = { CreatedOnToolsVersion = 7.1; + ProvisioningStyle = Automatic; }; }; }; @@ -284,13 +285,21 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; @@ -319,6 +328,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -333,13 +343,21 @@ CLANG_CXX_LIBRARY = "libc++"; CLANG_ENABLE_MODULES = YES; CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; @@ -359,6 +377,8 @@ IPHONEOS_DEPLOYMENT_TARGET = 9.1; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; @@ -371,7 +391,7 @@ buildSettings = { CLANG_ENABLE_MODULES = YES; CODE_SIGN_IDENTITY = "iPhone Developer"; - "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -384,6 +404,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -404,23 +425,28 @@ PRODUCT_BUNDLE_IDENTIFIER = com.pulsefix.GBPageControl; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; + SWIFT_VERSION = 4.0; }; name = Release; }; 00C05C7B1C1C854900D4B456 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + CODE_SIGN_STYLE = Automatic; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; - EMBEDDED_CONTENT_CONTAINS_SWIFT = YES; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = GBPageControlExample/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.pulsefix.GBPageControlExample; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; + PROVISIONING_PROFILE_SPECIFIER = ""; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -428,16 +454,20 @@ 00C05C7C1C1C854900D4B456 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { + ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; - "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; - EMBEDDED_CONTENT_CONTAINS_SWIFT = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = ""; INFOPLIST_FILE = GBPageControlExample/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.pulsefix.GBPageControlExample; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; + PROVISIONING_PROFILE_SPECIFIER = ""; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/GBPageControl/PageControl.swift b/GBPageControl/PageControl.swift index d8eafcb..884aebf 100644 --- a/GBPageControl/PageControl.swift +++ b/GBPageControl/PageControl.swift @@ -48,20 +48,20 @@ public class PageControl: NSObject { } public func handleTouch(touch:UITouch) -> Bool { - let location = touch.locationInNode(pageIndicatorNode!) + let location = touch.location(in: pageIndicatorNode!) let touchMargin:CGFloat = xMargin / 2.0 for i in 0...pageIndicators!.count - 1 { let indicator = pageIndicators![i] - if CGRectContainsPoint(CGRect( + if CGRect( x:indicator.frame.origin.x - touchMargin, y:indicator.frame.origin.y - touchMargin, width:indicator.frame.width + touchMargin * 2.0, - height:indicator.frame.height + touchMargin * 2.0), location) { - let point = CGPoint(x:-1.0 * CGFloat(i) * parentScene!.size.width, y:contentNode!.position.y) - contentNode!.runAction(SKAction.moveTo(point, duration:0.2), completion: { [weak self] in - self?.pageStateDidChange() - }) - return true + height:indicator.frame.height + touchMargin * 2.0).contains(location) { + let point = CGPoint(x:-1.0 * CGFloat(i) * parentScene!.size.width, y:contentNode!.position.y) + contentNode!.run(SKAction.move(to: point, duration:0.2), completion: { [weak self] in + self?.pageStateDidChange() + }) + return true } } return false @@ -87,40 +87,40 @@ public class PageControl: NSObject { pageIndicatorNode.addChild(pageCircle) pageIndicators.append(pageCircle) } - pageIndicatorNode.position = CGPoint(x:CGRectGetMidX(parentScene!.frame) - pageIndicatorWidth/2.0, y:yPosition + pageIndicatorHeight/2.0) + pageIndicatorNode.position = CGPoint(x: (parentScene!.frame.size.width / 2) - pageIndicatorWidth/2.0, y:yPosition + pageIndicatorHeight/2.0) parentScene?.addChild(pageIndicatorNode) } private func addPanGestureRecognizer() { - panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(PageControl.handlePanGesture(_:))) + panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.handlePanGesture)) parentScene?.view?.addGestureRecognizer(panGestureRecognizer) } - public func handlePanGesture(recognizer: UIPanGestureRecognizer) { - if recognizer.state == UIGestureRecognizerState.Began { - panGestureStartPoint = parentScene?.convertPointFromView(recognizer.locationInView(recognizer.view)) + @objc public func handlePanGesture(recognizer: UIPanGestureRecognizer) { + if recognizer.state == UIGestureRecognizerState.began { + panGestureStartPoint = parentScene?.convertPoint(fromView: recognizer.location(in: recognizer.view)) panGestureStartContentPosition = contentNode.position } - else if recognizer.state == UIGestureRecognizerState.Changed { + else if recognizer.state == UIGestureRecognizerState.changed { if panGestureStartPoint != nil { - let touchPoint = parentScene?.convertPointFromView(recognizer.locationInView(recognizer.view)) - let velocity = recognizer.velocityInView(recognizer.view!) - let slideMultiplier = abs(velocity.x) / 35000 - let newPosition = boundContentNode(CGPoint(x: contentNode.position.x - (panGestureStartPoint.x - touchPoint!.x) + (velocity.x * slideMultiplier), - y: contentNode!.position.y)) + let touchPoint = parentScene?.convertPoint(fromView: recognizer.location(in: recognizer.view)) + let velocity = recognizer.velocity(in: recognizer.view!) + let slideMultiplier : CGFloat = 1 / 100000 + let newPosition = boundContentNode(point: CGPoint(x: contentNode.position.x - (panGestureStartPoint.x - touchPoint!.x) + (velocity.x * slideMultiplier), + y: contentNode!.position.y)) if abs(newPosition.x - panGestureStartContentPosition.x) < parentScene!.size.width * 1.4 { contentNode!.position = newPosition } panGestureStartPoint = touchPoint } } - else if recognizer.state == UIGestureRecognizerState.Ended { + else if recognizer.state == UIGestureRecognizerState.ended { panGestureStartPoint = nil panGestureStartContentPosition = nil let page = getSelectedPage() let point = CGPoint(x:-1.0 * CGFloat(page) * parentScene!.size.width, y:contentNode!.position.y) - let moveTo = SKAction.moveTo(point, duration:0.2) - contentNode!.runAction(moveTo) + let moveTo = SKAction.move(to: point, duration:0.2) + contentNode!.run(moveTo) pageStateDidChange() } } @@ -150,3 +150,4 @@ public class PageControl: NSObject { return point } } + diff --git a/GBPageControlExample/AppDelegate.swift b/GBPageControlExample/AppDelegate.swift index 2b867ff..fdecef1 100644 --- a/GBPageControlExample/AppDelegate.swift +++ b/GBPageControlExample/AppDelegate.swift @@ -10,37 +10,34 @@ import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - + var window: UIWindow? - - - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - // Override point for customization after application launch. + + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { return true } - - func applicationWillResignActive(application: UIApplication) { + + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - - func applicationDidEnterBackground(application: UIApplication) { + + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - - func applicationWillEnterForeground(application: UIApplication) { + + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - - func applicationDidBecomeActive(application: UIApplication) { + + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - - func applicationWillTerminate(application: UIApplication) { + + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } - - + + } - diff --git a/GBPageControlExample/Assets.xcassets/AppIcon.appiconset/Contents.json b/GBPageControlExample/Assets.xcassets/AppIcon.appiconset/Contents.json index 118c98f..19882d5 100644 --- a/GBPageControlExample/Assets.xcassets/AppIcon.appiconset/Contents.json +++ b/GBPageControlExample/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -1,5 +1,15 @@ { "images" : [ + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "20x20", + "scale" : "3x" + }, { "idiom" : "iphone", "size" : "29x29", @@ -29,6 +39,11 @@ "idiom" : "iphone", "size" : "60x60", "scale" : "3x" + }, + { + "idiom" : "ios-marketing", + "size" : "1024x1024", + "scale" : "1x" } ], "info" : { diff --git a/GBPageControlExample/GameScene.swift b/GBPageControlExample/GameScene.swift index 33ba395..1ec4ac7 100644 --- a/GBPageControlExample/GameScene.swift +++ b/GBPageControlExample/GameScene.swift @@ -12,20 +12,20 @@ import GBPageControl var pageControl:PageControl! class GameScene: SKScene { - override func didMoveToView(view: SKView) { + override func didMove(to view: SKView) { addBackground() pageControl = PageControl(scene: self) addContent() - pageControl.enable(4) + pageControl.enable(numberOfPages: 4) } - override func willMoveFromView(view: SKView) { - pageControl.willMoveFromView(view) + override func willMove(from view: SKView) { + pageControl.willMoveFromView(view: view) } - override func touchesBegan(touches: Set, withEvent event: UIEvent?) { + override func touchesBegan(_ touches: Set, with event: UIEvent?) { for touch in touches { - if pageControl.handleTouch(touch) { + if pageControl.handleTouch(touch: touch) { //no op } else { @@ -36,7 +36,7 @@ class GameScene: SKScene { func addBackground() { let background = SKShapeNode(rect: CGRect(x: 0.0, y: 0.0, width: self.size.width, height: self.size.height)) - background.fillColor = UIColor.whiteColor() + background.fillColor = UIColor.white background.strokeColor = background.fillColor self.addChild(background) } @@ -47,7 +47,7 @@ class GameScene: SKScene { let x = self.size.width / 2.0 + self.size.width * CGFloat(i) let y = self.size.height / 2.0 sprite.position = CGPoint(x:x, y:y) - pageControl.addChild(sprite) + pageControl.addChild(node: sprite) } } } diff --git a/GBPageControlExample/GameViewController.swift b/GBPageControlExample/GameViewController.swift index 3ad82a5..17ceb21 100644 --- a/GBPageControlExample/GameViewController.swift +++ b/GBPageControlExample/GameViewController.swift @@ -10,21 +10,21 @@ import UIKit import SpriteKit class GameViewController: UIViewController { - + override func viewDidLoad() { super.viewDidLoad() } - override func viewDidAppear(animated: Bool) { + override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) let scene = GameScene(size: view.bounds.size) let skView = view as! SKView skView.ignoresSiblingOrder = true - scene.scaleMode = .ResizeFill + scene.scaleMode = .resizeFill skView.presentScene(scene) } - override func prefersStatusBarHidden() -> Bool { + func prefersStatusBarHidden() -> Bool { return true } } diff --git a/GBPageControlExample/Info.plist b/GBPageControlExample/Info.plist index b7d4e46..421b316 100644 --- a/GBPageControlExample/Info.plist +++ b/GBPageControlExample/Info.plist @@ -30,13 +30,13 @@ armv7 + UIRequiresFullScreen + UIStatusBarHidden UISupportedInterfaceOrientations UIInterfaceOrientationPortrait - UIInterfaceOrientationLandscapeLeft - UIInterfaceOrientationLandscapeRight