From 93fb4efc24feb9840e5f74c6b3b837f5598def8c Mon Sep 17 00:00:00 2001 From: Pasha Date: Sat, 23 Mar 2019 21:32:29 -0700 Subject: [PATCH] Migrate to Swift 4.2 --- MetalParticles.xcodeproj/project.pbxproj | 33 ++++++- MetalParticles/AppDelegate.swift | 27 +---- MetalParticles/Base.lproj/Main.storyboard | 17 ++-- MetalParticles/MarkerWidget.swift | 8 +- MetalParticles/ParticleLab.swift | 98 +++++++++---------- MetalParticles/ViewController.swift | 81 +++++++-------- MetalParticlesTests/MetalParticlesTests.swift | 2 +- 7 files changed, 132 insertions(+), 134 deletions(-) diff --git a/MetalParticles.xcodeproj/project.pbxproj b/MetalParticles.xcodeproj/project.pbxproj index 21c5694..861aa90 100644 --- a/MetalParticles.xcodeproj/project.pbxproj +++ b/MetalParticles.xcodeproj/project.pbxproj @@ -201,12 +201,13 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0700; + LastUpgradeCheck = 1010; ORGANIZATIONNAME = "Simon Gladman"; TargetAttributes = { BE75EE8B1A6A2CC000B20D49 = { CreatedOnToolsVersion = 6.1.1; DevelopmentTeam = ZBFYF9JG5V; + ProvisioningStyle = Automatic; }; BE75EEA01A6A2CC000B20D49 = { CreatedOnToolsVersion = 6.1.1; @@ -313,13 +314,23 @@ 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_DEPRECATED_OBJC_IMPLEMENTATIONS = 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_IMPLICIT_RETAIN_SELF = 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*]" = "iPhone Developer"; @@ -328,6 +339,7 @@ ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", @@ -347,6 +359,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -359,13 +372,23 @@ 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_DEPRECATED_OBJC_IMPLEMENTATIONS = 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_IMPLICIT_RETAIN_SELF = 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*]" = "iPhone Developer"; @@ -373,6 +396,7 @@ ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_UNDECLARED_SELECTOR = YES; @@ -385,7 +409,8 @@ MTL_OPTIMIZATION_LEVEL = s; SDKROOT = iphoneos; SWIFT_DISABLE_SAFETY_CHECKS = YES; - SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 4.2; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; @@ -397,6 +422,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = MetalParticles/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -406,6 +432,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "uk.co.flexmonkey.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; + PROVISIONING_PROFILE_SPECIFIER = ""; }; name = Debug; }; @@ -415,6 +442,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = MetalParticles/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; @@ -424,6 +452,7 @@ PRODUCT_BUNDLE_IDENTIFIER = "uk.co.flexmonkey.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE = ""; + PROVISIONING_PROFILE_SPECIFIER = ""; }; name = Release; }; diff --git a/MetalParticles/AppDelegate.swift b/MetalParticles/AppDelegate.swift index b3751fc..1c2e055 100644 --- a/MetalParticles/AppDelegate.swift +++ b/MetalParticles/AppDelegate.swift @@ -13,34 +13,9 @@ 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: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { return true } - 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) { - // 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) { - // 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) { - // 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) { - // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. - } - - } diff --git a/MetalParticles/Base.lproj/Main.storyboard b/MetalParticles/Base.lproj/Main.storyboard index 3a2a49b..b245ad7 100644 --- a/MetalParticles/Base.lproj/Main.storyboard +++ b/MetalParticles/Base.lproj/Main.storyboard @@ -1,21 +1,26 @@ - - + + + + + - + + + - + - + - + diff --git a/MetalParticles/MarkerWidget.swift b/MetalParticles/MarkerWidget.swift index d511f66..909666a 100644 --- a/MetalParticles/MarkerWidget.swift +++ b/MetalParticles/MarkerWidget.swift @@ -24,11 +24,11 @@ class Circle: CAShapeLayer { func draw() { - fillColor = UIColor.lightGrayColor().CGColor + fillColor = UIColor.lightGray.cgColor let ballRect = CGRect(x: -10, y: -10, width: 20, height: 20) - let ballPath = UIBezierPath(ovalInRect: ballRect) + let ballPath = UIBezierPath(ovalIn: ballRect) - path = ballPath.CGPath + path = ballPath.cgPath } -} \ No newline at end of file +} diff --git a/MetalParticles/ParticleLab.swift b/MetalParticles/ParticleLab.swift index c775ace..2964b9a 100644 --- a/MetalParticles/ParticleLab.swift +++ b/MetalParticles/ParticleLab.swift @@ -47,8 +47,8 @@ class ParticleLab: MTKView let alignment:Int = 0x4000 let particlesMemoryByteSize:Int - private var particlesMemory:UnsafeMutablePointer = nil - private var particlesVoidPtr: COpaquePointer! + private var particlesMemory:UnsafeMutableRawPointer? = nil + private var particlesVoidPtr: OpaquePointer! private var particlesParticlePtr: UnsafeMutablePointer! private var particlesParticleBufferPtr: UnsafeMutableBufferPointer! @@ -59,7 +59,7 @@ class ParticleLab: MTKView private var frameStartTime: CFAbsoluteTime! private var frameNumber = 0 - let particleSize = sizeof(Particle) + let particleSize = MemoryLayout.size weak var particleLabDelegate: ParticleLabDelegate? @@ -94,17 +94,17 @@ class ParticleLab: MTKView bytesPerRow = 4 * imageWidth region = MTLRegionMake2D(0, 0, Int(imageWidth), Int(imageHeight)) - blankBitmapRawData = [UInt8](count: Int(imageWidth * imageHeight * 4), repeatedValue: 0) - particlesMemoryByteSize = particleCount * sizeof(Particle) + blankBitmapRawData = [UInt8](repeating: 0, count: Int(imageWidth * imageHeight * 4)) + particlesMemoryByteSize = particleCount * MemoryLayout.size - let formatter = NSNumberFormatter() + let formatter = NumberFormatter() formatter.usesGroupingSeparator = true - formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle + formatter.numberStyle = .decimal - statusPrefix = formatter.stringFromNumber(numParticles.rawValue * 4)! + " Particles" + statusPrefix = "\(numParticles.rawValue * 4) Particles" - let frameWidth = hiDPI ? width / UInt(UIScreen.mainScreen().scale) : width - let frameHeight = hiDPI ? height / UInt(UIScreen.mainScreen().scale) : height + let frameWidth = hiDPI ? width / UInt(UIScreen.main.scale) : width + let frameHeight = hiDPI ? height / UInt(UIScreen.main.scale) : height super.init(frame: CGRect(x: 0, y: 0, width: Int(frameWidth), height: Int(frameHeight)), device: MTLCreateSystemDefaultDevice()) @@ -115,7 +115,7 @@ class ParticleLab: MTKView setUpMetal() - multipleTouchEnabled = true + isMultipleTouchEnabled = true } required init(coder: NSCoder) @@ -133,11 +133,11 @@ class ParticleLab: MTKView { posix_memalign(&particlesMemory, alignment, particlesMemoryByteSize) - particlesVoidPtr = COpaquePointer(particlesMemory) + particlesVoidPtr = OpaquePointer(particlesMemory) particlesParticlePtr = UnsafeMutablePointer(particlesVoidPtr) particlesParticleBufferPtr = UnsafeMutableBufferPointer(start: particlesParticlePtr, count: particleCount) - resetParticles(true) + resetParticles(edgesOnly: true) } func resetGravityWells() @@ -226,14 +226,14 @@ class ParticleLab: MTKView return } - defaultLibrary = device.newDefaultLibrary() - commandQueue = device.newCommandQueue() + defaultLibrary = device.makeDefaultLibrary() + commandQueue = device.makeCommandQueue() - kernelFunction = defaultLibrary.newFunctionWithName("particleRendererShader") + kernelFunction = defaultLibrary.makeFunction(name: "particleRendererShader") do { - try pipelineState = device.newComputePipelineStateWithFunction(kernelFunction!) + try pipelineState = device.makeComputePipelineState(function: kernelFunction!) } catch { @@ -250,12 +250,12 @@ class ParticleLab: MTKView var imageWidthFloat = Float(imageWidth) var imageHeightFloat = Float(imageHeight) - imageWidthFloatBuffer = device.newBufferWithBytes(&imageWidthFloat, length: sizeof(Float), options: MTLResourceOptions.CPUCacheModeDefaultCache) + imageWidthFloatBuffer = device.makeBuffer(bytes: &imageWidthFloat, length: MemoryLayout.size, options: []) - imageHeightFloatBuffer = device.newBufferWithBytes(&imageHeightFloat, length: sizeof(Float), options: MTLResourceOptions.CPUCacheModeDefaultCache) + imageHeightFloatBuffer = device.makeBuffer(bytes: &imageHeightFloat, length: MemoryLayout.size, options: []) } - override func drawRect(dirtyRect: CGRect) + override func draw(_ dirtyRect: CGRect) { guard let device = device else { @@ -277,31 +277,31 @@ class ParticleLab: MTKView frameNumber = 0 } - let commandBuffer = commandQueue.commandBuffer() - let commandEncoder = commandBuffer.computeCommandEncoder() + guard let commandBuffer = commandQueue.makeCommandBuffer(), + let commandEncoder = commandBuffer.makeComputeCommandEncoder() else { return } commandEncoder.setComputePipelineState(pipelineState) - let particlesBufferNoCopy = device.newBufferWithBytesNoCopy(particlesMemory, length: Int(particlesMemoryByteSize), - options: MTLResourceOptions.CPUCacheModeDefaultCache, deallocator: nil) + let particlesBufferNoCopy = device.makeBuffer(bytesNoCopy: particlesMemory!, length: Int(particlesMemoryByteSize), + options: [], deallocator: nil) - commandEncoder.setBuffer(particlesBufferNoCopy, offset: 0, atIndex: 0) - commandEncoder.setBuffer(particlesBufferNoCopy, offset: 0, atIndex: 1) + commandEncoder.setBuffer(particlesBufferNoCopy, offset: 0, index: 0) + commandEncoder.setBuffer(particlesBufferNoCopy, offset: 0, index: 1) - let inGravityWell = device.newBufferWithBytes(&gravityWellParticle, length: particleSize, options: MTLResourceOptions.CPUCacheModeDefaultCache) - commandEncoder.setBuffer(inGravityWell, offset: 0, atIndex: 2) + let inGravityWell = device.makeBuffer(bytes: &gravityWellParticle, length: particleSize, options: []) + commandEncoder.setBuffer(inGravityWell, offset: 0, index: 2) - let colorBuffer = device.newBufferWithBytes(&particleColor, length: sizeof(ParticleColor), options: MTLResourceOptions.CPUCacheModeDefaultCache) - commandEncoder.setBuffer(colorBuffer, offset: 0, atIndex: 3) + let colorBuffer = device.makeBuffer(bytes: &particleColor, length: MemoryLayout.size, options: []) + commandEncoder.setBuffer(colorBuffer, offset: 0, index: 3) - commandEncoder.setBuffer(imageWidthFloatBuffer, offset: 0, atIndex: 4) - commandEncoder.setBuffer(imageHeightFloatBuffer, offset: 0, atIndex: 5) + commandEncoder.setBuffer(imageWidthFloatBuffer, offset: 0, index: 4) + commandEncoder.setBuffer(imageHeightFloatBuffer, offset: 0, index: 5) - let dragFactorBuffer = device.newBufferWithBytes(&dragFactor, length: sizeof(Float), options: MTLResourceOptions.CPUCacheModeDefaultCache) - commandEncoder.setBuffer(dragFactorBuffer, offset: 0, atIndex: 6) + let dragFactorBuffer = device.makeBuffer(bytes: &dragFactor, length: MemoryLayout.size, options: []) + commandEncoder.setBuffer(dragFactorBuffer, offset: 0, index: 6) - let respawnOutOfBoundsParticlesBuffer = device.newBufferWithBytes(&respawnOutOfBoundsParticles, length: sizeof(Bool), options: MTLResourceOptions.CPUCacheModeDefaultCache) - commandEncoder.setBuffer(respawnOutOfBoundsParticlesBuffer, offset: 0, atIndex: 7) + let respawnOutOfBoundsParticlesBuffer = device.makeBuffer(bytes: &respawnOutOfBoundsParticles, length: MemoryLayout.size, options: []) + commandEncoder.setBuffer(respawnOutOfBoundsParticlesBuffer, offset: 0, index: 7) guard let drawable = currentDrawable else { @@ -314,14 +314,13 @@ class ParticleLab: MTKView if clearOnStep { - drawable.texture.replaceRegion(self.region, + drawable.texture.replace(region: self.region, mipmapLevel: 0, withBytes: blankBitmapRawData, bytesPerRow: Int(bytesPerRow)) } - - commandEncoder.setTexture(drawable.texture, atIndex: 0) + commandEncoder.setTexture(drawable.texture, index: 0) commandEncoder.dispatchThreadgroups(threadgroupsPerGrid, threadsPerThreadgroup: threadsPerThreadgroup) @@ -329,26 +328,21 @@ class ParticleLab: MTKView if !clearOnStep { - let inPlaceTexture = UnsafeMutablePointer.alloc(1) - inPlaceTexture.initialize(drawable.texture) - - blur.encodeToCommandBuffer(commandBuffer, - inPlaceTexture: inPlaceTexture, - fallbackCopyAllocator: nil) + let inPlaceTexture = UnsafeMutablePointer.allocate(capacity: 1) + inPlaceTexture.initialize(to: drawable.texture) - erode.encodeToCommandBuffer(commandBuffer, - inPlaceTexture: inPlaceTexture, - fallbackCopyAllocator: nil) + blur.encode(commandBuffer: commandBuffer, inPlaceTexture: inPlaceTexture, fallbackCopyAllocator: nil) + erode.encode(commandBuffer: commandBuffer, inPlaceTexture: inPlaceTexture, fallbackCopyAllocator: nil) } commandBuffer.commit() drawable.present() - particleLabDelegate?.particleLabDidUpdate(statusPrefix + statusPostix) + particleLabDelegate?.particleLabDidUpdate(status: statusPrefix + statusPostix) } - final func getGravityWellNormalisedPosition(gravityWell gravityWell: GravityWell) -> (x: Float, y: Float) + final func getGravityWellNormalisedPosition(gravityWell: GravityWell) -> (x: Float, y: Float) { let returnPoint: (x: Float, y: Float) @@ -373,7 +367,7 @@ class ParticleLab: MTKView return returnPoint } - final func setGravityWellProperties(gravityWellIndex gravityWellIndex: Int, normalisedPositionX: Float, normalisedPositionY: Float, mass: Float, spin: Float) + final func setGravityWellProperties(gravityWellIndex: Int, normalisedPositionX: Float, normalisedPositionY: Float, mass: Float, spin: Float) { switch gravityWellIndex { @@ -391,7 +385,7 @@ class ParticleLab: MTKView } } - final func setGravityWellProperties(gravityWell gravityWell: GravityWell, normalisedPositionX: Float, normalisedPositionY: Float, mass: Float, spin: Float) + final func setGravityWellProperties(gravityWell: GravityWell, normalisedPositionX: Float, normalisedPositionY: Float, mass: Float, spin: Float) { let imageWidthFloat = Float(imageWidth) let imageHeightFloat = Float(imageHeight) diff --git a/MetalParticles/ViewController.swift b/MetalParticles/ViewController.swift index c078c8c..16aa946 100644 --- a/MetalParticles/ViewController.swift +++ b/MetalParticles/ViewController.swift @@ -30,7 +30,7 @@ class ViewController: UIViewController, ParticleLabDelegate let menuButton = UIButton() let statusLabel = UILabel() - let floatPi = Float(M_PI) + let floatPi = Float(Double.pi) let hiDPI = false @@ -46,16 +46,16 @@ class ViewController: UIViewController, ParticleLabDelegate { super.viewDidLoad() - view.backgroundColor = UIColor.blackColor() + view.backgroundColor = UIColor.black - print(UIScreen.mainScreen().scale) + print(UIScreen.main.scale) let numParticles = ParticleCount.EightMillion if hiDPI { - particleLab = ParticleLab(width: UInt(view.frame.width * UIScreen.mainScreen().scale), - height: UInt(view.frame.height * UIScreen.mainScreen().scale), + particleLab = ParticleLab(width: UInt(view.frame.width * UIScreen.main.scale), + height: UInt(view.frame.height * UIScreen.main.scale), numParticles: numParticles, hiDPI: true) } @@ -79,19 +79,19 @@ class ViewController: UIViewController, ParticleLabDelegate view.addSubview(particleLab) - menuButton.layer.borderColor = UIColor.lightGrayColor().CGColor + menuButton.layer.borderColor = UIColor.lightGray.cgColor menuButton.layer.borderWidth = 1 menuButton.layer.cornerRadius = 5 - menuButton.layer.backgroundColor = UIColor.darkGrayColor().CGColor + menuButton.layer.backgroundColor = UIColor.darkGray.cgColor menuButton.showsTouchWhenHighlighted = true - menuButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit - menuButton.setImage(UIImage(named: "hamburger.png"), forState: UIControlState.Normal) - menuButton.addTarget(self, action: #selector(ViewController.displayCallout), forControlEvents: UIControlEvents.TouchDown) + menuButton.imageView?.contentMode = UIView.ContentMode.scaleAspectFit + menuButton.setImage(UIImage(named: "hamburger.png"), for: .normal) + menuButton.addTarget(self, action: #selector(ViewController.displayCallout), for: .touchDown) view.addSubview(menuButton) statusLabel.text = "http://flexmonkey.blogspot.co.uk" - statusLabel.textColor = UIColor.darkGrayColor() + statusLabel.textColor = UIColor.darkGray view.addSubview(statusLabel) } @@ -99,9 +99,9 @@ class ViewController: UIViewController, ParticleLabDelegate override func viewDidLayoutSubviews() { statusLabel.frame = CGRect(x: 5, - y: view.frame.height - statusLabel.intrinsicContentSize().height, + y: view.frame.height - statusLabel.intrinsicContentSize.height, width: view.frame.width, - height: statusLabel.intrinsicContentSize().height) + height: statusLabel.intrinsicContentSize.height) menuButton.frame = CGRect(x: view.frame.width - 35, y: view.frame.height - 35, @@ -114,25 +114,25 @@ class ViewController: UIViewController, ParticleLabDelegate // handle metal unavailable here } - override func touchesBegan(touches: Set, withEvent event: UIEvent?) + override func touchesBegan(_ touches: Set, with event: UIEvent?) { currentTouches = currentTouches.union(touches) } - override func touchesEnded(touches: Set, withEvent event: UIEvent?) + override func touchesEnded(_ touches: Set, with event: UIEvent?) { - currentTouches = currentTouches.subtract(touches) + currentTouches.subtract(touches) } - func displayCallout() + @objc func displayCallout() { - let alertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) + let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) - let cloudChamberAction = UIAlertAction(title: DemoModes.cloudChamber.rawValue, style: UIAlertActionStyle.Default, handler: calloutActionHandler) - let orbitsAction = UIAlertAction(title: DemoModes.orbits.rawValue, style: UIAlertActionStyle.Default, handler: calloutActionHandler) - let multiTouchAction = UIAlertAction(title: DemoModes.multiTouch.rawValue, style: UIAlertActionStyle.Default, handler: calloutActionHandler) - let respawnAction = UIAlertAction(title: DemoModes.respawn.rawValue, style: UIAlertActionStyle.Default, handler: calloutActionHandler) - let iPadProAction = UIAlertAction(title: DemoModes.iPadProDemo.rawValue, style: UIAlertActionStyle.Default, handler: calloutActionHandler) + let cloudChamberAction = UIAlertAction(title: DemoModes.cloudChamber.rawValue, style: .default, handler: calloutActionHandler) + let orbitsAction = UIAlertAction(title: DemoModes.orbits.rawValue, style: .default, handler: calloutActionHandler) + let multiTouchAction = UIAlertAction(title: DemoModes.multiTouch.rawValue, style: .default, handler: calloutActionHandler) + let respawnAction = UIAlertAction(title: DemoModes.respawn.rawValue, style: .default, handler: calloutActionHandler) + let iPadProAction = UIAlertAction(title: DemoModes.iPadProDemo.rawValue, style: .default, handler: calloutActionHandler) alertController.addAction(cloudChamberAction) alertController.addAction(orbitsAction) @@ -149,9 +149,9 @@ class ViewController: UIViewController, ParticleLabDelegate popoverPresentationController.sourceView = view } - particleLab.paused = true + particleLab.isPaused = true - presentViewController(alertController, animated: true, completion: {self.particleLab.paused = false}) + present(alertController, animated: true, completion: {self.particleLab.isPaused = false}) } func calloutActionHandler(value: UIAlertAction!) -> Void @@ -164,31 +164,31 @@ class ViewController: UIViewController, ParticleLabDelegate particleLab.dragFactor = 0.82 particleLab.respawnOutOfBoundsParticles = true particleLab.clearOnStep = true - particleLab.resetParticles(false) + particleLab.resetParticles(edgesOnly: false) case .cloudChamber: particleLab.dragFactor = 0.8 particleLab.respawnOutOfBoundsParticles = false particleLab.clearOnStep = true - particleLab.resetParticles(true) + particleLab.resetParticles(edgesOnly: true) case .multiTouch: particleLab.dragFactor = 0.95 particleLab.respawnOutOfBoundsParticles = false particleLab.clearOnStep = true - particleLab.resetParticles(false) + particleLab.resetParticles(edgesOnly: false) case .respawn: particleLab.dragFactor = 0.98 particleLab.respawnOutOfBoundsParticles = true particleLab.clearOnStep = true - particleLab.resetParticles(true) + particleLab.resetParticles(edgesOnly: true) case .iPadProDemo: particleLab.dragFactor = 0.5 particleLab.respawnOutOfBoundsParticles = true particleLab.clearOnStep = false - particleLab.resetParticles(true) + particleLab.resetParticles(edgesOnly: true) } } @@ -239,15 +239,15 @@ class ViewController: UIViewController, ParticleLabDelegate { let currentTouchesArray = Array(currentTouches) - for (i, currentTouch) in currentTouchesArray.enumerate() where i < 4 + for (i, currentTouch) in currentTouchesArray.enumerated() where i < 4 { let touchMultiplier = currentTouch.force == 0 && currentTouch.maximumPossibleForce == 0 ? 1 : Float(currentTouch.force / currentTouch.maximumPossibleForce) particleLab.setGravityWellProperties(gravityWellIndex: i, - normalisedPositionX: Float(currentTouch.locationInView(view).x / view.frame.width) , - normalisedPositionY: Float(currentTouch.locationInView(view).y / view.frame.height), + normalisedPositionX: Float(currentTouch.location(in: view).x / view.frame.width) , + normalisedPositionY: Float(currentTouch.location(in: view).y / view.frame.height), mass: 40 * touchMultiplier, spin: 20 * touchMultiplier) } @@ -357,16 +357,12 @@ class ViewController: UIViewController, ParticleLabDelegate mass: 26, spin: -19 * sin(gravityWellAngle * 1.5)) } - - override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask - { - return UIInterfaceOrientationMask.Landscape + override var supportedInterfaceOrientations: UIInterfaceOrientationMask { + return .landscape } - - override func preferredStatusBarStyle() -> UIStatusBarStyle - { - return UIStatusBarStyle.LightContent + override var preferredStatusBarStyle: UIStatusBarStyle { + return .lightContent } override func didReceiveMemoryWarning() @@ -375,8 +371,7 @@ class ViewController: UIViewController, ParticleLabDelegate // Dispose of any resources that can be recreated. } - override func prefersStatusBarHidden() -> Bool - { + override var prefersStatusBarHidden: Bool { return true } } diff --git a/MetalParticlesTests/MetalParticlesTests.swift b/MetalParticlesTests/MetalParticlesTests.swift index 2c67dd4..c893803 100644 --- a/MetalParticlesTests/MetalParticlesTests.swift +++ b/MetalParticlesTests/MetalParticlesTests.swift @@ -28,7 +28,7 @@ class MetalParticlesTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock() { + self.measure() { // Put the code you want to measure the time of here. } }