diff --git a/README.md b/README.md index d2c542c..8dd05a8 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,5 @@ ASHSpringyCollectionView This is an example of how to use UIKit Dynamics with UICollectionViews while employing a tiling system for efficiency even when scrolling through thousands of items. ![Example](http://f.cl.ly/items/411o450x2E3A3c3m2b3k/springyCollectionView.gif) + +A Swift version can be found in the folder /SWIFT. \ No newline at end of file diff --git a/SWIFT/CollectionViewUIDynamics.xcodeproj/project.pbxproj b/SWIFT/CollectionViewUIDynamics.xcodeproj/project.pbxproj new file mode 100644 index 0000000..4745240 --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics.xcodeproj/project.pbxproj @@ -0,0 +1,428 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + A167354D1AC8064500EAA3CC /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A167354C1AC8064500EAA3CC /* AppDelegate.swift */; }; + A167354F1AC8064500EAA3CC /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A167354E1AC8064500EAA3CC /* ViewController.swift */; }; + A16735521AC8064500EAA3CC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A16735501AC8064500EAA3CC /* Main.storyboard */; }; + A16735541AC8064500EAA3CC /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A16735531AC8064500EAA3CC /* Images.xcassets */; }; + A16735571AC8064500EAA3CC /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = A16735551AC8064500EAA3CC /* LaunchScreen.xib */; }; + A16735631AC8064500EAA3CC /* CollectionViewUIDynamicsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A16735621AC8064500EAA3CC /* CollectionViewUIDynamicsTests.swift */; }; + A167356D1AC8066300EAA3CC /* CollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A167356C1AC8066300EAA3CC /* CollectionViewController.swift */; }; + A167356F1AC8067D00EAA3CC /* MyFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = A167356E1AC8067D00EAA3CC /* MyFlowLayout.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + A167355D1AC8064500EAA3CC /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = A167353F1AC8064500EAA3CC /* Project object */; + proxyType = 1; + remoteGlobalIDString = A16735461AC8064500EAA3CC; + remoteInfo = CollectionViewUIDynamics; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + A16735471AC8064500EAA3CC /* CollectionViewUIDynamics.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CollectionViewUIDynamics.app; sourceTree = BUILT_PRODUCTS_DIR; }; + A167354B1AC8064500EAA3CC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + A167354C1AC8064500EAA3CC /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; + A167354E1AC8064500EAA3CC /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + A16735511AC8064500EAA3CC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + A16735531AC8064500EAA3CC /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; + A16735561AC8064500EAA3CC /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; + A167355C1AC8064500EAA3CC /* CollectionViewUIDynamicsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CollectionViewUIDynamicsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + A16735611AC8064500EAA3CC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + A16735621AC8064500EAA3CC /* CollectionViewUIDynamicsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewUIDynamicsTests.swift; sourceTree = ""; }; + A167356C1AC8066300EAA3CC /* CollectionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewController.swift; sourceTree = ""; }; + A167356E1AC8067D00EAA3CC /* MyFlowLayout.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MyFlowLayout.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + A16735441AC8064500EAA3CC /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + A16735591AC8064500EAA3CC /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + A167353E1AC8064500EAA3CC = { + isa = PBXGroup; + children = ( + A16735491AC8064500EAA3CC /* CollectionViewUIDynamics */, + A167355F1AC8064500EAA3CC /* CollectionViewUIDynamicsTests */, + A16735481AC8064500EAA3CC /* Products */, + ); + sourceTree = ""; + }; + A16735481AC8064500EAA3CC /* Products */ = { + isa = PBXGroup; + children = ( + A16735471AC8064500EAA3CC /* CollectionViewUIDynamics.app */, + A167355C1AC8064500EAA3CC /* CollectionViewUIDynamicsTests.xctest */, + ); + name = Products; + sourceTree = ""; + }; + A16735491AC8064500EAA3CC /* CollectionViewUIDynamics */ = { + isa = PBXGroup; + children = ( + A167354C1AC8064500EAA3CC /* AppDelegate.swift */, + A167354E1AC8064500EAA3CC /* ViewController.swift */, + A167356C1AC8066300EAA3CC /* CollectionViewController.swift */, + A167356E1AC8067D00EAA3CC /* MyFlowLayout.swift */, + A16735501AC8064500EAA3CC /* Main.storyboard */, + A16735531AC8064500EAA3CC /* Images.xcassets */, + A16735551AC8064500EAA3CC /* LaunchScreen.xib */, + A167354A1AC8064500EAA3CC /* Supporting Files */, + ); + path = CollectionViewUIDynamics; + sourceTree = ""; + }; + A167354A1AC8064500EAA3CC /* Supporting Files */ = { + isa = PBXGroup; + children = ( + A167354B1AC8064500EAA3CC /* Info.plist */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; + A167355F1AC8064500EAA3CC /* CollectionViewUIDynamicsTests */ = { + isa = PBXGroup; + children = ( + A16735621AC8064500EAA3CC /* CollectionViewUIDynamicsTests.swift */, + A16735601AC8064500EAA3CC /* Supporting Files */, + ); + path = CollectionViewUIDynamicsTests; + sourceTree = ""; + }; + A16735601AC8064500EAA3CC /* Supporting Files */ = { + isa = PBXGroup; + children = ( + A16735611AC8064500EAA3CC /* Info.plist */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + A16735461AC8064500EAA3CC /* CollectionViewUIDynamics */ = { + isa = PBXNativeTarget; + buildConfigurationList = A16735661AC8064500EAA3CC /* Build configuration list for PBXNativeTarget "CollectionViewUIDynamics" */; + buildPhases = ( + A16735431AC8064500EAA3CC /* Sources */, + A16735441AC8064500EAA3CC /* Frameworks */, + A16735451AC8064500EAA3CC /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = CollectionViewUIDynamics; + productName = CollectionViewUIDynamics; + productReference = A16735471AC8064500EAA3CC /* CollectionViewUIDynamics.app */; + productType = "com.apple.product-type.application"; + }; + A167355B1AC8064500EAA3CC /* CollectionViewUIDynamicsTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = A16735691AC8064500EAA3CC /* Build configuration list for PBXNativeTarget "CollectionViewUIDynamicsTests" */; + buildPhases = ( + A16735581AC8064500EAA3CC /* Sources */, + A16735591AC8064500EAA3CC /* Frameworks */, + A167355A1AC8064500EAA3CC /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + A167355E1AC8064500EAA3CC /* PBXTargetDependency */, + ); + name = CollectionViewUIDynamicsTests; + productName = CollectionViewUIDynamicsTests; + productReference = A167355C1AC8064500EAA3CC /* CollectionViewUIDynamicsTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + A167353F1AC8064500EAA3CC /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0620; + ORGANIZATIONNAME = Anerma; + TargetAttributes = { + A16735461AC8064500EAA3CC = { + CreatedOnToolsVersion = 6.2; + }; + A167355B1AC8064500EAA3CC = { + CreatedOnToolsVersion = 6.2; + TestTargetID = A16735461AC8064500EAA3CC; + }; + }; + }; + buildConfigurationList = A16735421AC8064500EAA3CC /* Build configuration list for PBXProject "CollectionViewUIDynamics" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = A167353E1AC8064500EAA3CC; + productRefGroup = A16735481AC8064500EAA3CC /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + A16735461AC8064500EAA3CC /* CollectionViewUIDynamics */, + A167355B1AC8064500EAA3CC /* CollectionViewUIDynamicsTests */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + A16735451AC8064500EAA3CC /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A16735521AC8064500EAA3CC /* Main.storyboard in Resources */, + A16735571AC8064500EAA3CC /* LaunchScreen.xib in Resources */, + A16735541AC8064500EAA3CC /* Images.xcassets in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + A167355A1AC8064500EAA3CC /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + A16735431AC8064500EAA3CC /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A167356D1AC8066300EAA3CC /* CollectionViewController.swift in Sources */, + A167356F1AC8067D00EAA3CC /* MyFlowLayout.swift in Sources */, + A167354F1AC8064500EAA3CC /* ViewController.swift in Sources */, + A167354D1AC8064500EAA3CC /* AppDelegate.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + A16735581AC8064500EAA3CC /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A16735631AC8064500EAA3CC /* CollectionViewUIDynamicsTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + A167355E1AC8064500EAA3CC /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = A16735461AC8064500EAA3CC /* CollectionViewUIDynamics */; + targetProxy = A167355D1AC8064500EAA3CC /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin PBXVariantGroup section */ + A16735501AC8064500EAA3CC /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + A16735511AC8064500EAA3CC /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + A16735551AC8064500EAA3CC /* LaunchScreen.xib */ = { + isa = PBXVariantGroup; + children = ( + A16735561AC8064500EAA3CC /* Base */, + ); + name = LaunchScreen.xib; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + A16735641AC8064500EAA3CC /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = 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_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_SYMBOLS_PRIVATE_EXTERN = NO; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 8.2; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + A16735651AC8064500EAA3CC /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = 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_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 8.2; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + A16735671AC8064500EAA3CC /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + INFOPLIST_FILE = CollectionViewUIDynamics/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + A16735681AC8064500EAA3CC /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + INFOPLIST_FILE = CollectionViewUIDynamics/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; + A167356A1AC8064500EAA3CC /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + FRAMEWORK_SEARCH_PATHS = ( + "$(SDKROOT)/Developer/Library/Frameworks", + "$(inherited)", + ); + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + INFOPLIST_FILE = CollectionViewUIDynamicsTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_NAME = "$(TARGET_NAME)"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CollectionViewUIDynamics.app/CollectionViewUIDynamics"; + }; + name = Debug; + }; + A167356B1AC8064500EAA3CC /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + FRAMEWORK_SEARCH_PATHS = ( + "$(SDKROOT)/Developer/Library/Frameworks", + "$(inherited)", + ); + INFOPLIST_FILE = CollectionViewUIDynamicsTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_NAME = "$(TARGET_NAME)"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/CollectionViewUIDynamics.app/CollectionViewUIDynamics"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + A16735421AC8064500EAA3CC /* Build configuration list for PBXProject "CollectionViewUIDynamics" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A16735641AC8064500EAA3CC /* Debug */, + A16735651AC8064500EAA3CC /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + A16735661AC8064500EAA3CC /* Build configuration list for PBXNativeTarget "CollectionViewUIDynamics" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A16735671AC8064500EAA3CC /* Debug */, + A16735681AC8064500EAA3CC /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; + A16735691AC8064500EAA3CC /* Build configuration list for PBXNativeTarget "CollectionViewUIDynamicsTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + A167356A1AC8064500EAA3CC /* Debug */, + A167356B1AC8064500EAA3CC /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = A167353F1AC8064500EAA3CC /* Project object */; +} diff --git a/SWIFT/CollectionViewUIDynamics/AppDelegate.swift b/SWIFT/CollectionViewUIDynamics/AppDelegate.swift new file mode 100644 index 0000000..4db37fa --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/AppDelegate.swift @@ -0,0 +1,46 @@ +// +// AppDelegate.swift +// CollectionViewUIDynamics +// +// Created by Andreas Neusüß on 29.03.15. +// Copyright (c) 2015 Anerma. All rights reserved. +// + +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. + 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/SWIFT/CollectionViewUIDynamics/Base.lproj/LaunchScreen.xib b/SWIFT/CollectionViewUIDynamics/Base.lproj/LaunchScreen.xib new file mode 100644 index 0000000..d611b82 --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/Base.lproj/LaunchScreen.xib @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SWIFT/CollectionViewUIDynamics/Base.lproj/Main.storyboard b/SWIFT/CollectionViewUIDynamics/Base.lproj/Main.storyboard new file mode 100644 index 0000000..ec22c8c --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/Base.lproj/Main.storyboard @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SWIFT/CollectionViewUIDynamics/CollectionViewController.swift b/SWIFT/CollectionViewUIDynamics/CollectionViewController.swift new file mode 100644 index 0000000..22e9f2a --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/CollectionViewController.swift @@ -0,0 +1,94 @@ +// +// CollectionViewController.swift +// CollectionViewUIDynamics +// +// Created by Andreas Neusüß on 29.03.15. +// Copyright (c) 2015 Anerma. All rights reserved. +// + +import UIKit + +let reuseIdentifier = "MyCell" + +class CollectionViewController: UICollectionViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + // Uncomment the following line to preserve selection between presentations + // self.clearsSelectionOnViewWillAppear = false + + // Register cell classes + self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier) + + // Do any additional setup after loading the view. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + /* + // MARK: - Navigation + + // In a storyboard-based application, you will often want to do a little preparation before navigation + override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { + // Get the new view controller using [segue destinationViewController]. + // Pass the selected object to the new view controller. + } + */ + + // MARK: UICollectionViewDataSource + + override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { + + return 1 + } + + + override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return 10000 + } + + override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { + let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as UICollectionViewCell + + // Configure the cell + cell.backgroundColor = UIColor.orangeColor() + + return cell + } + + // MARK: UICollectionViewDelegate + + /* + // Uncomment this method to specify if the specified item should be highlighted during tracking + override func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool { + return true + } + */ + + /* + // Uncomment this method to specify if the specified item should be selected + override func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool { + return true + } + */ + + /* + // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item + override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool { + return false + } + + override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool { + return false + } + + override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) { + + } + */ + +} diff --git a/SWIFT/CollectionViewUIDynamics/Images.xcassets/AppIcon.appiconset/Contents.json b/SWIFT/CollectionViewUIDynamics/Images.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..118c98f --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/Images.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,38 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/SWIFT/CollectionViewUIDynamics/Info.plist b/SWIFT/CollectionViewUIDynamics/Info.plist new file mode 100644 index 0000000..701ec63 --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/Info.plist @@ -0,0 +1,40 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + de.anerma.$(PRODUCT_NAME:rfc1034identifier) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/SWIFT/CollectionViewUIDynamics/MyFlowLayout.swift b/SWIFT/CollectionViewUIDynamics/MyFlowLayout.swift new file mode 100644 index 0000000..094f460 --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/MyFlowLayout.swift @@ -0,0 +1,151 @@ +// +// MyFlowLayout.swift +// CollectionViewUIDynamics +// +// Created by Andreas Neusüß on 29.03.15. +// Copyright (c) 2015 Anerma. All rights reserved. +// + +import UIKit + +class MyFlowLayout: UICollectionViewFlowLayout { + private var dynamicAnimator : UIDynamicAnimator! + private var visibleIndexPaths : NSMutableSet = NSMutableSet() + private var latestDelta : CGFloat! + + + override init () { + super.init() + + setUp() + } + + required init(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + setUp() + } + + func setUp() { + self.minimumInteritemSpacing = 20 + self.minimumLineSpacing = 20 + self.itemSize = CGSizeMake(44, 44) + self.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) + + self.dynamicAnimator = UIDynamicAnimator(collectionViewLayout: self) + } + + + override func prepareLayout() { + super.prepareLayout() + + // Need to overflow our actual visible rect slightly to avoid flickering. + let visibleRect = CGRectInset(CGRect(origin: collectionView!.bounds.origin, size: collectionView!.bounds.size), -100, -100)//rect to setup the behaviors in – bigger rect: more behaviors calculated. Can be used to fight against too fast scrolling. + let itemsInVisibleRect = super.layoutAttributesForElementsInRect(visibleRect) as [UICollectionViewLayoutAttributes] + + + //some dance to get the indexPath property from the itemsInVisibleRect (Array of UICollectionViewLayoutAttributes) + let obj = itemsInVisibleRect as NSObject + let arr = obj.valueForKey("indexPath") as NSArray + let itemsIndexPathInVisibleRect = NSSet(array: arr) + + // Step 1: Remove any behaviours that are no longer visible. + let noLongerVisibleBehaviors = dynamicAnimator.behaviors.filter( { (obj : AnyObject)->Bool in + let behavior = obj as? UIAttachmentBehavior + let item = behavior?.items.last as? UICollectionViewLayoutAttributes + + if let indexPathOfItem = item?.indexPath { + let currentlyVisible : Bool = itemsIndexPathInVisibleRect.member(indexPathOfItem) != nil + + return !currentlyVisible + } + + return false + }) + + //Remove the behaviors of cells that are not visible + for (index, behavior : UIAttachmentBehavior) in enumerate(noLongerVisibleBehaviors as [UIAttachmentBehavior]) { + dynamicAnimator.removeBehavior(behavior) + if let item = behavior.items.first as? UICollectionViewLayoutAttributes { + visibleIndexPaths.removeObject(item.indexPath) + } + } + + // Step 2: Add any newly visible behaviours. + // A "newly visible" item is one that is in the itemsInVisibleRect(Set|Array) but not in the visibleIndexPathsSet + let newlyVisibleItems = itemsInVisibleRect.filter( { (item : AnyObject) -> Bool in + let layoutAttribute = item as? UICollectionViewLayoutAttributes + + if let indexPath = layoutAttribute?.indexPath? { + let contained : Bool = self.visibleIndexPaths.member(indexPath) != nil + return !contained + } + return false + + }) + + let touchLocation = collectionView!.panGestureRecognizer.locationInView(collectionView) + + for (index, item : UICollectionViewLayoutAttributes) in enumerate(newlyVisibleItems) { + + let springBehavior = UIAttachmentBehavior(item: item, attachedToAnchor: item.center) + springBehavior.length = 0 + springBehavior.damping = 0.8 + springBehavior.frequency = 1 + + // If our touchLocation is not (0,0), we'll need to adjust our item's center "in flight" + if !CGPointEqualToPoint(CGPointZero, touchLocation) { + let xDistanceFromTouch : CGFloat = fabs(touchLocation.x - springBehavior.anchorPoint.x) + let yDistanceFromTouch : CGFloat = fabs(touchLocation.y - springBehavior.anchorPoint.y) + let scrollResistance : CGFloat = (yDistanceFromTouch + xDistanceFromTouch) / 1500.0 + + if latestDelta < 0 { + item.center.y += max(latestDelta, latestDelta * scrollResistance) + } + else { + item.center.y += min(latestDelta, latestDelta * scrollResistance) + } + + } + + dynamicAnimator.addBehavior(springBehavior) + visibleIndexPaths.addObject(item.indexPath) + + + } + } + + override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? { + return dynamicAnimator.itemsInRect(rect) + } + override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! { + return dynamicAnimator.layoutAttributesForCellAtIndexPath(indexPath) + } + + override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { + let delta = newBounds.origin.y - collectionView!.bounds.origin.y + + latestDelta = delta + + let locationOfTouch = collectionView!.panGestureRecognizer.locationInView(collectionView) + + for (index, springBehavior : UIAttachmentBehavior) in enumerate(dynamicAnimator.behaviors as [UIAttachmentBehavior]) { + let yDistanceFromTouch = fabs(locationOfTouch.y - springBehavior.anchorPoint.y) + let xDistanceFromTouch = fabs(locationOfTouch.x - springBehavior.anchorPoint.x) + + let scrollResistance = (yDistanceFromTouch + xDistanceFromTouch) / 1500.0 + + let item = springBehavior.items.first as UICollectionViewLayoutAttributes + + if delta < 0 { + item.center.y += max(delta, delta * scrollResistance) + } + else { + item.center.y -= max(delta, delta + scrollResistance) + } + + dynamicAnimator.updateItemUsingCurrentState(item) + } + + return false + } +} diff --git a/SWIFT/CollectionViewUIDynamics/ViewController.swift b/SWIFT/CollectionViewUIDynamics/ViewController.swift new file mode 100644 index 0000000..7110278 --- /dev/null +++ b/SWIFT/CollectionViewUIDynamics/ViewController.swift @@ -0,0 +1,25 @@ +// +// ViewController.swift +// CollectionViewUIDynamics +// +// Created by Andreas Neusüß on 29.03.15. +// Copyright (c) 2015 Anerma. All rights reserved. +// + +import UIKit + +class ViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + // Do any additional setup after loading the view, typically from a nib. + } + + override func didReceiveMemoryWarning() { + super.didReceiveMemoryWarning() + // Dispose of any resources that can be recreated. + } + + +} + diff --git a/SWIFT/CollectionViewUIDynamicsTests/CollectionViewUIDynamicsTests.swift b/SWIFT/CollectionViewUIDynamicsTests/CollectionViewUIDynamicsTests.swift new file mode 100644 index 0000000..5652b2c --- /dev/null +++ b/SWIFT/CollectionViewUIDynamicsTests/CollectionViewUIDynamicsTests.swift @@ -0,0 +1,36 @@ +// +// CollectionViewUIDynamicsTests.swift +// CollectionViewUIDynamicsTests +// +// Created by Andreas Neusüß on 29.03.15. +// Copyright (c) 2015 Anerma. All rights reserved. +// + +import UIKit +import XCTest + +class CollectionViewUIDynamicsTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDown() { + // Put teardown code here. This method is called after the invocation of each test method in the class. + super.tearDown() + } + + func testExample() { + // This is an example of a functional test case. + XCTAssert(true, "Pass") + } + + func testPerformanceExample() { + // This is an example of a performance test case. + self.measureBlock() { + // Put the code you want to measure the time of here. + } + } + +} diff --git a/SWIFT/CollectionViewUIDynamicsTests/Info.plist b/SWIFT/CollectionViewUIDynamicsTests/Info.plist new file mode 100644 index 0000000..bd7e785 --- /dev/null +++ b/SWIFT/CollectionViewUIDynamicsTests/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + de.anerma.$(PRODUCT_NAME:rfc1034identifier) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + +