├── .gitignore ├── LICENSE ├── ParticlesText ├── ParticlesText.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist └── ParticlesText │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── Particle.metal │ ├── ParticlesBuilder.h │ ├── ParticlesBuilder.m │ ├── ParticlesMTKView.h │ ├── ParticlesMTKView.m │ ├── ViewController.h │ ├── ViewController.m │ ├── main.m │ └── problem.zip ├── README.md └── Resources ├── diffusion.gif ├── shake.gif └── static.gif /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 1EB0672D214BDBA500F0BF1E /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB0672C214BDBA500F0BF1E /* AppDelegate.m */; }; 11 | 1EB06730214BDBA500F0BF1E /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB0672F214BDBA500F0BF1E /* ViewController.m */; }; 12 | 1EB06733214BDBA500F0BF1E /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1EB06731214BDBA500F0BF1E /* Main.storyboard */; }; 13 | 1EB06735214BDBA600F0BF1E /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1EB06734214BDBA600F0BF1E /* Assets.xcassets */; }; 14 | 1EB06738214BDBA600F0BF1E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1EB06736214BDBA600F0BF1E /* LaunchScreen.storyboard */; }; 15 | 1EB0673B214BDBA600F0BF1E /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB0673A214BDBA600F0BF1E /* main.m */; }; 16 | 1EB06748214BDCC100F0BF1E /* ParticlesBuilder.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB06743214BDCC000F0BF1E /* ParticlesBuilder.m */; }; 17 | 1EB06749214BDCC100F0BF1E /* ParticlesMTKView.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EB06744214BDCC000F0BF1E /* ParticlesMTKView.m */; }; 18 | 1EB0674B214BDCC100F0BF1E /* Particle.metal in Sources */ = {isa = PBXBuildFile; fileRef = 1EB06746214BDCC100F0BF1E /* Particle.metal */; }; 19 | 9F3F509C231CC5ED00DC0B5F /* problem.zip in Resources */ = {isa = PBXBuildFile; fileRef = 9F3F509B231CC5ED00DC0B5F /* problem.zip */; }; 20 | /* End PBXBuildFile section */ 21 | 22 | /* Begin PBXFileReference section */ 23 | 1EB06728214BDBA500F0BF1E /* ParticlesText.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ParticlesText.app; sourceTree = BUILT_PRODUCTS_DIR; }; 24 | 1EB0672B214BDBA500F0BF1E /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 25 | 1EB0672C214BDBA500F0BF1E /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 26 | 1EB0672E214BDBA500F0BF1E /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 27 | 1EB0672F214BDBA500F0BF1E /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 28 | 1EB06732214BDBA500F0BF1E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 29 | 1EB06734214BDBA600F0BF1E /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 30 | 1EB06737214BDBA600F0BF1E /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 31 | 1EB06739214BDBA600F0BF1E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 32 | 1EB0673A214BDBA600F0BF1E /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 33 | 1EB06742214BDCC000F0BF1E /* ParticlesBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParticlesBuilder.h; sourceTree = ""; }; 34 | 1EB06743214BDCC000F0BF1E /* ParticlesBuilder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ParticlesBuilder.m; sourceTree = ""; }; 35 | 1EB06744214BDCC000F0BF1E /* ParticlesMTKView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ParticlesMTKView.m; sourceTree = ""; }; 36 | 1EB06746214BDCC100F0BF1E /* Particle.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = Particle.metal; sourceTree = ""; }; 37 | 1EB06747214BDCC100F0BF1E /* ParticlesMTKView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParticlesMTKView.h; sourceTree = ""; }; 38 | 9F3F509B231CC5ED00DC0B5F /* problem.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = problem.zip; sourceTree = ""; }; 39 | /* End PBXFileReference section */ 40 | 41 | /* Begin PBXFrameworksBuildPhase section */ 42 | 1EB06725214BDBA500F0BF1E /* Frameworks */ = { 43 | isa = PBXFrameworksBuildPhase; 44 | buildActionMask = 2147483647; 45 | files = ( 46 | ); 47 | runOnlyForDeploymentPostprocessing = 0; 48 | }; 49 | /* End PBXFrameworksBuildPhase section */ 50 | 51 | /* Begin PBXGroup section */ 52 | 1EB0671F214BDBA500F0BF1E = { 53 | isa = PBXGroup; 54 | children = ( 55 | 1EB0672A214BDBA500F0BF1E /* ParticlesText */, 56 | 1EB06729214BDBA500F0BF1E /* Products */, 57 | ); 58 | sourceTree = ""; 59 | }; 60 | 1EB06729214BDBA500F0BF1E /* Products */ = { 61 | isa = PBXGroup; 62 | children = ( 63 | 1EB06728214BDBA500F0BF1E /* ParticlesText.app */, 64 | ); 65 | name = Products; 66 | sourceTree = ""; 67 | }; 68 | 1EB0672A214BDBA500F0BF1E /* ParticlesText */ = { 69 | isa = PBXGroup; 70 | children = ( 71 | 9F3F509B231CC5ED00DC0B5F /* problem.zip */, 72 | 1EB06746214BDCC100F0BF1E /* Particle.metal */, 73 | 1EB06742214BDCC000F0BF1E /* ParticlesBuilder.h */, 74 | 1EB06743214BDCC000F0BF1E /* ParticlesBuilder.m */, 75 | 1EB06747214BDCC100F0BF1E /* ParticlesMTKView.h */, 76 | 1EB06744214BDCC000F0BF1E /* ParticlesMTKView.m */, 77 | 1EB0672B214BDBA500F0BF1E /* AppDelegate.h */, 78 | 1EB0672C214BDBA500F0BF1E /* AppDelegate.m */, 79 | 1EB0672E214BDBA500F0BF1E /* ViewController.h */, 80 | 1EB0672F214BDBA500F0BF1E /* ViewController.m */, 81 | 1EB06731214BDBA500F0BF1E /* Main.storyboard */, 82 | 1EB06734214BDBA600F0BF1E /* Assets.xcassets */, 83 | 1EB06736214BDBA600F0BF1E /* LaunchScreen.storyboard */, 84 | 1EB06739214BDBA600F0BF1E /* Info.plist */, 85 | 1EB0673A214BDBA600F0BF1E /* main.m */, 86 | ); 87 | path = ParticlesText; 88 | sourceTree = ""; 89 | }; 90 | /* End PBXGroup section */ 91 | 92 | /* Begin PBXNativeTarget section */ 93 | 1EB06727214BDBA500F0BF1E /* ParticlesText */ = { 94 | isa = PBXNativeTarget; 95 | buildConfigurationList = 1EB0673E214BDBA600F0BF1E /* Build configuration list for PBXNativeTarget "ParticlesText" */; 96 | buildPhases = ( 97 | 1EB06724214BDBA500F0BF1E /* Sources */, 98 | 1EB06725214BDBA500F0BF1E /* Frameworks */, 99 | 1EB06726214BDBA500F0BF1E /* Resources */, 100 | ); 101 | buildRules = ( 102 | ); 103 | dependencies = ( 104 | ); 105 | name = ParticlesText; 106 | productName = ParticlesText; 107 | productReference = 1EB06728214BDBA500F0BF1E /* ParticlesText.app */; 108 | productType = "com.apple.product-type.application"; 109 | }; 110 | /* End PBXNativeTarget section */ 111 | 112 | /* Begin PBXProject section */ 113 | 1EB06720214BDBA500F0BF1E /* Project object */ = { 114 | isa = PBXProject; 115 | attributes = { 116 | LastUpgradeCheck = 0940; 117 | ORGANIZATIONNAME = "Rose LZY"; 118 | TargetAttributes = { 119 | 1EB06727214BDBA500F0BF1E = { 120 | CreatedOnToolsVersion = 9.4.1; 121 | }; 122 | }; 123 | }; 124 | buildConfigurationList = 1EB06723214BDBA500F0BF1E /* Build configuration list for PBXProject "ParticlesText" */; 125 | compatibilityVersion = "Xcode 9.3"; 126 | developmentRegion = en; 127 | hasScannedForEncodings = 0; 128 | knownRegions = ( 129 | en, 130 | Base, 131 | ); 132 | mainGroup = 1EB0671F214BDBA500F0BF1E; 133 | productRefGroup = 1EB06729214BDBA500F0BF1E /* Products */; 134 | projectDirPath = ""; 135 | projectRoot = ""; 136 | targets = ( 137 | 1EB06727214BDBA500F0BF1E /* ParticlesText */, 138 | ); 139 | }; 140 | /* End PBXProject section */ 141 | 142 | /* Begin PBXResourcesBuildPhase section */ 143 | 1EB06726214BDBA500F0BF1E /* Resources */ = { 144 | isa = PBXResourcesBuildPhase; 145 | buildActionMask = 2147483647; 146 | files = ( 147 | 1EB06738214BDBA600F0BF1E /* LaunchScreen.storyboard in Resources */, 148 | 9F3F509C231CC5ED00DC0B5F /* problem.zip in Resources */, 149 | 1EB06735214BDBA600F0BF1E /* Assets.xcassets in Resources */, 150 | 1EB06733214BDBA500F0BF1E /* Main.storyboard in Resources */, 151 | ); 152 | runOnlyForDeploymentPostprocessing = 0; 153 | }; 154 | /* End PBXResourcesBuildPhase section */ 155 | 156 | /* Begin PBXSourcesBuildPhase section */ 157 | 1EB06724214BDBA500F0BF1E /* Sources */ = { 158 | isa = PBXSourcesBuildPhase; 159 | buildActionMask = 2147483647; 160 | files = ( 161 | 1EB06730214BDBA500F0BF1E /* ViewController.m in Sources */, 162 | 1EB0673B214BDBA600F0BF1E /* main.m in Sources */, 163 | 1EB0672D214BDBA500F0BF1E /* AppDelegate.m in Sources */, 164 | 1EB06748214BDCC100F0BF1E /* ParticlesBuilder.m in Sources */, 165 | 1EB0674B214BDCC100F0BF1E /* Particle.metal in Sources */, 166 | 1EB06749214BDCC100F0BF1E /* ParticlesMTKView.m in Sources */, 167 | ); 168 | runOnlyForDeploymentPostprocessing = 0; 169 | }; 170 | /* End PBXSourcesBuildPhase section */ 171 | 172 | /* Begin PBXVariantGroup section */ 173 | 1EB06731214BDBA500F0BF1E /* Main.storyboard */ = { 174 | isa = PBXVariantGroup; 175 | children = ( 176 | 1EB06732214BDBA500F0BF1E /* Base */, 177 | ); 178 | name = Main.storyboard; 179 | sourceTree = ""; 180 | }; 181 | 1EB06736214BDBA600F0BF1E /* LaunchScreen.storyboard */ = { 182 | isa = PBXVariantGroup; 183 | children = ( 184 | 1EB06737214BDBA600F0BF1E /* Base */, 185 | ); 186 | name = LaunchScreen.storyboard; 187 | sourceTree = ""; 188 | }; 189 | /* End PBXVariantGroup section */ 190 | 191 | /* Begin XCBuildConfiguration section */ 192 | 1EB0673C214BDBA600F0BF1E /* Debug */ = { 193 | isa = XCBuildConfiguration; 194 | buildSettings = { 195 | ALWAYS_SEARCH_USER_PATHS = NO; 196 | CLANG_ANALYZER_NONNULL = YES; 197 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 198 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 199 | CLANG_CXX_LIBRARY = "libc++"; 200 | CLANG_ENABLE_MODULES = YES; 201 | CLANG_ENABLE_OBJC_ARC = YES; 202 | CLANG_ENABLE_OBJC_WEAK = YES; 203 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 204 | CLANG_WARN_BOOL_CONVERSION = YES; 205 | CLANG_WARN_COMMA = YES; 206 | CLANG_WARN_CONSTANT_CONVERSION = YES; 207 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 208 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 209 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 210 | CLANG_WARN_EMPTY_BODY = YES; 211 | CLANG_WARN_ENUM_CONVERSION = YES; 212 | CLANG_WARN_INFINITE_RECURSION = YES; 213 | CLANG_WARN_INT_CONVERSION = YES; 214 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 215 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 216 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 217 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 218 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 219 | CLANG_WARN_STRICT_PROTOTYPES = YES; 220 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 221 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 222 | CLANG_WARN_UNREACHABLE_CODE = YES; 223 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 224 | CODE_SIGN_IDENTITY = "iPhone Developer"; 225 | COPY_PHASE_STRIP = NO; 226 | DEBUG_INFORMATION_FORMAT = dwarf; 227 | ENABLE_STRICT_OBJC_MSGSEND = YES; 228 | ENABLE_TESTABILITY = YES; 229 | GCC_C_LANGUAGE_STANDARD = gnu11; 230 | GCC_DYNAMIC_NO_PIC = NO; 231 | GCC_NO_COMMON_BLOCKS = YES; 232 | GCC_OPTIMIZATION_LEVEL = 0; 233 | GCC_PREPROCESSOR_DEFINITIONS = ( 234 | "DEBUG=1", 235 | "$(inherited)", 236 | ); 237 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 238 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 239 | GCC_WARN_UNDECLARED_SELECTOR = YES; 240 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 241 | GCC_WARN_UNUSED_FUNCTION = YES; 242 | GCC_WARN_UNUSED_VARIABLE = YES; 243 | IPHONEOS_DEPLOYMENT_TARGET = 11.4; 244 | MTL_ENABLE_DEBUG_INFO = YES; 245 | ONLY_ACTIVE_ARCH = YES; 246 | SDKROOT = iphoneos; 247 | }; 248 | name = Debug; 249 | }; 250 | 1EB0673D214BDBA600F0BF1E /* Release */ = { 251 | isa = XCBuildConfiguration; 252 | buildSettings = { 253 | ALWAYS_SEARCH_USER_PATHS = NO; 254 | CLANG_ANALYZER_NONNULL = YES; 255 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 256 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 257 | CLANG_CXX_LIBRARY = "libc++"; 258 | CLANG_ENABLE_MODULES = YES; 259 | CLANG_ENABLE_OBJC_ARC = YES; 260 | CLANG_ENABLE_OBJC_WEAK = YES; 261 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 262 | CLANG_WARN_BOOL_CONVERSION = YES; 263 | CLANG_WARN_COMMA = YES; 264 | CLANG_WARN_CONSTANT_CONVERSION = YES; 265 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 266 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 267 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 268 | CLANG_WARN_EMPTY_BODY = YES; 269 | CLANG_WARN_ENUM_CONVERSION = YES; 270 | CLANG_WARN_INFINITE_RECURSION = YES; 271 | CLANG_WARN_INT_CONVERSION = YES; 272 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 273 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 274 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 275 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 276 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 277 | CLANG_WARN_STRICT_PROTOTYPES = YES; 278 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 279 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 280 | CLANG_WARN_UNREACHABLE_CODE = YES; 281 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 282 | CODE_SIGN_IDENTITY = "iPhone Developer"; 283 | COPY_PHASE_STRIP = NO; 284 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 285 | ENABLE_NS_ASSERTIONS = NO; 286 | ENABLE_STRICT_OBJC_MSGSEND = YES; 287 | GCC_C_LANGUAGE_STANDARD = gnu11; 288 | GCC_NO_COMMON_BLOCKS = YES; 289 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 290 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 291 | GCC_WARN_UNDECLARED_SELECTOR = YES; 292 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 293 | GCC_WARN_UNUSED_FUNCTION = YES; 294 | GCC_WARN_UNUSED_VARIABLE = YES; 295 | IPHONEOS_DEPLOYMENT_TARGET = 11.4; 296 | MTL_ENABLE_DEBUG_INFO = NO; 297 | SDKROOT = iphoneos; 298 | VALIDATE_PRODUCT = YES; 299 | }; 300 | name = Release; 301 | }; 302 | 1EB0673F214BDBA600F0BF1E /* Debug */ = { 303 | isa = XCBuildConfiguration; 304 | buildSettings = { 305 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 306 | CODE_SIGN_STYLE = Automatic; 307 | DEVELOPMENT_TEAM = 5URH7SFAPA; 308 | INFOPLIST_FILE = ParticlesText/Info.plist; 309 | LD_RUNPATH_SEARCH_PATHS = ( 310 | "$(inherited)", 311 | "@executable_path/Frameworks", 312 | ); 313 | PRODUCT_BUNDLE_IDENTIFIER = com.monkyu.ParticlesText; 314 | PRODUCT_NAME = "$(TARGET_NAME)"; 315 | TARGETED_DEVICE_FAMILY = "1,2"; 316 | }; 317 | name = Debug; 318 | }; 319 | 1EB06740214BDBA600F0BF1E /* Release */ = { 320 | isa = XCBuildConfiguration; 321 | buildSettings = { 322 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 323 | CODE_SIGN_STYLE = Automatic; 324 | DEVELOPMENT_TEAM = 5URH7SFAPA; 325 | INFOPLIST_FILE = ParticlesText/Info.plist; 326 | LD_RUNPATH_SEARCH_PATHS = ( 327 | "$(inherited)", 328 | "@executable_path/Frameworks", 329 | ); 330 | PRODUCT_BUNDLE_IDENTIFIER = com.monkyu.ParticlesText; 331 | PRODUCT_NAME = "$(TARGET_NAME)"; 332 | TARGETED_DEVICE_FAMILY = "1,2"; 333 | }; 334 | name = Release; 335 | }; 336 | /* End XCBuildConfiguration section */ 337 | 338 | /* Begin XCConfigurationList section */ 339 | 1EB06723214BDBA500F0BF1E /* Build configuration list for PBXProject "ParticlesText" */ = { 340 | isa = XCConfigurationList; 341 | buildConfigurations = ( 342 | 1EB0673C214BDBA600F0BF1E /* Debug */, 343 | 1EB0673D214BDBA600F0BF1E /* Release */, 344 | ); 345 | defaultConfigurationIsVisible = 0; 346 | defaultConfigurationName = Release; 347 | }; 348 | 1EB0673E214BDBA600F0BF1E /* Build configuration list for PBXNativeTarget "ParticlesText" */ = { 349 | isa = XCConfigurationList; 350 | buildConfigurations = ( 351 | 1EB0673F214BDBA600F0BF1E /* Debug */, 352 | 1EB06740214BDBA600F0BF1E /* Release */, 353 | ); 354 | defaultConfigurationIsVisible = 0; 355 | defaultConfigurationName = Release; 356 | }; 357 | /* End XCConfigurationList section */ 358 | }; 359 | rootObject = 1EB06720214BDBA500F0BF1E /* Project object */; 360 | } 361 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // ParticlesText 4 | // 5 | // Created by roselzy on 2018/9/14. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // ParticlesText 4 | // 5 | // Created by roselzy on 2018/9/14. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // 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. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // 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. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // 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. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "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" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/Particle.metal: -------------------------------------------------------------------------------- 1 | // 2 | // Particle.metal 3 | // ParticlesOC 4 | // 5 | // Created by roselzy on 2018/9/3. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | kernel void particleRendererShader(texture2d outTexture [[texture(0)]], 13 | const device float4 *inParticles [[buffer(0)]], 14 | device float4 *outParticles [[buffer(1)]], 15 | const device float4 *originParticles [[buffer(2)]], 16 | constant float &imageWidth [[buffer(3)]], 17 | constant float &imageHeight [[buffer(4)]], 18 | device float3 &particleColor [[buffer(5)]], 19 | constant float &duration [[buffer(6)]], 20 | const device float4 *dislocationInParticles [[buffer(7)]], 21 | device int &inFinishHoming [[buffer(8)]], 22 | device int &inFinishDiffused [[buffer(9)]], 23 | constant int &finishType [[buffer(10)]], 24 | const device int &startDiffuseFlag [[buffer(11)]], 25 | const device float &randomDisplacement [[buffer(12)]], 26 | uint id [[thread_position_in_grid]]) { 27 | const float4 inParticle = inParticles[id]; 28 | const float4 dislocationParticle = dislocationInParticles[id]; 29 | const float4 originParticle = originParticles[id]; 30 | const float inParticleZ = inParticle.z; 31 | const float inParticleW = inParticle.w; 32 | 33 | const uint2 particlePositionA(inParticle.x, inParticle.y); 34 | const float2 inParticlePosition(inParticle.x, inParticle.y); 35 | const float2 dislocationParticlePosition(dislocationParticle.x, dislocationParticle.y); 36 | const float2 originParticlesPosition(originParticle.x, originParticle.y); 37 | float currentDistance = fabs(distance(originParticlesPosition, inParticlePosition)); 38 | float currentDislocationDistance = fabs(distance(dislocationParticlePosition, inParticlePosition)); 39 | float alpha = float(1) - currentDistance / fabs(distance(originParticlesPosition, dislocationParticlePosition)); 40 | float4 outColor = float4(particleColor.x * alpha, particleColor.y * alpha, particleColor.z * alpha, alpha); 41 | 42 | float destinationX; 43 | float destinationY; 44 | float slope; 45 | float b; 46 | float stepDistance; 47 | float distance_between_origin_dislocation_particles; 48 | 49 | if (finishType not_eq 2) { // not diffuse effect 50 | if (particlePositionA.x > 0 && particlePositionA.y > 0 && particlePositionA.x < imageWidth && particlePositionA.y < imageHeight) { 51 | outTexture.write(outColor, particlePositionA); 52 | } 53 | distance_between_origin_dislocation_particles = abs(distance(dislocationParticlePosition, originParticlesPosition)) / duration / 60; 54 | if (inFinishHoming == 1) { 55 | if (finishType == 1) { 56 | float fid = float(id); 57 | // do some shake effect 58 | destinationX = originParticle.x + randomDisplacement * sin(fid) * 3 * randomDisplacement; 59 | destinationY = originParticle.y + randomDisplacement * cos(fid) * 3 * randomDisplacement; 60 | }else if (finishType == 0) { 61 | destinationX = inParticle.x; 62 | destinationY = inParticle.y; 63 | } 64 | }else { 65 | if (inParticle.x == originParticle.x) { // slope is near to +inf or -inf 66 | float distance_only_y_axis = abs(dislocationParticlePosition.y - originParticlesPosition.y) / duration / 60; 67 | destinationX = inParticle.x; 68 | if (inParticle.y > originParticle.y) { 69 | destinationY = inParticle.y - distance_only_y_axis; 70 | }else { 71 | destinationY = inParticle.y + distance_only_y_axis; 72 | } 73 | }else { 74 | if (((inParticle.x < originParticle.x) and (inParticle.y < originParticle.y)) or ((inParticle.x > originParticle.x) and (inParticle.y > originParticle.y))) { 75 | // slope is negative 76 | slope = (originParticle.y - inParticle.y) / (inParticle.x - originParticle.x); 77 | stepDistance = sqrt(pow(distance_between_origin_dislocation_particles, 2) / (pow(slope, 2) + 1)); 78 | b = originParticle.y - slope * originParticle.x; 79 | if (inParticle.x < originParticle.x) { 80 | destinationX = inParticle.x + stepDistance; 81 | }else { 82 | destinationX = inParticle.x - stepDistance; 83 | } 84 | destinationY = slope * destinationX + b; 85 | }else { 86 | // slope is positive 87 | slope = (inParticle.y - originParticle.y) / (originParticle.x - inParticle.x); 88 | stepDistance = sqrt(pow(distance_between_origin_dislocation_particles, 2) / (pow(slope, 2) + 1)); 89 | b = originParticle.y - slope * originParticle.x; 90 | if (inParticle.x < originParticle.x) { 91 | destinationX = inParticle.x + stepDistance; 92 | }else { 93 | destinationX = inParticle.x - stepDistance; 94 | } 95 | destinationY = slope * destinationX + b; 96 | } 97 | } 98 | } 99 | }else { // diffuse effect 100 | if (inFinishHoming == 0 or startDiffuseFlag == 0) { // back to origin location animation is not finish 101 | if (particlePositionA.x > 0 && particlePositionA.y > 0 && particlePositionA.x < imageWidth && particlePositionA.y < imageHeight) { 102 | outTexture.write(outColor, particlePositionA); 103 | } 104 | distance_between_origin_dislocation_particles = abs(distance(dislocationParticlePosition, originParticlesPosition)) / duration / 60; 105 | if (inParticle.x == originParticle.x) { // slope is near to +inf or -inf 106 | float distance_only_y_axis = abs(dislocationParticlePosition.y - originParticlesPosition.y) / duration / 60; 107 | destinationX = inParticle.x; 108 | if (inParticle.y > originParticle.y) { 109 | destinationY = inParticle.y - distance_only_y_axis; 110 | }else { 111 | destinationY = inParticle.y + distance_only_y_axis; 112 | } 113 | }else { 114 | if (((inParticle.x < originParticle.x) and (inParticle.y < originParticle.y)) or ((inParticle.x > originParticle.x) and (inParticle.y > originParticle.y))) { 115 | // slope is negative 116 | slope = (originParticle.y - inParticle.y) / (inParticle.x - originParticle.x); 117 | stepDistance = sqrt(pow(distance_between_origin_dislocation_particles, 2) / (pow(slope, 2) + 1)); 118 | b = originParticle.y - slope * originParticle.x; 119 | if (inParticle.x < originParticle.x) { 120 | destinationX = inParticle.x + stepDistance; 121 | }else { 122 | destinationX = inParticle.x - stepDistance; 123 | } 124 | destinationY = slope * destinationX + b; 125 | }else { 126 | // slope is positive 127 | slope = (inParticle.y - originParticle.y) / (originParticle.x - inParticle.x); 128 | stepDistance = sqrt(pow(distance_between_origin_dislocation_particles, 2) / (pow(slope, 2) + 1)); 129 | b = originParticle.y - slope * originParticle.x; 130 | if (inParticle.x < originParticle.x) { 131 | destinationX = inParticle.x + stepDistance; 132 | }else { 133 | destinationX = inParticle.x - stepDistance; 134 | } 135 | destinationY = slope * destinationX + b; 136 | } 137 | } 138 | }else { // 扩散动画 139 | alpha = currentDislocationDistance / fabs(distance(originParticlesPosition, dislocationParticlePosition)); 140 | outColor = float4(particleColor.x * alpha, particleColor.y * alpha, particleColor.z * alpha, alpha); 141 | if (particlePositionA.x > 0 && particlePositionA.y > 0 && particlePositionA.x < imageWidth && particlePositionA.y < imageHeight) { 142 | outTexture.write(outColor, particlePositionA); 143 | } 144 | if (startDiffuseFlag == 1) { 145 | distance_between_origin_dislocation_particles = fabs(distance(dislocationParticlePosition, originParticlesPosition)) / 1.0 / 60.0; 146 | if (inParticle.x == dislocationParticle.x) { // slope is near to +inf or -inf 147 | float distance_only_y_axis = fabs(dislocationParticlePosition.y - originParticlesPosition.y) / 1.0 / 60.0; // 一秒为时长,每次刷新移动的距离 148 | destinationX = inParticle.x; 149 | if (inParticle.y > dislocationParticle.y) { 150 | destinationY = inParticle.y - distance_only_y_axis; 151 | }else { 152 | destinationY = inParticle.y + distance_only_y_axis; 153 | } 154 | }else { 155 | if (((inParticle.x < dislocationParticle.x) and (inParticle.y < originParticle.y)) or ((inParticle.x > dislocationParticle.x) and (inParticle.y > dislocationParticle.y))) { 156 | // slope is negative 157 | slope = (dislocationParticle.y - inParticle.y) / (inParticle.x - dislocationParticle.x); 158 | stepDistance = sqrt(pow(distance_between_origin_dislocation_particles, 2) / (pow(slope, 2) + 1)); 159 | b = dislocationParticle.y - slope * dislocationParticle.x; 160 | if (inParticle.x < dislocationParticle.x) { 161 | destinationX = inParticle.x + stepDistance; 162 | }else { 163 | destinationX = inParticle.x - stepDistance; 164 | } 165 | destinationY = slope * destinationX + b; 166 | }else { 167 | // slope is positive 168 | slope = (inParticle.y - dislocationParticle.y) / (dislocationParticle.x - inParticle.x); 169 | stepDistance = sqrt(pow(distance_between_origin_dislocation_particles, 2) / (pow(slope, 2) + 1)); 170 | b = dislocationParticle.y - slope * dislocationParticle.x; 171 | if (inParticle.x < dislocationParticle.x) { 172 | destinationX = inParticle.x + stepDistance; 173 | }else { 174 | destinationX = inParticle.x - stepDistance; 175 | } 176 | destinationY = slope * destinationX + b; 177 | } 178 | } 179 | }else { 180 | destinationX = inParticle.x; 181 | destinationY = inParticle.y; 182 | } 183 | } 184 | } 185 | float4 outParticle; 186 | outParticle = {destinationX, destinationY, inParticleZ, inParticleW}; 187 | outParticles[id] = outParticle; 188 | } 189 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/ParticlesBuilder.h: -------------------------------------------------------------------------------- 1 | // 2 | // ParticlesBuilder.h 3 | // ParticlesOC 4 | // 5 | // Created by roselzy on 2018/9/12. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | @class ParticlesMTKView; 12 | 13 | typedef NS_ENUM(int, ParticleFinishType) { 14 | ParticleFinishTypeStatic = 0, 15 | ParticleFinishTypeShake = 1, 16 | ParticleFinishTypeDiffuse = 2, 17 | }; 18 | 19 | @interface ParticlesBuilder : NSObject 20 | @property (nonatomic, assign) CGRect frame; 21 | @property (nonatomic, copy) NSString *text; 22 | @property (nonatomic, strong) UIFont *font; 23 | @property (nonatomic, assign) CGFloat density; 24 | @property (nonatomic, assign) CGFloat dispersionX; 25 | @property (nonatomic, assign) CGFloat dispersionY; 26 | @property (nonatomic, assign) float duration; 27 | @property (nonatomic, copy) NSString *hexColor; 28 | @property (nonatomic, assign) BOOL adjustsFontSizeToFitWidth; 29 | @property (nonatomic, assign) ParticleFinishType particleFinishType; 30 | - (ParticlesMTKView *)build; 31 | @end 32 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/ParticlesBuilder.m: -------------------------------------------------------------------------------- 1 | // 2 | // ParticlesBuilder.m 3 | // ParticlesOC 4 | // 5 | // Created by roselzy on 2018/9/12. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import "ParticlesBuilder.h" 10 | #import "ParticlesMTKView.h" 11 | 12 | @implementation ParticlesBuilder 13 | 14 | - (ParticlesMTKView *)build { 15 | ParticlesMTKView *particlesMTKView = [[ParticlesMTKView alloc] initWithFrame:self.frame]; 16 | particlesMTKView.text = self.text; 17 | particlesMTKView.font = self.font; 18 | particlesMTKView.density = self.density; 19 | particlesMTKView.dispersionX = self.dispersionX; 20 | particlesMTKView.dispersionY = self.dispersionY; 21 | particlesMTKView.duration = self.duration; 22 | particlesMTKView.hexColor = self.hexColor; 23 | particlesMTKView.particleFinishType = self.particleFinishType; 24 | particlesMTKView.adjustsFontSizeToFitWidth = self.adjustsFontSizeToFitWidth; 25 | return particlesMTKView; 26 | } 27 | 28 | @end 29 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/ParticlesMTKView.h: -------------------------------------------------------------------------------- 1 | // 2 | // ParticlesMTKView.h 3 | // ParticlesOC 4 | // 5 | // Created by roselzy on 2018/9/5. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "ParticlesBuilder.h" 11 | 12 | static NSString *const ParticlesHomingAnimationFinishedNotification = @"ParticlesHomingAnimationFinishedNotification"; 13 | static NSString *const ParticlesDiffuseAnimationFinishedNotification = @"ParticlesDiffuseAnimationFinishedNotification"; 14 | 15 | @interface ParticlesMTKView : MTKView 16 | @property (nonatomic, copy) NSString *text; 17 | @property (nonatomic, strong) UIFont *font; 18 | @property (nonatomic, assign) NSInteger density; // 粒子密度 1 - 10%的粒子数 10 - 100%粒子 19 | @property (nonatomic, assign) CGFloat dispersionX; // 分散度 0 - 不分散 1 - 最大分散 20 | @property (nonatomic, assign) CGFloat dispersionY; // 分散度 0 - 不分散 1 - 最大分散 21 | @property (nonatomic, assign) float32_t duration; // 粒子效果持续时间 22 | @property (nonatomic, copy) NSString *hexColor; // 粒子颜色 23 | @property (nonatomic, assign) BOOL adjustsFontSizeToFitWidth; // 是否调整字体大小,来适配mtkview的frame,否则以宽度为限制,计算全铺开的高度 24 | @property (nonatomic, assign) ParticleFinishType particleFinishType; // 粒子完成归位动画后的事件类型 25 | - (instancetype)initWithBuilder:(void(^)(ParticlesBuilder *builder))handler; 26 | - (void)prepareAnimating; 27 | - (void)startAnimating; 28 | @end 29 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/ParticlesMTKView.m: -------------------------------------------------------------------------------- 1 | // 2 | // ParticlesMTKView.m 3 | // ParticlesOC 4 | // 5 | // Created by roselzy on 2018/9/5. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import "ParticlesMTKView.h" 10 | 11 | struct Vector4 { 12 | float32_t x; 13 | float32_t y; 14 | float32_t z; 15 | float32_t w; 16 | }; 17 | 18 | struct Particle { 19 | struct Vector4 particle; 20 | }; 21 | 22 | struct ParticleColor { 23 | float32_t r; 24 | float32_t g; 25 | float32_t b; 26 | float32_t a; 27 | }; 28 | 29 | #define Mask8(x) ( (x) & 0xFF ) 30 | #define R(x) ( Mask8(x) ) 31 | #define G(x) ( Mask8(x >> 8 ) ) 32 | #define B(x) ( Mask8(x >> 16) ) 33 | 34 | @interface ParticlesMTKView () 35 | @property (nonatomic, assign) NSInteger particlesCount; 36 | @property (nonatomic, copy) NSArray *textPoints; 37 | 38 | @property (nonatomic, strong) id widthBuffer; 39 | @property (nonatomic, strong) id heightBuffer; 40 | 41 | @property (nonatomic, strong) id commandQueue; 42 | @property (nonatomic, strong) id library; 43 | @property (nonatomic, strong) id computePipelineState; 44 | 45 | @property (nonatomic, assign) NSInteger particlesMemoryByteSize; 46 | @property (nonatomic, strong) id particlesBuffer; 47 | @property (nonatomic, strong) id colorBuffer; 48 | @property (nonatomic, strong) id originParticlesPositionBuffer; 49 | @property (nonatomic, strong) id dislocationParticlesPositionBuffer; 50 | @property (nonatomic, strong) id durationBuffer; 51 | 52 | @property (nonatomic, assign) MTLSize threadsPerThreadGroup; 53 | @property (nonatomic, assign) MTLSize threadGroupsPerGrid; 54 | 55 | @property (nonatomic, assign) MTLRegion region; 56 | @property (nonatomic, assign) void *blankData; 57 | @property (nonatomic, assign) NSUInteger bytesPerRow; 58 | 59 | @property (nonatomic, strong) dispatch_queue_t concurrentQueue; 60 | @property (nonatomic, assign) NSInteger particleInterval; 61 | @property (nonatomic, assign) NSUInteger existParticlesCount; 62 | 63 | @property (nonatomic, assign) int isFinishHomingAnimation; 64 | @property (nonatomic, assign) int isFinishDiffuseAnimation; 65 | @property (nonatomic, strong) id finishHomingStateBuffer; 66 | @property (nonatomic, strong) id finishDiffuseStateBuffer; 67 | 68 | @property (nonatomic, strong) id particleFinishTypeBuffer; 69 | 70 | @property (nonatomic, assign) int startDiffuseFlag; 71 | @property (nonatomic, strong) id diffuseFlagBuffer; 72 | 73 | @property (nonatomic, assign) BOOL shouldStart; 74 | 75 | @property (nonatomic, strong) NSTimer *timer; 76 | @property (nonatomic, assign) float randomDisplacement; 77 | @property (nonatomic, strong) id randomDisplacementBuffer; 78 | @end 79 | 80 | @implementation ParticlesMTKView 81 | 82 | - (instancetype)initWithBuilder:(void (^)(ParticlesBuilder *))handler { 83 | ParticlesBuilder *builder = [[ParticlesBuilder alloc] init]; 84 | handler(builder); 85 | return [builder build]; 86 | } 87 | 88 | - (instancetype)initWithFrame:(CGRect)frame { 89 | self = [super initWithFrame:frame]; 90 | if (self) { 91 | self.concurrentQueue = dispatch_queue_create("concurrent.particles.com", DISPATCH_QUEUE_CONCURRENT); 92 | } 93 | return self; 94 | } 95 | 96 | - (void)prepareAnimating { 97 | self.isFinishHomingAnimation = 0; 98 | self.isFinishDiffuseAnimation = 0; 99 | self.startDiffuseFlag = 0; 100 | self.shouldStart = NO; 101 | self.randomDisplacement = (drand48() - 0.5) * 8; 102 | self.layer.opaque = NO; 103 | [self initComponents]; 104 | } 105 | 106 | - (void)initComponents { 107 | [self initDevice]; 108 | [self initExtraProperty]; 109 | [self initParticlesColor]; 110 | [self initDrawable]; 111 | [self initPointers]; 112 | [self initParticlesPoints]; 113 | [self initComputePipelineState]; 114 | [self updatePointsPosition]; 115 | } 116 | 117 | - (void)initDevice { 118 | self.device = MTLCreateSystemDefaultDevice(); 119 | self.framebufferOnly = NO; 120 | self.commandQueue = [self.device newCommandQueue]; 121 | } 122 | 123 | - (void)initExtraProperty { 124 | self.finishHomingStateBuffer = [self.device newBufferWithBytes:&_isFinishHomingAnimation length:sizeof(int) options:MTLResourceCPUCacheModeWriteCombined]; 125 | self.finishDiffuseStateBuffer = [self.device newBufferWithBytes:&_isFinishDiffuseAnimation length:sizeof(int) options:MTLResourceCPUCacheModeWriteCombined]; 126 | } 127 | 128 | - (void)initParticlesColor { 129 | if([self.hexColor hasPrefix:@"#"]) { 130 | self.hexColor = [self.hexColor stringByReplacingOccurrencesOfString:@"#" withString:@""]; 131 | }else if ([self.hexColor hasPrefix:@"0x"]) { 132 | self.hexColor = [self.hexColor stringByReplacingOccurrencesOfString:@"0x" withString:@""]; 133 | } 134 | unsigned int red, green, blue; 135 | NSRange range; 136 | range.length = 2; 137 | range.location = 0; 138 | [[NSScanner scannerWithString:[self.hexColor substringWithRange:range]] scanHexInt:&red]; 139 | range.location = 2; 140 | [[NSScanner scannerWithString:[self.hexColor substringWithRange:range]] scanHexInt:&green]; 141 | range.location = 4; 142 | [[NSScanner scannerWithString:[self.hexColor substringWithRange:range]] scanHexInt:&blue]; 143 | struct ParticleColor particleColor = {(float)(red / 255.0f), (float)(green / 255.0f), (float)(blue / 255.0f), (float)(1.0)}; 144 | self.colorBuffer = [self.device newBufferWithBytes:&particleColor length:sizeof(struct ParticleColor) options:MTLResourceCPUCacheModeWriteCombined]; 145 | self.particleFinishTypeBuffer = [self.device newBufferWithBytes:&_particleFinishType length:sizeof(int) options:MTLResourceCPUCacheModeWriteCombined]; 146 | } 147 | 148 | - (void)initDrawable { 149 | CGFloat scale = [UIScreen mainScreen].scale; 150 | if (self.window) { 151 | scale = self.window.screen.scale; 152 | } 153 | CGSize drawableSize = self.bounds.size; 154 | drawableSize.width *= scale; 155 | drawableSize.height *= scale; 156 | self.drawableSize = drawableSize; 157 | } 158 | 159 | - (void)initPointers { 160 | CGFloat scale = [UIScreen mainScreen].scale; 161 | if (self.window) { 162 | scale = self.window.screen.scale; 163 | } 164 | 165 | CGSize blankSize = CGSizeMake(self.bounds.size.width * scale, self.bounds.size.height * scale); 166 | self.region = MTLRegionMake2D(0, 0, blankSize.width, blankSize.height); 167 | self.blankData = calloc(1, blankSize.width * blankSize.height * 4); 168 | self.bytesPerRow = 4 * blankSize.width; 169 | 170 | NSUInteger labelWidth = 0; 171 | NSUInteger labelHeight = 0; 172 | CGRect rect = [self.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: self.font, NSForegroundColorAttributeName: [UIColor blackColor]} context:nil]; 173 | labelWidth = ceilf(CGRectGetWidth(rect)); 174 | if (labelWidth % 4 != 0) { 175 | NSInteger mod = labelWidth / 4; 176 | labelWidth = (mod + 1) * 4; 177 | } 178 | labelHeight = CGRectGetHeight(self.bounds); 179 | UILabel *placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)]; 180 | placeLabel.numberOfLines = 0; 181 | placeLabel.backgroundColor = [UIColor whiteColor]; 182 | placeLabel.text = self.text; 183 | placeLabel.font = self.font; 184 | placeLabel.textColor = [UIColor blackColor]; 185 | if (self.adjustsFontSizeToFitWidth) { 186 | placeLabel.adjustsFontSizeToFitWidth = YES; 187 | } 188 | UIImage *image = [self imageFromView:placeLabel]; 189 | NSUInteger imageWidth = CGImageGetWidth(image.CGImage); 190 | NSUInteger imageHeight = CGImageGetHeight(image.CGImage); 191 | uint32_t *pixelData = [self pixelDataFromImage:image]; 192 | NSMutableArray *points = [NSMutableArray arrayWithCapacity:1]; 193 | for (NSInteger i = 0; i < imageHeight; i++) { 194 | for (NSInteger j = 0; j < imageWidth; j++) { 195 | uint32_t color = *pixelData; 196 | CGFloat r = R(color) / 255.0f; 197 | CGFloat g = G(color) / 255.0f; 198 | CGFloat b = B(color) / 255.0f; 199 | if (r == 0.0 && g == 0.0 && b == 0.0) { 200 | [points addObject:[NSValue valueWithCGPoint:CGPointMake(j + self.frame.origin.x, i)]]; 201 | } 202 | pixelData++; 203 | } 204 | } 205 | self.textPoints = [points copy]; 206 | 207 | if (self.density < 1 || self.density > 10) { 208 | [NSException raise:@"Invalid density value" format:@"Particle density must between 1 and 10"]; 209 | } 210 | if (self.density == 10) { 211 | self.particlesCount = self.textPoints.count; 212 | self.particleInterval = 1; 213 | self.existParticlesCount = self.particlesCount; 214 | }else { // 比如是 1 舍弃 90% 的粒子 215 | self.particlesCount = self.textPoints.count; 216 | self.particleInterval = 20 / self.density; // 数组越界问题 217 | NSInteger mod = (self.particlesCount) / self.particleInterval; 218 | if (mod == 0) { 219 | self.existParticlesCount = (self.particlesCount) / self.particleInterval; 220 | }else { 221 | self.existParticlesCount = (self.particlesCount) / self.particleInterval + 1; 222 | } 223 | } 224 | NSInteger mod = self.existParticlesCount % 4; 225 | if(mod != 0) { 226 | self.existParticlesCount = (self.existParticlesCount / 4 + 1) * 4; 227 | } 228 | self.particlesMemoryByteSize = self.existParticlesCount * sizeof(struct Particle); 229 | } 230 | 231 | - (void)initParticlesPoints { 232 | struct Particle *particles = malloc(self.existParticlesCount * sizeof(struct Particle)); 233 | struct Particle *originParticles = malloc(self.existParticlesCount * sizeof(struct Particle)); 234 | struct Particle *dislocationParticles = malloc(self.existParticlesCount * sizeof(struct Particle)); 235 | 236 | memset(particles, 0, self.existParticlesCount * sizeof(struct Particle)); 237 | memset(originParticles, 0, self.existParticlesCount * sizeof(struct Particle)); 238 | memset(dislocationParticles, 0, self.existParticlesCount * sizeof(struct Particle)); 239 | NSInteger tempParticleIndex = 0; 240 | for (NSInteger i = 0; i < self.existParticlesCount; i++) { 241 | CGPoint point = [[self.textPoints objectAtIndex:tempParticleIndex % self.textPoints.count] CGPointValue]; 242 | tempParticleIndex = tempParticleIndex + self.particleInterval; 243 | struct Particle originParticle = { point.x, point.y, [self rand], [self rand] }; 244 | float32_t randomDisX = [self randomX]; 245 | float32_t randomDisY = [self randomY]; 246 | point.x += randomDisX; 247 | point.y += randomDisY; 248 | struct Particle singleParticle = { point.x, point.y, [self rand], [self rand] }; 249 | particles[i] = singleParticle; 250 | originParticles[i] = originParticle; 251 | dislocationParticles[i] = singleParticle; 252 | } 253 | self.particlesBuffer = [self.device newBufferWithBytes:particles length:self.particlesMemoryByteSize options:MTLResourceCPUCacheModeWriteCombined]; 254 | self.originParticlesPositionBuffer = [self.device newBufferWithBytes:originParticles length:self.particlesMemoryByteSize options:MTLResourceCPUCacheModeWriteCombined]; 255 | self.dislocationParticlesPositionBuffer = [self.device newBufferWithBytes:particles length:self.particlesMemoryByteSize options:MTLResourceCPUCacheModeDefaultCache]; 256 | /// 做资源copy; 257 | free(particles); 258 | free(originParticles); 259 | free(dislocationParticles); 260 | self.durationBuffer = [self.device newBufferWithBytes:&_duration length:sizeof(float32_t) options:MTLResourceCPUCacheModeWriteCombined]; 261 | self.diffuseFlagBuffer = [self.device newBufferWithBytes:&_startDiffuseFlag length:sizeof(int) options:MTLResourceCPUCacheModeWriteCombined]; 262 | 263 | } 264 | 265 | - (void)initComputePipelineState { 266 | self.library = [self.device newDefaultLibrary]; 267 | id kernelFunction = [self.library newFunctionWithName:@"particleRendererShader"]; 268 | NSError *error; 269 | self.computePipelineState = [self.device newComputePipelineStateWithFunction:kernelFunction error:&error]; 270 | if (error) { 271 | NSLog(@"newComputePipelineStateWithFunction error"); 272 | } 273 | float32_t width = self.drawableSize.width; 274 | float32_t height = self.drawableSize.height; 275 | 276 | NSUInteger threadExecutionWidth = self.computePipelineState.threadExecutionWidth; 277 | self.threadsPerThreadGroup = MTLSizeMake(threadExecutionWidth, 1, 1); 278 | self.threadGroupsPerGrid = MTLSizeMake(self.existParticlesCount / threadExecutionWidth, 1, 1); 279 | NSLog(@"width: %f height: %f",width, height); 280 | self.widthBuffer = [self.device newBufferWithBytes:&width length:sizeof(float32_t) options:MTLResourceOptionCPUCacheModeWriteCombined]; 281 | self.heightBuffer = [self.device newBufferWithBytes:&height length:sizeof(float32_t) options:MTLResourceOptionCPUCacheModeWriteCombined]; 282 | self.randomDisplacementBuffer = [self.device newBufferWithBytes:&_randomDisplacement length:sizeof(float32_t) options:MTLResourceOptionCPUCacheModeWriteCombined]; 283 | } 284 | 285 | - (void)updatePointsPosition { 286 | [self update]; 287 | } 288 | 289 | - (void)update { 290 | @autoreleasepool { 291 | id nextDrawable = [((CAMetalLayer *)self.layer) nextDrawable]; 292 | id commandBuffer = [self.commandQueue commandBuffer]; 293 | id computeEncoder = [commandBuffer computeCommandEncoder]; 294 | [computeEncoder setComputePipelineState:self.computePipelineState]; 295 | [computeEncoder setBuffer:self.particlesBuffer offset:0 atIndex:0]; 296 | [computeEncoder setBuffer:self.particlesBuffer offset:0 atIndex:1]; 297 | [computeEncoder setBuffer:self.originParticlesPositionBuffer offset:0 atIndex:2]; 298 | [computeEncoder setBuffer:self.widthBuffer offset:0 atIndex:3]; 299 | [computeEncoder setBuffer:self.heightBuffer offset:0 atIndex:4]; 300 | [computeEncoder setBuffer:self.colorBuffer offset:0 atIndex:5]; 301 | [computeEncoder setBuffer:self.durationBuffer offset:0 atIndex:6]; 302 | [computeEncoder setBuffer:self.dislocationParticlesPositionBuffer offset:0 atIndex:7]; 303 | [computeEncoder setBuffer:self.finishHomingStateBuffer offset:0 atIndex:8]; 304 | [computeEncoder setBuffer:self.finishHomingStateBuffer offset:0 atIndex:9]; 305 | [computeEncoder setBuffer:self.finishDiffuseStateBuffer offset:0 atIndex:9]; 306 | [computeEncoder setBuffer:self.particleFinishTypeBuffer offset:0 atIndex:10]; 307 | [computeEncoder setBuffer:self.diffuseFlagBuffer offset:0 atIndex:11]; 308 | [computeEncoder setBuffer:self.randomDisplacementBuffer offset:0 atIndex:12]; 309 | [nextDrawable.texture replaceRegion:self.region mipmapLevel:0 withBytes:self.blankData bytesPerRow:self.bytesPerRow]; 310 | [computeEncoder setTexture:nextDrawable.texture atIndex:0]; 311 | [computeEncoder dispatchThreadgroups:self.threadGroupsPerGrid threadsPerThreadgroup:self.threadsPerThreadGroup]; 312 | [commandBuffer addCompletedHandler:^(id _Nonnull commandBuffer) { 313 | }]; 314 | [computeEncoder endEncoding]; 315 | [commandBuffer presentDrawable:nextDrawable]; 316 | [commandBuffer commit]; 317 | } 318 | } 319 | 320 | - (void)startAnimating { 321 | self.shouldStart = YES; 322 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.duration * 1. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 323 | self.isFinishHomingAnimation = 1; 324 | self.finishHomingStateBuffer = [self.device newBufferWithBytes:&self->_isFinishHomingAnimation length:sizeof(int) options:MTLResourceCPUCacheModeWriteCombined]; 325 | [[NSNotificationCenter defaultCenter] postNotificationName:ParticlesHomingAnimationFinishedNotification object:nil]; 326 | if (self.particleFinishType == ParticleFinishTypeStatic) { 327 | self.shouldStart = NO; 328 | }else if (self.particleFinishType == ParticleFinishTypeShake) { 329 | self.shouldStart = YES; 330 | }else if (self.particleFinishType == ParticleFinishTypeDiffuse) { 331 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 332 | self.startDiffuseFlag = 1; 333 | self.diffuseFlagBuffer = [self.device newBufferWithBytes:&self->_startDiffuseFlag length:sizeof(int) options:MTLResourceCPUCacheModeWriteCombined]; 334 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 335 | self.isFinishDiffuseAnimation = 1; 336 | self.finishDiffuseStateBuffer = [self.device newBufferWithBytes:&self->_isFinishDiffuseAnimation length:sizeof(int) options:MTLResourceCPUCacheModeWriteCombined]; 337 | [[NSNotificationCenter defaultCenter] postNotificationName:ParticlesDiffuseAnimationFinishedNotification object:nil]; 338 | self.shouldStart = NO; 339 | }); 340 | }); 341 | } 342 | }); 343 | } 344 | 345 | - (void)startParticlesEffect { 346 | [self updatePointsPosition]; 347 | } 348 | 349 | - (void)drawRect:(CGRect)rect { 350 | [super drawRect:rect]; 351 | self.randomDisplacement = (drand48() - 0.5) * 4; 352 | self.randomDisplacementBuffer = [self.device newBufferWithBytes:&_randomDisplacement length:sizeof(float32_t) options:MTLResourceOptionCPUCacheModeWriteCombined]; 353 | if(self.shouldStart) { 354 | [self startParticlesEffect]; 355 | } 356 | } 357 | 358 | - (float32_t)randomDisplacement { 359 | return (drand48() - 0.5) * arc4random_uniform(20); 360 | } 361 | 362 | - (float32_t)rand { 363 | return (drand48() - 0.5) * 0.005; 364 | } 365 | 366 | - (float32_t)randomX { 367 | return (drand48() - 0.5) * [UIScreen mainScreen].bounds.size.width * self.dispersionX; 368 | } 369 | 370 | - (float32_t)randomY { 371 | return (drand48() - 0.5) * [UIScreen mainScreen].bounds.size.height * self.dispersionY; 372 | } 373 | 374 | - (UIImage *)imageFromView:(UIView *)view { 375 | UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, [UIScreen mainScreen].scale); 376 | CGContextRef ctx = UIGraphicsGetCurrentContext(); 377 | [view.layer renderInContext:ctx]; 378 | UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 379 | UIGraphicsEndImageContext(); 380 | return image; 381 | } 382 | 383 | - (uint32_t *)pixelDataFromImage:(UIImage *)image { 384 | NSData *pixelData = CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))); 385 | uint32_t *bytes = (uint32_t *)pixelData.bytes; 386 | return bytes; 387 | } 388 | 389 | @end 390 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // ParticlesText 4 | // 5 | // Created by roselzy on 2018/9/14. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // ParticlesText 4 | // 5 | // Created by roselzy on 2018/9/14. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "ParticlesMTKView.h" 11 | 12 | @interface ViewController () 13 | @property (nonatomic, strong) ParticlesMTKView *particlesMTKView; 14 | @end 15 | 16 | @implementation ViewController 17 | 18 | - (void)viewDidLoad { 19 | [super viewDidLoad]; 20 | [super viewDidLoad]; 21 | self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4]; 22 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homingAnimationFinished) name:ParticlesHomingAnimationFinishedNotification object:nil]; 23 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(diffusingAnimationFinished) name:ParticlesDiffuseAnimationFinishedNotification object:nil]; 24 | } 25 | 26 | - (void)viewDidAppear:(BOOL)animated { 27 | [super viewDidAppear:animated]; 28 | } 29 | 30 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 31 | self.particlesMTKView = [[ParticlesMTKView alloc] initWithBuilder:^(ParticlesBuilder *builder) { 32 | builder.frame = CGRectMake(50, 160, CGRectGetWidth(self.view.bounds) - 100, 100); 33 | builder.text = @"天青色等烟雨 "; 34 | builder.font = [UIFont systemFontOfSize:60]; 35 | builder.density = 10; 36 | builder.dispersionX = 2; 37 | builder.dispersionY = 2; 38 | builder.duration = 2.0; 39 | builder.hexColor = @"#1de0f9"; 40 | builder.adjustsFontSizeToFitWidth = YES; 41 | builder.particleFinishType = ParticleFinishTypeShake; 42 | }]; 43 | [self.view addSubview:self.particlesMTKView]; 44 | [self.particlesMTKView prepareAnimating]; 45 | [self.particlesMTKView startAnimating]; 46 | } 47 | 48 | - (void)homingAnimationFinished { 49 | NSLog(@"homing"); 50 | } 51 | 52 | - (void)diffusingAnimationFinished { 53 | NSLog(@"diffusing"); 54 | } 55 | 56 | 57 | @end 58 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // ParticlesText 4 | // 5 | // Created by roselzy on 2018/9/14. 6 | // Copyright © 2018 Rose LZY. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ParticlesText/ParticlesText/problem.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MonkYU/ParticlesText/88495b4e0637c3328adfb2cb51979c5dbe74cf77/ParticlesText/ParticlesText/problem.zip -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ParticlesText 2 | 基于文本的粒子动画 - Text diffusion animation consist of particles in iOS project 3 | 4 | ## Overview 5 | 6 | - ParticlesText是一个基于指定文本,生成粒子聚合和扩散动画的视图 7 | - 基于Metal 8 | - 支持粒子密度,X轴,Y轴方向扩散程度,动画时长,粒子颜色等设置 9 | 10 | 11 | ## 基本使用 - How To Use 12 | ``` 13 | self.particlesMTKView = [[ParticlesMTKView alloc] initWithBuilder:^(ParticlesBuilder *builder) { 14 | builder.frame = CGRectMake(0, 160, CGRectGetWidth(self.view.bounds), 300); 15 | builder.text = @"天青色等烟雨 而我在等你"; 16 | builder.font = [UIFont systemFontOfSize:60]; 17 | builder.density = 10; 18 | builder.dispersionX = 2; 19 | builder.dispersionY = 2; 20 | builder.duration = 2.0; 21 | builder.hexColor = @"#1de0f9"; 22 | builder.particleFinishType = ParticleFinishTypeShake; 23 | }]; 24 | ``` 25 | 26 | ## English-Version 27 | 28 | ## What is ParticlesText 29 | 30 | - ParticlesText is a view that generates particle aggregation and diffusion animation based on specified text. 31 | - Based on Metal 32 | - Support particle density, X-axis, Y-axis diffusion degree, animation duration, particle color, etc. 33 | 34 | ## 效果图 35 | 36 | ### 结束时静止 37 | ![结束时静止](https://github.com/MonkYU/ParticlesText/blob/master/Resources/static.gif) 38 | 39 | ### 结束时震动 40 | ![结束时晃动](https://github.com/MonkYU/ParticlesText/blob/master/Resources/shake.gif) 41 | 42 | ### 结束时扩散消失 43 | ![结束时扩散消失](https://github.com/MonkYU/ParticlesText/blob/master/Resources/diffusion.gif) 44 | -------------------------------------------------------------------------------- /Resources/diffusion.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MonkYU/ParticlesText/88495b4e0637c3328adfb2cb51979c5dbe74cf77/Resources/diffusion.gif -------------------------------------------------------------------------------- /Resources/shake.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MonkYU/ParticlesText/88495b4e0637c3328adfb2cb51979c5dbe74cf77/Resources/shake.gif -------------------------------------------------------------------------------- /Resources/static.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MonkYU/ParticlesText/88495b4e0637c3328adfb2cb51979c5dbe74cf77/Resources/static.gif --------------------------------------------------------------------------------