├── .gitignore ├── CollectionView-AppleCode ├── CircleLayout │ ├── CircleLayout.xcodeproj │ │ ├── project.pbxproj │ │ └── project.xcworkspace │ │ │ └── contents.xcworkspacedata │ ├── CircleLayout │ │ ├── AppDelegate.h │ │ ├── AppDelegate.m │ │ ├── Base.lproj │ │ │ ├── InfoPlist.strings │ │ │ └── LaunchScreen.xib │ │ ├── Cell.h │ │ ├── Cell.m │ │ ├── CircleLayout-Info.plist │ │ ├── CircleLayout-Prefix.pch │ │ ├── CircleLayout.h │ │ ├── CircleLayout.m │ │ ├── Images.xcassets │ │ │ ├── AppIcon.appiconset │ │ │ │ └── Contents.json │ │ │ └── LaunchImage.launchimage │ │ │ │ └── Contents.json │ │ ├── ViewController.h │ │ ├── ViewController.m │ │ └── main.m │ ├── LICENSE.txt │ └── ReadMe.md └── CollectionViewTransition │ ├── CollectionViewTransition.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata │ ├── CollectionViewTransition │ ├── APLAppDelegate.h │ ├── APLAppDelegate.m │ ├── APLCollectionViewCell.h │ ├── APLCollectionViewCell.m │ ├── APLCollectionViewController.h │ ├── APLCollectionViewController.m │ ├── APLDetailViewController.h │ ├── APLDetailViewController.m │ ├── APLGridCollectionViewController.h │ ├── APLGridCollectionViewController.m │ ├── APLStackCollectionViewController.h │ ├── APLStackCollectionViewController.m │ ├── APLStackLayout.h │ ├── APLStackLayout.m │ ├── APLTransitionLayout.h │ ├── APLTransitionLayout.m │ ├── APLTransitionManager.h │ ├── APLTransitionManager.m │ ├── Base.lproj │ │ └── Main.storyboard │ ├── CollectionViewTransition-Info.plist │ ├── CollectionViewTransition-Prefix.pch │ ├── Images.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── LaunchImage.launchimage │ │ │ └── Contents.json │ ├── Images │ │ ├── sa0.jpg │ │ ├── sa1.jpg │ │ ├── sa10.jpg │ │ ├── sa11.jpg │ │ ├── sa12.jpg │ │ ├── sa13.jpg │ │ ├── sa14.jpg │ │ ├── sa15.jpg │ │ ├── sa16.jpg │ │ ├── sa17.jpg │ │ ├── sa18.jpg │ │ ├── sa19.jpg │ │ ├── sa2.jpg │ │ ├── sa20.jpg │ │ ├── sa21.jpg │ │ ├── sa22.jpg │ │ ├── sa23.jpg │ │ ├── sa24.jpg │ │ ├── sa3.jpg │ │ ├── sa4.jpg │ │ ├── sa5.jpg │ │ ├── sa6.jpg │ │ ├── sa7.jpg │ │ ├── sa8.jpg │ │ └── sa9.jpg │ ├── en.lproj │ │ └── InfoPlist.strings │ └── main.m │ └── ReadMe.txt ├── NSURLSession_Test ├── NSURLSession_Test.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ │ └── contents.xcworkspacedata └── NSURLSession_Test │ ├── AppDelegate.h │ ├── AppDelegate.m │ ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json │ ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard │ ├── Info.plist │ ├── ViewController.h │ ├── ViewController.m │ └── main.m ├── README.md └── 排序算法图形 ├── STBasic.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── STBasic ├── Base.lproj │ └── Main.storyboard ├── Images.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── LaunchImage.launchimage │ │ └── Contents.json ├── STAppDelegate.h ├── STAppDelegate.m ├── STBasic-Info.plist ├── STBasic-Prefix.pch ├── STCodeViewController.h ├── STCodeViewController.m ├── STHanoiOperation.h ├── STHanoiOperation.m ├── STHanoiView.h ├── STHanoiView.m ├── STHanoiViewController.h ├── STHanoiViewController.m ├── STRootViewController.h ├── STRootViewController.m ├── STSortOperation.h ├── STSortOperation.m ├── STSortView.h ├── STSortView.m ├── STSortViewController.h ├── STSortViewController.m ├── en.lproj │ └── InfoPlist.strings └── main.m └── STBasicTests ├── STBasicTests-Info.plist ├── STBasicTests.m └── en.lproj └── InfoPlist.strings /.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 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/AppDelegate.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Simple app delegate. 7 | */ 8 | 9 | @class ViewController; 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/AppDelegate.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Simple app delegate. 7 | */ 8 | 9 | #import "AppDelegate.h" 10 | 11 | #import "ViewController.h" 12 | #import "CircleLayout.h" 13 | 14 | @interface AppDelegate () 15 | @property (strong, nonatomic) ViewController *viewController; 16 | @end 17 | 18 | @implementation AppDelegate 19 | 20 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 21 | { 22 | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 23 | _viewController = [[ViewController alloc] initWithCollectionViewLayout:[[CircleLayout alloc] init]]; 24 | 25 | self.window.rootViewController = self.viewController; 26 | 27 | [self.window makeKeyAndVisible]; 28 | return YES; 29 | } 30 | 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/Base.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/Cell.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Custom collection view cell for a blue dot. 7 | */ 8 | 9 | @interface Cell : UICollectionViewCell 10 | 11 | @end 12 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/Cell.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Custom collection view cell for a blue dot. 7 | */ 8 | 9 | #import "Cell.h" 10 | 11 | @implementation Cell 12 | 13 | - (instancetype)initWithFrame:(CGRect)frame 14 | { 15 | self = [super initWithFrame:frame]; 16 | if (self) { 17 | self.contentView.layer.cornerRadius = 35.0; 18 | self.contentView.layer.borderWidth = 1.0f; 19 | self.contentView.layer.borderColor = [UIColor whiteColor].CGColor; 20 | self.contentView.backgroundColor = [UIColor blueColor]; 21 | } 22 | return self; 23 | } 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/CircleLayout-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIcons 12 | 13 | CFBundleIcons~ipad 14 | 15 | CFBundleIdentifier 16 | $(PRODUCT_BUNDLE_IDENTIFIER) 17 | CFBundleInfoDictionaryVersion 18 | 6.0 19 | CFBundleName 20 | ${PRODUCT_NAME} 21 | CFBundlePackageType 22 | APPL 23 | CFBundleShortVersionString 24 | 2.0 25 | CFBundleSignature 26 | ???? 27 | CFBundleVersion 28 | 2.0 29 | LSRequiresIPhoneOS 30 | 31 | UILaunchStoryboardName 32 | LaunchScreen 33 | UIRequiredDeviceCapabilities 34 | 35 | armv7 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/CircleLayout-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'CircleLayout' target in the 'CircleLayout' project 3 | // 4 | 5 | #import 6 | 7 | #ifdef __OBJC__ 8 | @import UIKit; 9 | @import Foundation; 10 | #endif 11 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/CircleLayout.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Custom UICollectionViewLayout to represent Circle Layout design. 7 | */ 8 | 9 | @interface CircleLayout : UICollectionViewLayout 10 | 11 | @end 12 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/CircleLayout.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Custom UICollectionViewLayout to represent Circle Layout design. 7 | */ 8 | 9 | #import "CircleLayout.h" 10 | 11 | #define ITEM_SIZE 70 12 | 13 | @interface CircleLayout () 14 | @property (nonatomic, assign) CGPoint center; 15 | @property (nonatomic, assign) CGFloat radius; 16 | @property (nonatomic, assign) NSInteger cellCount; 17 | @end 18 | 19 | 20 | @implementation CircleLayout 21 | 22 | -(void)prepareLayout 23 | { 24 | [super prepareLayout]; 25 | 26 | CGSize size = self.collectionView.frame.size; 27 | _cellCount = [[self collectionView] numberOfItemsInSection:0]; 28 | _center = CGPointMake(size.width / 2.0, size.height / 2.0); 29 | _radius = MIN(size.width, size.height) / 2.5; 30 | } 31 | 32 | -(CGSize)collectionViewContentSize 33 | { 34 | return [self collectionView].frame.size; 35 | } 36 | 37 | - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path 38 | { 39 | UICollectionViewLayoutAttributes* attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:path]; 40 | attributes.size = CGSizeMake(ITEM_SIZE, ITEM_SIZE); 41 | attributes.center = CGPointMake(_center.x + _radius * cosf(2 * path.item * M_PI / _cellCount), 42 | _center.y + _radius * sinf(2 * path.item * M_PI / _cellCount)); 43 | return attributes; 44 | } 45 | 46 | -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect 47 | { 48 | NSMutableArray* attributes = [NSMutableArray array]; 49 | for (NSInteger i=0 ; i < self.cellCount; i++) { 50 | NSIndexPath* indexPath = [NSIndexPath indexPathForItem:i inSection:0]; 51 | [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]]; 52 | } 53 | return attributes; 54 | } 55 | 56 | - (UICollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedItemAtIndexPath:(NSIndexPath *)itemIndexPath 57 | { 58 | UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath]; 59 | attributes.alpha = 0.0; 60 | attributes.center = CGPointMake(_center.x, _center.y); 61 | return attributes; 62 | } 63 | 64 | - (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedItemAtIndexPath:(NSIndexPath *)itemIndexPath 65 | { 66 | UICollectionViewLayoutAttributes* attributes = [self layoutAttributesForItemAtIndexPath:itemIndexPath]; 67 | attributes.alpha = 0.0; 68 | attributes.center = CGPointMake(_center.x, _center.y); 69 | attributes.transform3D = CATransform3DMakeScale(0.1, 0.1, 1.0); 70 | return attributes; 71 | } 72 | 73 | @end -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "ipad", 5 | "scale" : "1x", 6 | "size" : "29x29" 7 | }, 8 | { 9 | "idiom" : "ipad", 10 | "scale" : "2x", 11 | "size" : "29x29" 12 | }, 13 | { 14 | "idiom" : "ipad", 15 | "scale" : "1x", 16 | "size" : "76x76" 17 | }, 18 | { 19 | "idiom" : "ipad", 20 | "scale" : "2x", 21 | "size" : "76x76" 22 | }, 23 | { 24 | "idiom" : "ipad", 25 | "scale" : "1x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "ipad", 30 | "scale" : "2x", 31 | "size" : "40x40" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "landscape", 5 | "idiom" : "ipad", 6 | "minimum-system-version" : "7.0", 7 | "extent" : "full-screen", 8 | "scale" : "1x" 9 | }, 10 | { 11 | "orientation" : "landscape", 12 | "idiom" : "ipad", 13 | "minimum-system-version" : "7.0", 14 | "extent" : "full-screen", 15 | "scale" : "2x" 16 | }, 17 | { 18 | "orientation" : "portrait", 19 | "idiom" : "ipad", 20 | "minimum-system-version" : "7.0", 21 | "extent" : "full-screen", 22 | "scale" : "1x" 23 | }, 24 | { 25 | "orientation" : "portrait", 26 | "idiom" : "ipad", 27 | "minimum-system-version" : "7.0", 28 | "extent" : "full-screen", 29 | "scale" : "2x" 30 | } 31 | ], 32 | "info" : { 33 | "version" : 1, 34 | "author" : "xcode" 35 | } 36 | } -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/ViewController.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | The primary view controller for this app. 7 | */ 8 | 9 | @interface ViewController : UICollectionViewController 10 | 11 | @end 12 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/ViewController.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | The primary view controller for this app. 7 | */ 8 | 9 | #import "ViewController.h" 10 | #import "Cell.h" 11 | 12 | NSString * const cellIdentifier = @"MY_CELL"; 13 | 14 | @interface ViewController () 15 | @property (nonatomic, assign) NSInteger cellCount; 16 | @end 17 | 18 | @implementation ViewController 19 | 20 | -(void)viewDidLoad 21 | { 22 | [super viewDidLoad]; 23 | self.cellCount = 20; 24 | UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; 25 | [self.collectionView addGestureRecognizer:tapRecognizer]; 26 | [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:cellIdentifier]; 27 | [self.collectionView reloadData]; 28 | self.collectionView.backgroundColor = [UIColor whiteColor]; 29 | } 30 | 31 | - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section; 32 | { 33 | return self.cellCount; 34 | } 35 | 36 | - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath; 37 | { 38 | Cell *cell = [cv dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; 39 | return cell; 40 | } 41 | 42 | - (void)handleTapGesture:(UITapGestureRecognizer *)sender { 43 | 44 | if (sender.state == UIGestureRecognizerStateEnded) 45 | { 46 | CGPoint initialPinchPoint = [sender locationInView:self.collectionView]; 47 | NSIndexPath* tappedCellPath = [self.collectionView indexPathForItemAtPoint:initialPinchPoint]; 48 | if (tappedCellPath!=nil) 49 | { 50 | self.cellCount = self.cellCount - 1; 51 | [self.collectionView performBatchUpdates:^{ 52 | [self.collectionView deleteItemsAtIndexPaths:@[tappedCellPath]]; 53 | 54 | } completion:nil]; 55 | } 56 | else 57 | { 58 | self.cellCount = self.cellCount + 1; 59 | [self.collectionView performBatchUpdates:^{ 60 | [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]]]; 61 | } completion:nil]; 62 | } 63 | } 64 | } 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/CircleLayout/main.m: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 3 | See LICENSE.txt for this sample’s licensing information 4 | 5 | Abstract: 6 | Main source file to this sample project. 7 | */ 8 | 9 | #import "AppDelegate.h" 10 | 11 | int main(int argc, char *argv[]) 12 | { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/LICENSE.txt: -------------------------------------------------------------------------------- 1 | Sample code project: CircleLayout 2 | Version: 2.0 3 | 4 | IMPORTANT: This Apple software is supplied to you by Apple 5 | Inc. ("Apple") in consideration of your agreement to the following 6 | terms, and your use, installation, modification or redistribution of 7 | this Apple software constitutes acceptance of these terms. If you do 8 | not agree with these terms, please do not use, install, modify or 9 | redistribute this Apple software. 10 | 11 | In consideration of your agreement to abide by the following terms, and 12 | subject to these terms, Apple grants you a personal, non-exclusive 13 | license, under Apple's copyrights in this original Apple software (the 14 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 15 | Software, with or without modifications, in source and/or binary forms; 16 | provided that if you redistribute the Apple Software in its entirety and 17 | without modifications, you must retain this notice and the following 18 | text and disclaimers in all such redistributions of the Apple Software. 19 | Neither the name, trademarks, service marks or logos of Apple Inc. may 20 | be used to endorse or promote products derived from the Apple Software 21 | without specific prior written permission from Apple. Except as 22 | expressly stated in this notice, no other rights or licenses, express or 23 | implied, are granted by Apple herein, including but not limited to any 24 | patent rights that may be infringed by your derivative works or by other 25 | works in which the Apple Software may be incorporated. 26 | 27 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 28 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 29 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 30 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 31 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 32 | 33 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 34 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 36 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 37 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 38 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 39 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 40 | POSSIBILITY OF SUCH DAMAGE. 41 | 42 | Copyright (C) 2015 Apple Inc. All Rights Reserved. 43 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CircleLayout/ReadMe.md: -------------------------------------------------------------------------------- 1 | # CircleLayout 2 | 3 | ## Description 4 | + Shows how to use a collection view to arrange views on a circle and use custom animations when inserting and removing items. 5 | 6 | ## Build Requirements 7 | + iOS 8.0 SDK or later 8 | 9 | ## Runtime Requirements 10 | + iOS 8.0 or later 11 | 12 | 13 | Copyright (C) 2015-2012 Apple Inc. All rights reserved. 14 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLAppDelegate.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLAppDelegate.h 3 | Abstract: Template delegate for the application. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | @import UIKit; 49 | 50 | @interface APLAppDelegate : UIResponder 51 | 52 | @property (nonatomic) UIWindow *window; 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLAppDelegate.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLAppDelegate.m 3 | Abstract: Template delegate for the application. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | #import "APLAppDelegate.h" 49 | #import "APLTransitionManager.h" 50 | #import "APLStackLayout.h" 51 | #import "APLStackCollectionViewController.h" 52 | 53 | @interface APLAppDelegate () 54 | 55 | @property (nonatomic) APLTransitionManager *transitionManager; 56 | 57 | @end 58 | 59 | 60 | #pragma mark - 61 | 62 | @implementation APLAppDelegate 63 | 64 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 65 | { 66 | UINavigationController *navController = (UINavigationController *)self.window.rootViewController; 67 | 68 | // setup our layout and initial collection view 69 | APLStackLayout *stackLayout = [[APLStackLayout alloc] init]; 70 | APLStackCollectionViewController *collectionViewController = [[APLStackCollectionViewController alloc] initWithCollectionViewLayout:stackLayout]; 71 | collectionViewController.title = @"Stack Layout"; 72 | navController.navigationBar.translucent = NO; 73 | navController.delegate = self; 74 | 75 | // add the single collection view to our navigation controller 76 | [navController setViewControllers:@[collectionViewController]]; 77 | 78 | // we want a light gray for the navigation bar, otherwise it defaults to white 79 | navController.navigationBar.barTintColor = [UIColor lightGrayColor]; 80 | 81 | // create our "transitioning" object to manage the pinch gesture to transitition between stack and grid layouts 82 | _transitionManager = 83 | [[APLTransitionManager alloc] initWithCollectionView:collectionViewController.collectionView]; 84 | self.transitionManager.delegate = self; 85 | 86 | return YES; 87 | } 88 | 89 | 90 | #pragma mark - APLTransitionControllerDelegate 91 | 92 | - (void)interactionBeganAtPoint:(CGPoint)p 93 | { 94 | UINavigationController *navController = (UINavigationController *)self.window.rootViewController; 95 | 96 | // Very basic communication between the transition controller and the top view controller 97 | // It would be easy to add more control, support pop, push or no-op. 98 | // 99 | UIViewController *viewController = 100 | [(APLCollectionViewController *)navController.topViewController nextViewControllerAtPoint:p]; 101 | if (viewController != nil) 102 | { 103 | [navController pushViewController:viewController animated:YES]; 104 | } 105 | else 106 | { 107 | [navController popViewControllerAnimated:YES]; 108 | } 109 | } 110 | 111 | 112 | #pragma mark - UINavigationControllerDelegate 113 | 114 | - (id )navigationController:(UINavigationController *)navigationController 115 | interactionControllerForAnimationController:(id ) animationController 116 | { 117 | // return our own transition manager if the incoming controller matches ours 118 | if (animationController == self.transitionManager) 119 | { 120 | return self.transitionManager; 121 | } 122 | return nil; 123 | } 124 | 125 | - (id )navigationController:(UINavigationController *)navigationController 126 | animationControllerForOperation:(UINavigationControllerOperation)operation 127 | fromViewController:(UIViewController *)fromVC 128 | toViewController:(UIViewController *)toVC 129 | { 130 | id transitionManager = nil; 131 | 132 | // make sure we are transitioning from or to a collection view controller, and that interaction is allowed 133 | if ([fromVC isKindOfClass:[UICollectionViewController class]] && 134 | [toVC isKindOfClass:[UICollectionViewController class]] && 135 | self.transitionManager.hasActiveInteraction) 136 | { 137 | self.transitionManager.navigationOperation = operation; 138 | transitionManager = self.transitionManager; 139 | } 140 | return transitionManager; 141 | } 142 | 143 | @end 144 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLCollectionViewCell.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLCollectionViewCell.h 3 | Abstract: The custom UICollectionViewCell containing a single UIImageView. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | @import UIKit; 49 | 50 | @interface APLCollectionViewCell : UICollectionViewCell 51 | 52 | @property (nonatomic, readonly) UIImageView *imageView; 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLCollectionViewCell.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLCollectionViewCell.m 3 | Abstract: The custom UICollectionViewCell containing a single UIImageView. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | #import "APLCollectionViewCell.h" 49 | 50 | @implementation APLCollectionViewCell 51 | 52 | - (id)initWithFrame:(CGRect)frame 53 | { 54 | self = [super initWithFrame:frame]; 55 | if (self != nil) 56 | { 57 | // create our image view so that is matches the height and width of this cell 58 | _imageView = [[UIImageView alloc] initWithFrame:self.bounds]; 59 | self.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 60 | self.imageView.contentMode = UIViewContentModeScaleAspectFill; 61 | self.imageView.clipsToBounds = YES; 62 | 63 | // add a white frame around the image 64 | self.imageView.layer.borderWidth = 3.0; 65 | self.imageView.layer.borderColor = [UIColor whiteColor].CGColor; 66 | 67 | // Define how the edges of the layer are rasterized for each of the four edges 68 | // (left, right, bottom, top) if the corresponding bit is set the edge will be antialiased 69 | // 70 | self.imageView.layer.edgeAntialiasingMask = 71 | kCALayerLeftEdge | kCALayerRightEdge | kCALayerBottomEdge | kCALayerTopEdge; 72 | 73 | [[self contentView] addSubview:self.imageView]; 74 | } 75 | return self; 76 | } 77 | 78 | @end 79 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLCollectionViewController.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLCollectionViewController.h 3 | Abstract: Base UICollectionViewController containing the basic functionality for collection views. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | @import UIKit; 49 | 50 | @interface APLCollectionViewController : UICollectionViewController 51 | 52 | - (UICollectionViewController *)nextViewControllerAtPoint:(CGPoint)p; 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLCollectionViewController.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLCollectionViewController.m 3 | Abstract: Base UICollectionViewController containing the basic functionality for collection views. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | #import "APLCollectionViewController.h" 49 | #import "APLTransitionLayout.h" 50 | #import "APLCollectionViewCell.h" 51 | 52 | #define USE_IMAGES 1 // if 1 images are used for each cell, if 0 we use varying UIColors swatches for each cell 53 | 54 | #define MAX_COUNT 24 55 | #define CELL_ID @"CELL_ID" 56 | 57 | 58 | #pragma mark - 59 | 60 | @implementation APLCollectionViewController 61 | 62 | - (id)initWithCollectionViewLayout:(UICollectionViewFlowLayout *)layout 63 | { 64 | if (self = [super initWithCollectionViewLayout:layout]) 65 | { 66 | // make sure we know about our cell prototype so dequeueReusableCellWithReuseIdentifier can work 67 | [self.collectionView registerClass:[APLCollectionViewCell class] forCellWithReuseIdentifier:CELL_ID]; 68 | } 69 | return self; 70 | } 71 | 72 | - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 73 | { 74 | APLCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_ID forIndexPath:indexPath]; 75 | 76 | #if USE_IMAGES 77 | // set the cell to use an image 78 | cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"sa%ld.jpg", (long)indexPath.item]]; 79 | #else 80 | // set the cell to use a color swatch 81 | CGFloat hue = ((CGFloat)indexPath.item)/MAX_COUNT; 82 | UIColor *cellColor = [UIColor colorWithHue:hue saturation:1.0 brightness:1.0 alpha:1.0]; 83 | cell.contentView.backgroundColor = cellColor; 84 | #endif 85 | 86 | return cell; 87 | } 88 | 89 | - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 90 | { 91 | return MAX_COUNT; 92 | } 93 | 94 | - (UICollectionViewController *)nextViewControllerAtPoint:(CGPoint)p 95 | { 96 | return nil; // subclass must override this method 97 | } 98 | 99 | // return our own UICollectionViewTransitionLayout object subclass to help in the transition 100 | // the cell positions based on gesture position 101 | // 102 | - (UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout 103 | { 104 | APLTransitionLayout *myCustomTransitionLayout = 105 | [[APLTransitionLayout alloc] initWithCurrentLayout:fromLayout nextLayout:toLayout]; 106 | return myCustomTransitionLayout; 107 | } 108 | 109 | @end 110 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLDetailViewController.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLDetailViewController.h 3 | Abstract: The view controller showing a single image (used as the detail view controller) 4 | 5 | Version: 1.1 6 | 7 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 8 | Inc. ("Apple") in consideration of your agreement to the following 9 | terms, and your use, installation, modification or redistribution of 10 | this Apple software constitutes acceptance of these terms. If you do 11 | not agree with these terms, please do not use, install, modify or 12 | redistribute this Apple software. 13 | 14 | In consideration of your agreement to abide by the following terms, and 15 | subject to these terms, Apple grants you a personal, non-exclusive 16 | license, under Apple's copyrights in this original Apple software (the 17 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 18 | Software, with or without modifications, in source and/or binary forms; 19 | provided that if you redistribute the Apple Software in its entirety and 20 | without modifications, you must retain this notice and the following 21 | text and disclaimers in all such redistributions of the Apple Software. 22 | Neither the name, trademarks, service marks or logos of Apple Inc. may 23 | be used to endorse or promote products derived from the Apple Software 24 | without specific prior written permission from Apple. Except as 25 | expressly stated in this notice, no other rights or licenses, express or 26 | implied, are granted by Apple herein, including but not limited to any 27 | patent rights that may be infringed by your derivative works or by other 28 | works in which the Apple Software may be incorporated. 29 | 30 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 31 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 32 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 33 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 34 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 35 | 36 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 37 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 40 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 41 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 42 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 43 | POSSIBILITY OF SUCH DAMAGE. 44 | 45 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 46 | 47 | */ 48 | 49 | #import 50 | 51 | @interface APLDetailViewController : UIViewController 52 | 53 | @property (nonatomic, strong) IBOutlet UIImage *image; 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLDetailViewController.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLDetailViewController.m 3 | Abstract: The view controller showing a single image (used as the detail view controller) 4 | 5 | Version: 1.1 6 | 7 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 8 | Inc. ("Apple") in consideration of your agreement to the following 9 | terms, and your use, installation, modification or redistribution of 10 | this Apple software constitutes acceptance of these terms. If you do 11 | not agree with these terms, please do not use, install, modify or 12 | redistribute this Apple software. 13 | 14 | In consideration of your agreement to abide by the following terms, and 15 | subject to these terms, Apple grants you a personal, non-exclusive 16 | license, under Apple's copyrights in this original Apple software (the 17 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 18 | Software, with or without modifications, in source and/or binary forms; 19 | provided that if you redistribute the Apple Software in its entirety and 20 | without modifications, you must retain this notice and the following 21 | text and disclaimers in all such redistributions of the Apple Software. 22 | Neither the name, trademarks, service marks or logos of Apple Inc. may 23 | be used to endorse or promote products derived from the Apple Software 24 | without specific prior written permission from Apple. Except as 25 | expressly stated in this notice, no other rights or licenses, express or 26 | implied, are granted by Apple herein, including but not limited to any 27 | patent rights that may be infringed by your derivative works or by other 28 | works in which the Apple Software may be incorporated. 29 | 30 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 31 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 32 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 33 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 34 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 35 | 36 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 37 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 40 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 41 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 42 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 43 | POSSIBILITY OF SUCH DAMAGE. 44 | 45 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 46 | 47 | */ 48 | 49 | #import "APLDetailViewController.h" 50 | 51 | @interface APLDetailViewController () 52 | @property (nonatomic, retain) IBOutlet UIImageView *imageView; 53 | @end 54 | 55 | #pragma mark - 56 | 57 | @implementation APLDetailViewController 58 | 59 | - (void)viewWillAppear:(BOOL)animated 60 | { 61 | // setup our image view if an image was set to this view controller 62 | self.imageView.image = self.image; 63 | } 64 | 65 | @end 66 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLGridCollectionViewController.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLGridCollectionViewController.h 3 | Abstract: The UICollectionViewController containing the grid default layout. 4 | 5 | Version: 1.1 6 | 7 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 8 | Inc. ("Apple") in consideration of your agreement to the following 9 | terms, and your use, installation, modification or redistribution of 10 | this Apple software constitutes acceptance of these terms. If you do 11 | not agree with these terms, please do not use, install, modify or 12 | redistribute this Apple software. 13 | 14 | In consideration of your agreement to abide by the following terms, and 15 | subject to these terms, Apple grants you a personal, non-exclusive 16 | license, under Apple's copyrights in this original Apple software (the 17 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 18 | Software, with or without modifications, in source and/or binary forms; 19 | provided that if you redistribute the Apple Software in its entirety and 20 | without modifications, you must retain this notice and the following 21 | text and disclaimers in all such redistributions of the Apple Software. 22 | Neither the name, trademarks, service marks or logos of Apple Inc. may 23 | be used to endorse or promote products derived from the Apple Software 24 | without specific prior written permission from Apple. Except as 25 | expressly stated in this notice, no other rights or licenses, express or 26 | implied, are granted by Apple herein, including but not limited to any 27 | patent rights that may be infringed by your derivative works or by other 28 | works in which the Apple Software may be incorporated. 29 | 30 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 31 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 32 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 33 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 34 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 35 | 36 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 37 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 40 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 41 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 42 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 43 | POSSIBILITY OF SUCH DAMAGE. 44 | 45 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 46 | 47 | */ 48 | 49 | @import UIKit; 50 | 51 | #import "APLCollectionViewController.h" 52 | 53 | @interface APLGridCollectionViewController : APLCollectionViewController 54 | 55 | @end 56 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLGridCollectionViewController.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLGridCollectionViewController.m 3 | Abstract: The UICollectionViewController containing the grid default layout. 4 | 5 | Version: 1.1 6 | 7 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 8 | Inc. ("Apple") in consideration of your agreement to the following 9 | terms, and your use, installation, modification or redistribution of 10 | this Apple software constitutes acceptance of these terms. If you do 11 | not agree with these terms, please do not use, install, modify or 12 | redistribute this Apple software. 13 | 14 | In consideration of your agreement to abide by the following terms, and 15 | subject to these terms, Apple grants you a personal, non-exclusive 16 | license, under Apple's copyrights in this original Apple software (the 17 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 18 | Software, with or without modifications, in source and/or binary forms; 19 | provided that if you redistribute the Apple Software in its entirety and 20 | without modifications, you must retain this notice and the following 21 | text and disclaimers in all such redistributions of the Apple Software. 22 | Neither the name, trademarks, service marks or logos of Apple Inc. may 23 | be used to endorse or promote products derived from the Apple Software 24 | without specific prior written permission from Apple. Except as 25 | expressly stated in this notice, no other rights or licenses, express or 26 | implied, are granted by Apple herein, including but not limited to any 27 | patent rights that may be infringed by your derivative works or by other 28 | works in which the Apple Software may be incorporated. 29 | 30 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 31 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 32 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 33 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 34 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 35 | 36 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 37 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 40 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 41 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 42 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 43 | POSSIBILITY OF SUCH DAMAGE. 44 | 45 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 46 | 47 | */ 48 | 49 | #import "APLGridCollectionViewController.h" 50 | #import "APLDetailViewController.h" 51 | #import "APLCollectionViewCell.h" 52 | 53 | @implementation APLGridCollectionViewController 54 | 55 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 56 | { 57 | // used tapped a collection view cell, navigate to a detail view controller showing that single photo 58 | APLCollectionViewCell *cell = (APLCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; 59 | if (cell.imageView.image != nil) 60 | { 61 | // we need to load the main storyboard because this view controller was created programmatically 62 | UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 63 | 64 | APLDetailViewController *detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"detailVC"]; 65 | detailViewController.image = cell.imageView.image; 66 | [self.navigationController pushViewController:detailViewController animated:YES]; 67 | } 68 | } 69 | 70 | @end 71 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLStackCollectionViewController.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLStackCollectionViewController.h 3 | Abstract: The UICollectionViewController containing the custom stack layout. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | @import UIKit; 49 | 50 | #import "APLCollectionViewController.h" 51 | 52 | @interface APLStackCollectionViewController : APLCollectionViewController 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLStackCollectionViewController.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLStackCollectionViewController.m 3 | Abstract: The UICollectionViewController containing the custom stack layout. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | #import "APLStackCollectionViewController.h" 49 | #import "APLGridCollectionViewController.h" 50 | #import "APLTransitionLayout.h" 51 | 52 | 53 | @implementation APLStackCollectionViewController 54 | 55 | - (void)viewDidAppear:(BOOL)animated 56 | { 57 | [super viewDidAppear:animated]; 58 | 59 | // NOTE: the following line of code is necessary to work around a bug in UICollectionView, 60 | // when you transition back to this view controller from a pinch inward gesture, 61 | // the z-ordering of the stacked photos may be wrong. 62 | // 63 | [self.collectionView reloadData]; 64 | } 65 | 66 | - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 67 | { 68 | // user tapped a stack of photos, navigate the grid layout view controller 69 | [self.navigationController pushViewController:[self nextViewControllerAtPoint:CGPointZero] animated:YES]; 70 | } 71 | 72 | // obtain the next collection view controller based on where the user tapped in case there are multiple stacks 73 | - (UICollectionViewController *)nextViewControllerAtPoint:(CGPoint)p 74 | { 75 | // we could have multiple section stacks, so we need to find the right one 76 | UICollectionViewFlowLayout *grid = [[UICollectionViewFlowLayout alloc] init]; 77 | grid.itemSize = CGSizeMake(75.0, 75.0); 78 | grid.sectionInset = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0); 79 | 80 | APLGridCollectionViewController *nextCollectionViewController = 81 | [[APLGridCollectionViewController alloc] initWithCollectionViewLayout:grid]; 82 | 83 | // Set "useLayoutToLayoutNavigationTransitions" to YES before pushing a a 84 | // UICollectionViewController onto a UINavigationController. The top view controller of 85 | // the navigation controller must be a UICollectionViewController that was pushed with 86 | // this property set to NO. This property should NOT be changed on a UICollectionViewController 87 | // that has already been pushed onto a UINavigationController. 88 | // 89 | nextCollectionViewController.useLayoutToLayoutNavigationTransitions = YES; 90 | 91 | nextCollectionViewController.title = @"Grid Layout"; 92 | 93 | return nextCollectionViewController; 94 | } 95 | 96 | @end 97 | 98 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLStackLayout.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLStackLayout.h 3 | Abstract: Custon collection view layout to stack collection view cells. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | @import UIKit; 49 | 50 | @interface APLStackLayout : UICollectionViewLayout 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLStackLayout.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLStackLayout.m 3 | Abstract: Custon collection view layout to stack collection view cells. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | #import "APLStackLayout.h" 49 | 50 | @interface APLStackLayout () 51 | 52 | @property (nonatomic, readwrite) NSInteger stackCount; 53 | @property (nonatomic, readwrite) CGSize itemSize; 54 | @property (nonatomic, readwrite) NSMutableArray *angles; 55 | @property (nonatomic, readwrite) NSMutableArray *attributesArray; 56 | 57 | @end 58 | 59 | #pragma mark - 60 | 61 | @implementation APLStackLayout 62 | 63 | - (id)init 64 | { 65 | self = [super init]; 66 | if (self != nil) 67 | { 68 | _stackCount = 5; 69 | _itemSize = CGSizeMake(150.0, 200.0); 70 | _angles = [[NSMutableArray alloc] initWithCapacity:self.stackCount * 10]; 71 | } 72 | return self; 73 | } 74 | 75 | - (void)prepareLayout 76 | { 77 | // Compute the angles for each photo in the stack layout 78 | // 79 | // Keep in mind we only display one section in this layout. 80 | // 81 | // We use rand() to generate the varying angles, but with always the same seed value 82 | // so we have consistent angles when calling this method. 83 | // 84 | srand(42); 85 | 86 | CGSize size = self.collectionView.bounds.size; 87 | CGPoint center = CGPointMake(size.width / 2.0, size.height / 2.0); 88 | 89 | // we only display one section in this layout 90 | NSInteger itemCount = [self.collectionView numberOfItemsInSection:0]; 91 | 92 | // remove all the old attributes 93 | [self.angles removeAllObjects]; 94 | 95 | CGFloat maxAngle = M_1_PI / 3.0; 96 | CGFloat minAngle = - M_1_PI / 3.0; 97 | CGFloat diff = maxAngle - minAngle; 98 | 99 | // compute and add the necessary angles for each photo 100 | [_angles addObject:@0.0]; 101 | for (NSInteger i = 1; i < self.stackCount * 10; i++) 102 | { 103 | CGFloat currentAngle = ((((CGFloat)rand()) / RAND_MAX) * diff) + minAngle; 104 | [self.angles addObject:[NSNumber numberWithFloat:currentAngle]]; 105 | } 106 | 107 | if (self.attributesArray == nil) 108 | { 109 | _attributesArray = [[NSMutableArray alloc] initWithCapacity:itemCount]; 110 | } 111 | // generate the new attributes array for each photo in the stack 112 | for (NSInteger i = 0; i < itemCount; i++) 113 | { 114 | NSInteger angleIndex = i % (self.stackCount * 10); 115 | 116 | NSNumber *angleNumber = self.angles[angleIndex]; 117 | 118 | CGFloat angle = angleNumber.floatValue; 119 | 120 | UICollectionViewLayoutAttributes *attributes = 121 | [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]]; 122 | attributes.size = self.itemSize; 123 | attributes.center = center; 124 | attributes.transform = CGAffineTransformMakeRotation(angle); 125 | 126 | if (i > self.stackCount) 127 | { 128 | attributes.alpha = 0.0; 129 | } 130 | else 131 | { 132 | attributes.alpha = 1.0; 133 | } 134 | attributes.zIndex = (itemCount - i); 135 | 136 | [self.attributesArray addObject:attributes]; 137 | } 138 | } 139 | 140 | - (void)invalidateLayout 141 | { 142 | [super invalidateLayout]; 143 | _attributesArray = nil; 144 | } 145 | 146 | - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds 147 | { 148 | CGRect bounds = self.collectionView.bounds; 149 | return ((CGRectGetWidth(newBounds) != CGRectGetWidth(bounds) || 150 | (CGRectGetHeight(newBounds) != CGRectGetHeight(bounds)))); 151 | } 152 | 153 | - (CGSize)collectionViewContentSize 154 | { 155 | return self.collectionView.bounds.size; 156 | } 157 | 158 | - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 159 | { 160 | return self.attributesArray[indexPath.item]; 161 | } 162 | 163 | - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 164 | { 165 | return self.attributesArray; 166 | } 167 | 168 | @end 169 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLTransitionLayout.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLTransitionLayout.h 3 | Abstract: Helps in the transition the of cell positions based on the pinch gesture position. 4 | 5 | Version: 1.1 6 | 7 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 8 | Inc. ("Apple") in consideration of your agreement to the following 9 | terms, and your use, installation, modification or redistribution of 10 | this Apple software constitutes acceptance of these terms. If you do 11 | not agree with these terms, please do not use, install, modify or 12 | redistribute this Apple software. 13 | 14 | In consideration of your agreement to abide by the following terms, and 15 | subject to these terms, Apple grants you a personal, non-exclusive 16 | license, under Apple's copyrights in this original Apple software (the 17 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 18 | Software, with or without modifications, in source and/or binary forms; 19 | provided that if you redistribute the Apple Software in its entirety and 20 | without modifications, you must retain this notice and the following 21 | text and disclaimers in all such redistributions of the Apple Software. 22 | Neither the name, trademarks, service marks or logos of Apple Inc. may 23 | be used to endorse or promote products derived from the Apple Software 24 | without specific prior written permission from Apple. Except as 25 | expressly stated in this notice, no other rights or licenses, express or 26 | implied, are granted by Apple herein, including but not limited to any 27 | patent rights that may be infringed by your derivative works or by other 28 | works in which the Apple Software may be incorporated. 29 | 30 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 31 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 32 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 33 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 34 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 35 | 36 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 37 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 40 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 41 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 42 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 43 | POSSIBILITY OF SUCH DAMAGE. 44 | 45 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 46 | 47 | */ 48 | 49 | @import UIKit; 50 | 51 | // we implement our own subclass of UICollectionViewTransitionLayout to help in the transition 52 | // of the cell positions based on gesture position 53 | // 54 | @interface APLTransitionLayout : UICollectionViewTransitionLayout 55 | 56 | @property (nonatomic) UIOffset offset; 57 | 58 | @end 59 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLTransitionLayout.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLTransitionLayout.m 3 | Abstract: Helps in the transition the of cell positions based on the pinch gesture position. 4 | 5 | Version: 1.1 6 | 7 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 8 | Inc. ("Apple") in consideration of your agreement to the following 9 | terms, and your use, installation, modification or redistribution of 10 | this Apple software constitutes acceptance of these terms. If you do 11 | not agree with these terms, please do not use, install, modify or 12 | redistribute this Apple software. 13 | 14 | In consideration of your agreement to abide by the following terms, and 15 | subject to these terms, Apple grants you a personal, non-exclusive 16 | license, under Apple's copyrights in this original Apple software (the 17 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 18 | Software, with or without modifications, in source and/or binary forms; 19 | provided that if you redistribute the Apple Software in its entirety and 20 | without modifications, you must retain this notice and the following 21 | text and disclaimers in all such redistributions of the Apple Software. 22 | Neither the name, trademarks, service marks or logos of Apple Inc. may 23 | be used to endorse or promote products derived from the Apple Software 24 | without specific prior written permission from Apple. Except as 25 | expressly stated in this notice, no other rights or licenses, express or 26 | implied, are granted by Apple herein, including but not limited to any 27 | patent rights that may be infringed by your derivative works or by other 28 | works in which the Apple Software may be incorporated. 29 | 30 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 31 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 32 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 33 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 34 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 35 | 36 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 37 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 38 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 39 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 40 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 41 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 42 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 43 | POSSIBILITY OF SUCH DAMAGE. 44 | 45 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 46 | 47 | */ 48 | 49 | #import "APLTransitionLayout.h" 50 | 51 | static NSString *kOffsetH = @"offsetH"; 52 | static NSString *kOffsetV = @"offsetV"; 53 | 54 | @implementation APLTransitionLayout 55 | 56 | // set the completion progress of the current transition. 57 | // 58 | - (void)setTransitionProgress:(CGFloat)transitionProgress 59 | { 60 | [super setTransitionProgress:transitionProgress]; 61 | 62 | // return the most recently set values for each key 63 | CGFloat offsetH = [self valueForAnimatedKey:kOffsetH]; 64 | CGFloat offsetV = [self valueForAnimatedKey:kOffsetV]; 65 | _offset = UIOffsetMake(offsetH, offsetV); 66 | } 67 | 68 | // called by the APLTransitionManager class while updating its transition progress, animating 69 | // the collection view items in an out of stack mode. 70 | // 71 | - (void)setOffset:(UIOffset)offset 72 | { 73 | // store the floating-point values with out meaningful keys for our transition layout object 74 | [self updateValue:offset.horizontal forAnimatedKey:kOffsetH]; 75 | [self updateValue:offset.vertical forAnimatedKey:kOffsetV]; 76 | _offset = offset; 77 | } 78 | 79 | // return the layout attributes for all of the cells and views in the specified rectangle 80 | - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 81 | { 82 | NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; 83 | 84 | for (UICollectionViewLayoutAttributes *currentAttribute in attributes) 85 | { 86 | CGPoint currentCenter = currentAttribute.center; 87 | CGPoint updatedCenter = CGPointMake(currentCenter.x + self.offset.horizontal, currentCenter.y + self.offset.vertical); 88 | currentAttribute.center = updatedCenter; 89 | } 90 | return attributes; 91 | } 92 | 93 | - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 94 | { 95 | // returns the layout attributes for the item at the specified index path 96 | UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForItemAtIndexPath:indexPath]; 97 | CGPoint currentCenter = attributes.center; 98 | CGPoint updatedCenter = CGPointMake(currentCenter.x + self.offset.horizontal, currentCenter.y + self.offset.vertical); 99 | attributes.center = updatedCenter; 100 | return attributes; 101 | } 102 | 103 | @end 104 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLTransitionManager.h: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLTransitionManager.h 3 | Abstract: Responsible for managing the transition between the two collection views via the pinch gesture recognizer. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | @import UIKit; 49 | 50 | @protocol APLTransitionManagerDelegate 51 | - (void)interactionBeganAtPoint:(CGPoint)p; 52 | @end 53 | 54 | #pragma mark - 55 | 56 | @interface APLTransitionManager : NSObject 57 | 58 | 59 | @property (nonatomic) id delegate; 60 | @property (nonatomic) BOOL hasActiveInteraction; 61 | @property (nonatomic) UINavigationControllerOperation navigationOperation; 62 | @property (nonatomic) UICollectionView *collectionView; 63 | 64 | - (instancetype)initWithCollectionView:(UICollectionView *)collectionView; 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/APLTransitionManager.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: APLTransitionManager.m 3 | Abstract: Responsible for managing the transition between the two collection views via the pinch gesture recognizer. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | #import "APLTransitionManager.h" 49 | #import "APLTransitionLayout.h" 50 | 51 | @interface APLTransitionManager () 52 | 53 | @property (nonatomic) APLTransitionLayout *transitionLayout; 54 | @property (nonatomic) id context; 55 | @property (nonatomic) CGFloat initialPinchDistance; 56 | @property (nonatomic) CGPoint initialPinchPoint; 57 | 58 | @end 59 | 60 | #pragma mark - 61 | 62 | @implementation APLTransitionManager 63 | 64 | - (instancetype)initWithCollectionView:(UICollectionView *)collectionView 65 | { 66 | self = [super init]; 67 | if (self != nil) 68 | { 69 | // setup our pinch gesture: 70 | // pinch in closes photos down into a stack, 71 | // pinch out expands the photos intoa grid 72 | // 73 | UIPinchGestureRecognizer *pinchGesture = 74 | [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; 75 | [collectionView addGestureRecognizer:pinchGesture]; 76 | 77 | self.collectionView = collectionView; 78 | } 79 | return self; 80 | } 81 | 82 | - (void)animateTransition:(id)transitionContext 83 | { 84 | 85 | } 86 | 87 | - (NSTimeInterval)transitionDuration:(id)transitionContext 88 | { 89 | // transition animation time between grid and stack layout 90 | return 1.0; 91 | } 92 | 93 | // required method for view controller transitions, called when the system needs to set up 94 | // the interactive portions of a view controller transition and start the animations 95 | // 96 | - (void)startInteractiveTransition:(id )transitionContext 97 | { 98 | self.context = transitionContext; 99 | 100 | UICollectionViewController *fromCollectionViewController = 101 | (UICollectionViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; 102 | UICollectionViewController *toCollectionViewController = 103 | (UICollectionViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; 104 | 105 | UIView *containerView = [transitionContext containerView]; 106 | [containerView addSubview:[toCollectionViewController view]]; 107 | 108 | self.transitionLayout = (APLTransitionLayout *)[fromCollectionViewController.collectionView startInteractiveTransitionToCollectionViewLayout:toCollectionViewController.collectionViewLayout completion:^(BOOL didFinish, BOOL didComplete) { 109 | [self.context completeTransition:didComplete]; 110 | self.transitionLayout = nil; 111 | self.context = nil; 112 | self.hasActiveInteraction = NO; 113 | }]; 114 | } 115 | 116 | - (void)updateWithProgress:(CGFloat)progress andOffset:(UIOffset)offset 117 | { 118 | if (self.context != nil && // we must have a valid context for updates 119 | ((progress != self.transitionLayout.transitionProgress) || !UIOffsetEqualToOffset(offset, self.transitionLayout.offset))) 120 | { 121 | [self.transitionLayout setOffset:offset]; 122 | [self.transitionLayout setTransitionProgress:progress]; 123 | [self.transitionLayout invalidateLayout]; 124 | [self.context updateInteractiveTransition:progress]; 125 | } 126 | } 127 | 128 | // called by our pinch gesture recognizer when the gesture has finished or cancelled, which 129 | // in turn is responsible for finishing or cancelling the transition. 130 | // 131 | - (void)endInteractionWithSuccess:(BOOL)success 132 | { 133 | if (self.context == nil) 134 | { 135 | self.hasActiveInteraction = NO; 136 | } 137 | // allow for the transition to finish when it's progress has started as a threshold of 10%, 138 | // if you want to require the pinch gesture with a wider threshold, change it it a value closer to 1.0 139 | // 140 | else if ((self.transitionLayout.transitionProgress > 0.1) && success) 141 | { 142 | [self.collectionView finishInteractiveTransition]; 143 | [self.context finishInteractiveTransition]; 144 | } 145 | else 146 | { 147 | [self.collectionView cancelInteractiveTransition]; 148 | [self.context cancelInteractiveTransition]; 149 | } 150 | } 151 | 152 | // action method for our pinch gesture recognizer 153 | // 154 | - (void)handlePinch:(UIPinchGestureRecognizer *)sender 155 | { 156 | // here we want to end the transition interaction if the user stops or finishes the pinch gesture 157 | if (sender.state == UIGestureRecognizerStateEnded) 158 | { 159 | [self endInteractionWithSuccess:YES]; 160 | } 161 | else if (sender.state == UIGestureRecognizerStateCancelled) 162 | { 163 | [self endInteractionWithSuccess:NO]; 164 | } 165 | else if (sender.numberOfTouches == 2) 166 | { 167 | // here we expect two finger touch 168 | CGPoint point; // the main touch point 169 | CGPoint point1; // location of touch #1 170 | CGPoint point2; // location of touch #2 171 | CGFloat distance; // computed distance between both touches 172 | 173 | // return the locations of each gesture’s touches in the local coordinate system of a given view 174 | point1 = [sender locationOfTouch:0 inView:sender.view]; 175 | point2 = [sender locationOfTouch:1 inView:sender.view]; 176 | distance = sqrt((point1.x - point2.x) * (point1.x - point2.x) + (point1.y - point2.y) * (point1.y - point2.y)); 177 | 178 | // get the main touch point 179 | point = [sender locationInView:sender.view]; 180 | 181 | if (sender.state == UIGestureRecognizerStateBegan) 182 | { 183 | // start the pinch in our out 184 | if (!self.hasActiveInteraction) 185 | { 186 | self.initialPinchDistance = distance; 187 | self.initialPinchPoint = point; 188 | self.hasActiveInteraction = YES; // the transition is in active motion 189 | [self.delegate interactionBeganAtPoint:point]; 190 | } 191 | } 192 | 193 | if (self.hasActiveInteraction) 194 | { 195 | if (sender.state == UIGestureRecognizerStateChanged) 196 | { 197 | // update the progress of the transtition as the user continues to pinch 198 | CGFloat offsetX = point.x - self.initialPinchPoint.x; 199 | CGFloat offsetY = point.y - self.initialPinchPoint.y; 200 | UIOffset offsetToUse = UIOffsetMake(offsetX, offsetY); 201 | 202 | CGFloat distanceDelta = distance - self.initialPinchDistance; 203 | if (self.navigationOperation == UINavigationControllerOperationPop) 204 | { 205 | distanceDelta = -distanceDelta; 206 | } 207 | CGFloat dimension = sqrt(self.collectionView.bounds.size.width * self.collectionView.bounds.size.width + self.collectionView.bounds.size.height * self.collectionView.bounds.size.height); 208 | CGFloat progress = MAX(MIN((distanceDelta / dimension), 1.0), 0.0); 209 | 210 | // tell our UICollectionViewTransitionLayout subclass (transitionLayout) 211 | // the progress state of the pinch gesture 212 | // 213 | [self updateWithProgress:progress andOffset:offsetToUse]; 214 | } 215 | } 216 | } 217 | } 218 | 219 | @end 220 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/CollectionViewTransition-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.example.apple-samplecode.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.1 25 | LSRequiresIPhoneOS 26 | 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/CollectionViewTransition-Prefix.pch: -------------------------------------------------------------------------------- 1 | #import 2 | 3 | #ifndef __IPHONE_7_0 4 | #warning "This project uses features only available in iOS SDK 7.0 and later." 5 | #endif 6 | 7 | #ifdef __OBJC__ 8 | @import UIKit; 9 | @import Foundation; 10 | #endif 11 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa0.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa1.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa10.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa11.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa12.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa13.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa14.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa15.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa16.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa17.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa17.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa18.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa18.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa19.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa19.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa2.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa20.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa21.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa21.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa22.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa22.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa23.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa23.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa24.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa24.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa3.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa4.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa5.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa6.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa7.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa8.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa9.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanc/OpenCode_62/4cf85775553714cb52c24e51a86444eda31a55cc/CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/Images/sa9.jpg -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/CollectionViewTransition/main.m: -------------------------------------------------------------------------------- 1 | /* 2 | File: main.m 3 | Abstract: Standard main file. 4 | Version: 1.1 5 | 6 | Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple 7 | Inc. ("Apple") in consideration of your agreement to the following 8 | terms, and your use, installation, modification or redistribution of 9 | this Apple software constitutes acceptance of these terms. If you do 10 | not agree with these terms, please do not use, install, modify or 11 | redistribute this Apple software. 12 | 13 | In consideration of your agreement to abide by the following terms, and 14 | subject to these terms, Apple grants you a personal, non-exclusive 15 | license, under Apple's copyrights in this original Apple software (the 16 | "Apple Software"), to use, reproduce, modify and redistribute the Apple 17 | Software, with or without modifications, in source and/or binary forms; 18 | provided that if you redistribute the Apple Software in its entirety and 19 | without modifications, you must retain this notice and the following 20 | text and disclaimers in all such redistributions of the Apple Software. 21 | Neither the name, trademarks, service marks or logos of Apple Inc. may 22 | be used to endorse or promote products derived from the Apple Software 23 | without specific prior written permission from Apple. Except as 24 | expressly stated in this notice, no other rights or licenses, express or 25 | implied, are granted by Apple herein, including but not limited to any 26 | patent rights that may be infringed by your derivative works or by other 27 | works in which the Apple Software may be incorporated. 28 | 29 | The Apple Software is provided by Apple on an "AS IS" basis. APPLE 30 | MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION 31 | THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS 32 | FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND 33 | OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. 34 | 35 | IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL 36 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 | INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, 39 | MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED 40 | AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), 41 | STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE 42 | POSSIBILITY OF SUCH DAMAGE. 43 | 44 | Copyright (C) 2013 Apple Inc. All Rights Reserved. 45 | 46 | */ 47 | 48 | @import UIKit; 49 | 50 | #import "APLAppDelegate.h" 51 | 52 | int main(int argc, char * argv[]) 53 | { 54 | @autoreleasepool { 55 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([APLAppDelegate class])); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /CollectionView-AppleCode/CollectionViewTransition/ReadMe.txt: -------------------------------------------------------------------------------- 1 | Collection View Transition 2 | ========================== 3 | 4 | This sample illustrates how to create a custom transition when navigating between two collection views in a navigation hierarchy managed by a navigation controller. It uses a subclass of UICollectionViewTransitionLayout to help in the transition of the cell positions based on gesture position. 5 | 6 | The application has two view collection view controllers that display images. The first is a stack view, the second is a grid view. You can transition from the stack to the grid by tapping on the stack. You can also use a pinch gesture, in which case you can control the speed of, and even reverse, the transition. 7 | 8 | =========================================================================== 9 | BUILD REQUIREMENTS: 10 | 11 | Xcode 5.0, iOS 7.0 or later 12 | 13 | =========================================================================== 14 | RUNTIME REQUIREMENTS: 15 | 16 | iOS 7.0 or later 17 | 18 | =========================================================================== 19 | Copyright (C) 2013 Apple Inc. All rights reserved. -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | FD704DD31C892FEC00CB29B3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = FD704DD21C892FEC00CB29B3 /* main.m */; }; 11 | FD704DD61C892FEC00CB29B3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = FD704DD51C892FEC00CB29B3 /* AppDelegate.m */; }; 12 | FD704DD91C892FEC00CB29B3 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = FD704DD81C892FEC00CB29B3 /* ViewController.m */; }; 13 | FD704DDC1C892FEC00CB29B3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FD704DDA1C892FEC00CB29B3 /* Main.storyboard */; }; 14 | FD704DDE1C892FEC00CB29B3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FD704DDD1C892FEC00CB29B3 /* Assets.xcassets */; }; 15 | FD704DE11C892FEC00CB29B3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FD704DDF1C892FEC00CB29B3 /* LaunchScreen.storyboard */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | FD704DCE1C892FEC00CB29B3 /* NSURLSession_Test.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NSURLSession_Test.app; sourceTree = BUILT_PRODUCTS_DIR; }; 20 | FD704DD21C892FEC00CB29B3 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 21 | FD704DD41C892FEC00CB29B3 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 22 | FD704DD51C892FEC00CB29B3 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 23 | FD704DD71C892FEC00CB29B3 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 24 | FD704DD81C892FEC00CB29B3 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 25 | FD704DDB1C892FEC00CB29B3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 26 | FD704DDD1C892FEC00CB29B3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 27 | FD704DE01C892FEC00CB29B3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 28 | FD704DE21C892FEC00CB29B3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 29 | /* End PBXFileReference section */ 30 | 31 | /* Begin PBXFrameworksBuildPhase section */ 32 | FD704DCB1C892FEC00CB29B3 /* Frameworks */ = { 33 | isa = PBXFrameworksBuildPhase; 34 | buildActionMask = 2147483647; 35 | files = ( 36 | ); 37 | runOnlyForDeploymentPostprocessing = 0; 38 | }; 39 | /* End PBXFrameworksBuildPhase section */ 40 | 41 | /* Begin PBXGroup section */ 42 | FD704DC51C892FEC00CB29B3 = { 43 | isa = PBXGroup; 44 | children = ( 45 | FD704DD01C892FEC00CB29B3 /* NSURLSession_Test */, 46 | FD704DCF1C892FEC00CB29B3 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | FD704DCF1C892FEC00CB29B3 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | FD704DCE1C892FEC00CB29B3 /* NSURLSession_Test.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | FD704DD01C892FEC00CB29B3 /* NSURLSession_Test */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | FD704DD41C892FEC00CB29B3 /* AppDelegate.h */, 62 | FD704DD51C892FEC00CB29B3 /* AppDelegate.m */, 63 | FD704DD71C892FEC00CB29B3 /* ViewController.h */, 64 | FD704DD81C892FEC00CB29B3 /* ViewController.m */, 65 | FD704DDA1C892FEC00CB29B3 /* Main.storyboard */, 66 | FD704DDD1C892FEC00CB29B3 /* Assets.xcassets */, 67 | FD704DDF1C892FEC00CB29B3 /* LaunchScreen.storyboard */, 68 | FD704DE21C892FEC00CB29B3 /* Info.plist */, 69 | FD704DD11C892FEC00CB29B3 /* Supporting Files */, 70 | ); 71 | path = NSURLSession_Test; 72 | sourceTree = ""; 73 | }; 74 | FD704DD11C892FEC00CB29B3 /* Supporting Files */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | FD704DD21C892FEC00CB29B3 /* main.m */, 78 | ); 79 | name = "Supporting Files"; 80 | sourceTree = ""; 81 | }; 82 | /* End PBXGroup section */ 83 | 84 | /* Begin PBXNativeTarget section */ 85 | FD704DCD1C892FEC00CB29B3 /* NSURLSession_Test */ = { 86 | isa = PBXNativeTarget; 87 | buildConfigurationList = FD704DE51C892FEC00CB29B3 /* Build configuration list for PBXNativeTarget "NSURLSession_Test" */; 88 | buildPhases = ( 89 | FD704DCA1C892FEC00CB29B3 /* Sources */, 90 | FD704DCB1C892FEC00CB29B3 /* Frameworks */, 91 | FD704DCC1C892FEC00CB29B3 /* Resources */, 92 | ); 93 | buildRules = ( 94 | ); 95 | dependencies = ( 96 | ); 97 | name = NSURLSession_Test; 98 | productName = NSURLSession_Test; 99 | productReference = FD704DCE1C892FEC00CB29B3 /* NSURLSession_Test.app */; 100 | productType = "com.apple.product-type.application"; 101 | }; 102 | /* End PBXNativeTarget section */ 103 | 104 | /* Begin PBXProject section */ 105 | FD704DC61C892FEC00CB29B3 /* Project object */ = { 106 | isa = PBXProject; 107 | attributes = { 108 | LastUpgradeCheck = 0720; 109 | ORGANIZATIONNAME = SuperWang; 110 | TargetAttributes = { 111 | FD704DCD1C892FEC00CB29B3 = { 112 | CreatedOnToolsVersion = 7.2; 113 | }; 114 | }; 115 | }; 116 | buildConfigurationList = FD704DC91C892FEC00CB29B3 /* Build configuration list for PBXProject "NSURLSession_Test" */; 117 | compatibilityVersion = "Xcode 3.2"; 118 | developmentRegion = English; 119 | hasScannedForEncodings = 0; 120 | knownRegions = ( 121 | en, 122 | Base, 123 | ); 124 | mainGroup = FD704DC51C892FEC00CB29B3; 125 | productRefGroup = FD704DCF1C892FEC00CB29B3 /* Products */; 126 | projectDirPath = ""; 127 | projectRoot = ""; 128 | targets = ( 129 | FD704DCD1C892FEC00CB29B3 /* NSURLSession_Test */, 130 | ); 131 | }; 132 | /* End PBXProject section */ 133 | 134 | /* Begin PBXResourcesBuildPhase section */ 135 | FD704DCC1C892FEC00CB29B3 /* Resources */ = { 136 | isa = PBXResourcesBuildPhase; 137 | buildActionMask = 2147483647; 138 | files = ( 139 | FD704DE11C892FEC00CB29B3 /* LaunchScreen.storyboard in Resources */, 140 | FD704DDE1C892FEC00CB29B3 /* Assets.xcassets in Resources */, 141 | FD704DDC1C892FEC00CB29B3 /* Main.storyboard in Resources */, 142 | ); 143 | runOnlyForDeploymentPostprocessing = 0; 144 | }; 145 | /* End PBXResourcesBuildPhase section */ 146 | 147 | /* Begin PBXSourcesBuildPhase section */ 148 | FD704DCA1C892FEC00CB29B3 /* Sources */ = { 149 | isa = PBXSourcesBuildPhase; 150 | buildActionMask = 2147483647; 151 | files = ( 152 | FD704DD91C892FEC00CB29B3 /* ViewController.m in Sources */, 153 | FD704DD61C892FEC00CB29B3 /* AppDelegate.m in Sources */, 154 | FD704DD31C892FEC00CB29B3 /* main.m in Sources */, 155 | ); 156 | runOnlyForDeploymentPostprocessing = 0; 157 | }; 158 | /* End PBXSourcesBuildPhase section */ 159 | 160 | /* Begin PBXVariantGroup section */ 161 | FD704DDA1C892FEC00CB29B3 /* Main.storyboard */ = { 162 | isa = PBXVariantGroup; 163 | children = ( 164 | FD704DDB1C892FEC00CB29B3 /* Base */, 165 | ); 166 | name = Main.storyboard; 167 | sourceTree = ""; 168 | }; 169 | FD704DDF1C892FEC00CB29B3 /* LaunchScreen.storyboard */ = { 170 | isa = PBXVariantGroup; 171 | children = ( 172 | FD704DE01C892FEC00CB29B3 /* Base */, 173 | ); 174 | name = LaunchScreen.storyboard; 175 | sourceTree = ""; 176 | }; 177 | /* End PBXVariantGroup section */ 178 | 179 | /* Begin XCBuildConfiguration section */ 180 | FD704DE31C892FEC00CB29B3 /* Debug */ = { 181 | isa = XCBuildConfiguration; 182 | buildSettings = { 183 | ALWAYS_SEARCH_USER_PATHS = NO; 184 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 185 | CLANG_CXX_LIBRARY = "libc++"; 186 | CLANG_ENABLE_MODULES = YES; 187 | CLANG_ENABLE_OBJC_ARC = YES; 188 | CLANG_WARN_BOOL_CONVERSION = YES; 189 | CLANG_WARN_CONSTANT_CONVERSION = YES; 190 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 191 | CLANG_WARN_EMPTY_BODY = YES; 192 | CLANG_WARN_ENUM_CONVERSION = YES; 193 | CLANG_WARN_INT_CONVERSION = YES; 194 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 195 | CLANG_WARN_UNREACHABLE_CODE = YES; 196 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 197 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 198 | COPY_PHASE_STRIP = NO; 199 | DEBUG_INFORMATION_FORMAT = dwarf; 200 | ENABLE_STRICT_OBJC_MSGSEND = YES; 201 | ENABLE_TESTABILITY = YES; 202 | GCC_C_LANGUAGE_STANDARD = gnu99; 203 | GCC_DYNAMIC_NO_PIC = NO; 204 | GCC_NO_COMMON_BLOCKS = YES; 205 | GCC_OPTIMIZATION_LEVEL = 0; 206 | GCC_PREPROCESSOR_DEFINITIONS = ( 207 | "DEBUG=1", 208 | "$(inherited)", 209 | ); 210 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 211 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 212 | GCC_WARN_UNDECLARED_SELECTOR = YES; 213 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 214 | GCC_WARN_UNUSED_FUNCTION = YES; 215 | GCC_WARN_UNUSED_VARIABLE = YES; 216 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 217 | MTL_ENABLE_DEBUG_INFO = YES; 218 | ONLY_ACTIVE_ARCH = YES; 219 | SDKROOT = iphoneos; 220 | }; 221 | name = Debug; 222 | }; 223 | FD704DE41C892FEC00CB29B3 /* Release */ = { 224 | isa = XCBuildConfiguration; 225 | buildSettings = { 226 | ALWAYS_SEARCH_USER_PATHS = NO; 227 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 228 | CLANG_CXX_LIBRARY = "libc++"; 229 | CLANG_ENABLE_MODULES = YES; 230 | CLANG_ENABLE_OBJC_ARC = YES; 231 | CLANG_WARN_BOOL_CONVERSION = YES; 232 | CLANG_WARN_CONSTANT_CONVERSION = YES; 233 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 234 | CLANG_WARN_EMPTY_BODY = YES; 235 | CLANG_WARN_ENUM_CONVERSION = YES; 236 | CLANG_WARN_INT_CONVERSION = YES; 237 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 238 | CLANG_WARN_UNREACHABLE_CODE = YES; 239 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 240 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 241 | COPY_PHASE_STRIP = NO; 242 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 243 | ENABLE_NS_ASSERTIONS = NO; 244 | ENABLE_STRICT_OBJC_MSGSEND = YES; 245 | GCC_C_LANGUAGE_STANDARD = gnu99; 246 | GCC_NO_COMMON_BLOCKS = YES; 247 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 248 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 249 | GCC_WARN_UNDECLARED_SELECTOR = YES; 250 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 251 | GCC_WARN_UNUSED_FUNCTION = YES; 252 | GCC_WARN_UNUSED_VARIABLE = YES; 253 | IPHONEOS_DEPLOYMENT_TARGET = 9.2; 254 | MTL_ENABLE_DEBUG_INFO = NO; 255 | SDKROOT = iphoneos; 256 | VALIDATE_PRODUCT = YES; 257 | }; 258 | name = Release; 259 | }; 260 | FD704DE61C892FEC00CB29B3 /* Debug */ = { 261 | isa = XCBuildConfiguration; 262 | buildSettings = { 263 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 264 | INFOPLIST_FILE = NSURLSession_Test/Info.plist; 265 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 266 | PRODUCT_BUNDLE_IDENTIFIER = "zy.NSURLSession-Test"; 267 | PRODUCT_NAME = "$(TARGET_NAME)"; 268 | }; 269 | name = Debug; 270 | }; 271 | FD704DE71C892FEC00CB29B3 /* Release */ = { 272 | isa = XCBuildConfiguration; 273 | buildSettings = { 274 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 275 | INFOPLIST_FILE = NSURLSession_Test/Info.plist; 276 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 277 | PRODUCT_BUNDLE_IDENTIFIER = "zy.NSURLSession-Test"; 278 | PRODUCT_NAME = "$(TARGET_NAME)"; 279 | }; 280 | name = Release; 281 | }; 282 | /* End XCBuildConfiguration section */ 283 | 284 | /* Begin XCConfigurationList section */ 285 | FD704DC91C892FEC00CB29B3 /* Build configuration list for PBXProject "NSURLSession_Test" */ = { 286 | isa = XCConfigurationList; 287 | buildConfigurations = ( 288 | FD704DE31C892FEC00CB29B3 /* Debug */, 289 | FD704DE41C892FEC00CB29B3 /* Release */, 290 | ); 291 | defaultConfigurationIsVisible = 0; 292 | defaultConfigurationName = Release; 293 | }; 294 | FD704DE51C892FEC00CB29B3 /* Build configuration list for PBXNativeTarget "NSURLSession_Test" */ = { 295 | isa = XCConfigurationList; 296 | buildConfigurations = ( 297 | FD704DE61C892FEC00CB29B3 /* Debug */, 298 | FD704DE71C892FEC00CB29B3 /* Release */, 299 | ); 300 | defaultConfigurationIsVisible = 0; 301 | }; 302 | /* End XCConfigurationList section */ 303 | }; 304 | rootObject = FD704DC61C892FEC00CB29B3 /* Project object */; 305 | } 306 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // NSURLSession_Test 4 | // 5 | // Created by SuperWang on 16/3/4. 6 | // Copyright © 2016年 SuperWang. 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 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // NSURLSession_Test 4 | // 5 | // Created by SuperWang on 16/3/4. 6 | // Copyright © 2016年 SuperWang. 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 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // 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. 25 | // 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. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // 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. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // 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. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // 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. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/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 | 27 | 28 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/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 | 26 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 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 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | NSAppTransportSecurity 40 | 41 | NSAllowsArbitraryLoads 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // NSURLSession_Test 4 | // 5 | // Created by SuperWang on 16/3/4. 6 | // Copyright © 2016年 SuperWang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // NSURLSession_Test 4 | // 5 | // Created by SuperWang on 16/3/4. 6 | // Copyright © 2016年 SuperWang. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | 20 | //文章分享 21 | //http://www.cocoachina.com/industry/20131106/7304.html 22 | //http://www.jianshu.com/p/ba7523372edf 23 | //http://blog.jobbole.com/52477/ 24 | 25 | 26 | //自己手动封装一个请求库系列 27 | //http://www.cocoachina.com/ios/20150526/11944.html 28 | //http://www.cocoachina.com/ios/20150526/11945.html 29 | //http://www.cocoachina.com/ios/20150526/11946.html 30 | 31 | /* 32 | Core Foundation中NSURLConnection在2003年就随着Safari一同发布,诞生在十多年前,由于iOS设备硬件和软件升级比较快,原有的NSURLConenection网络请求的性能需要重构,2013年iOS7中给出了新的网络请求方案-NSURLSession。日常的iOS开发中,AFNetWorking算是最常见的网络请求库,AFNetworking 1.0建立在NSURLConnection的基础之上,AFNetworking 2.0使用NSURLConnection基础API,以及较新基于NSURLSession的API的选项,3.X版本删除了所有基于NSURLConnection API的所有支持,新的API完全基于NSURLSession。 33 | 34 | NSURLSessionDataTask主要用于获取JSON/XML数据; 35 | NSURLSessionDownloadTask的作用是下载文件; 36 | NSURLSessionUploadTask上传文件会用到; 37 | */ 38 | 39 | 40 | //NSURLSessionDataTask 41 | //get请求,获得百度apistor中一个天气预报的接口 42 | //添加了请求头参数 43 | //设置了请求超时时间 44 | //1,url 45 | NSURL *url=[NSURL URLWithString:@"http://apis.baidu.com/heweather/pro/attractions?cityid=CN10101010018A"]; 46 | //2,requst 47 | NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10]; 48 | [urlRequest setHTTPMethod: @"GET"]; 49 | [urlRequest addValue: @"f087325ca8cce25cd894541e0ff3d9f4" forHTTPHeaderField: @"apikey"]; 50 | 51 | //3,URLSession 替换URLConnection 52 | NSURLSession *urlSession=[NSURLSession sharedSession]; 53 | 54 | //4,使用block的方式获得数据 55 | NSURLSessionDataTask *dataTask=[urlSession dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 56 | NSDictionary *content = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; 57 | NSLog(@"%@",content); 58 | }]; 59 | //恢复 60 | [dataTask resume]; 61 | 62 | 63 | 64 | //上传任务 65 | //NSURLSessionUploadTask 66 | //通过下面的方法,将需要上传的数据传入fromData可以实现上传的请求 67 | //NSURLSessionUploadTask *uploadTask = [urlSession uploadTaskWithRequest:<#(nonnull NSURLRequest *)#> fromData:<#(nonnull NSData *)#>]; 68 | 69 | 70 | 71 | //下载任务 72 | //NSURLSessionDownloadTask 73 | // NSURLSessionDownloadTask *downloadTask = [urlSession downloadTaskWithRequest:urlRequest completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { 74 | // 75 | // }]; 76 | 77 | } 78 | 79 | - (void)didReceiveMemoryWarning { 80 | [super didReceiveMemoryWarning]; 81 | // Dispose of any resources that can be recreated. 82 | } 83 | 84 | @end 85 | -------------------------------------------------------------------------------- /NSURLSession_Test/NSURLSession_Test/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // NSURLSession_Test 4 | // 5 | // Created by SuperWang on 16/3/4. 6 | // Copyright © 2016年 SuperWang. 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 例子集锦 2 | 3 | ##简介 4 | 一些技术的测试Demo和简单的注释 5 | 6 | ###包含的例子(会持续更新) 7 | - NSURLSession 8 | 9 | Core Foundation中NSURLConnection在2003年就随着Safari一同发布,诞生在十多年前,由于iOS设备硬件和软件升级比较快,原有的NSURLConenection网络请求的性能需要重构,2013年iOS7中给出了新的网络请求方案-NSURLSession。日常的iOS开发中,AFNetWorking算是最常见的网络请求库,AFNetworking 1.0建立在NSURLConnection的基础之上,AFNetworking 2.0使用NSURLConnection基础API,以及较新基于NSURLSession的API的选项,3.X版本删除了所有基于NSURLConnection API的所有支持,新的API完全基于NSURLSession 10 | 11 | **数据通信用到类和协议可以参考下图** 12 | ![icon](http://upload-images.jianshu.io/upload_images/1048365-89477eebc0df677f.png?imageMogr2/auto-orient/strip%7CimageView2/2) 13 | 14 | 上图中的URL Loading的NSURLSession,NSURLSession需要用到三种异步的任务NSURLSessionDataTask,NSURLSessionDownloadTask和NSURLSessionUploadTask。 15 | NSURLSessionDataTask主要用于获取JSON/XML数据; 16 | NSURLSessionDownloadTask的作用是下载文件; 17 | NSURLSessionUploadTask上传文件会用到; 18 | 19 | - 算法排序 20 | 21 | ``` 22 | 冒泡排序 23 | 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。 24 | 这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端,故名。 25 | 算法原理 26 | 冒泡排序算法的运作如下:(从后往前) 27 | 比较相邻的元素。如果第一个比第二个大,就交换他们两个。 28 | 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。 29 | 针对所有的元素重复以上的步骤,除了最后一个。 30 | 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 31 | ``` 32 | 33 | ``` 34 | 选择排序 35 | 选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。 36 | ``` 37 | 38 | ``` 39 | 插入排序 40 | 插入排序的基本思想是:每步将一个待排序的纪录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。 41 | 每次处理就是将无序数列的第一个元素与有序数列的元素从后往前逐个进行比较,找出插入位置,将该元素插入到有序数列的合适位置中。 42 | ``` 43 | ``` 44 | 快速排序 45 | 快速排序(Quicksort)是对冒泡排序的一种改进。 46 | 快速排序由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。 47 | ``` 48 | 49 | - CollectionView 50 | 51 | 官方对于自定义layout的例子 52 | 53 | github上较好的一个例子 54 | 55 | 56 | 57 | 58 | ###PDF签名的例子 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /排序算法图形/STBasic.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/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 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "40x40", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "60x60", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /排序算法图形/STBasic/Images.xcassets/LaunchImage.launchimage/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "orientation" : "portrait", 5 | "idiom" : "iphone", 6 | "extent" : "full-screen", 7 | "minimum-system-version" : "7.0", 8 | "scale" : "2x" 9 | }, 10 | { 11 | "orientation" : "portrait", 12 | "idiom" : "iphone", 13 | "subtype" : "retina4", 14 | "extent" : "full-screen", 15 | "minimum-system-version" : "7.0", 16 | "scale" : "2x" 17 | } 18 | ], 19 | "info" : { 20 | "version" : 1, 21 | "author" : "xcode" 22 | } 23 | } -------------------------------------------------------------------------------- /排序算法图形/STBasic/STAppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // STAppDelegate.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-1. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STAppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STAppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // STAppDelegate.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-1. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STAppDelegate.h" 10 | 11 | @implementation STAppDelegate 12 | 13 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 14 | { 15 | // Override point for customization after application launch. 16 | 17 | /* 18 | i=0 j:0-10 19 | i=1 j:0-9 20 | i=2 j:0-8 21 | ... 22 | i=10 j:0 23 | */ 24 | 25 | //冒泡排序 26 | int a [12] = {20,6,2,8,9,12,2,4,3,4,7,11}; 27 | BOOL exchanged = YES; 28 | //i:整体遍历的次数 29 | //j:当前遍历的位置 30 | for (int i = 0 ; i<11&&exchanged; i++) { 31 | exchanged = NO; 32 | for (int j = 0 ; j<(11-i); j++) 33 | { 34 | NSLog(@"---%d---%d",i,j); 35 | int b = a[j]; 36 | int c = a[j+1]; 37 | if (b>c) { 38 | a[j] = c; 39 | a[j+1] = b; 40 | exchanged = YES; 41 | 42 | } 43 | } 44 | 45 | } 46 | for (int i = 0 ; i< 12;i++ ) { 47 | NSLog(@"for is end :%d",a[i]); 48 | } 49 | 50 | 51 | //选择排序 52 | int b[8] = {10,2,5,34,6,7,8,1}; 53 | 54 | for (int i = 0; i<8; i++) 55 | { 56 | for (int n = i ; n<8; n++) 57 | { 58 | int bi = b[i]; 59 | int bn = b[n]; 60 | if (bi>bn) 61 | { 62 | b[i] = bn; 63 | b[n] = bi; 64 | 65 | } 66 | } 67 | } 68 | 69 | for (int i = 0 ; i< 8;i++ ) { 70 | NSLog(@"for is end :%d",b[i]); 71 | } 72 | 73 | 74 | //插入排序 75 | int c[8] = {10,2,5,34,6,7,8,1}; 76 | for (int i = 1; i<8; i++) 77 | { 78 | int ci = c[i]; 79 | 80 | int cj; 81 | 82 | int j = i; 83 | 84 | while (j>0&&(cj = c[j-1])>ci) 85 | { 86 | c[j] = cj; 87 | j--; 88 | } 89 | c[j] = ci; 90 | 91 | } 92 | return YES; 93 | } 94 | 95 | - (void)applicationWillResignActive:(UIApplication *)application 96 | { 97 | // 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. 98 | // 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. 99 | } 100 | 101 | - (void)applicationDidEnterBackground:(UIApplication *)application 102 | { 103 | // 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. 104 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 105 | } 106 | 107 | - (void)applicationWillEnterForeground:(UIApplication *)application 108 | { 109 | // 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. 110 | } 111 | 112 | - (void)applicationDidBecomeActive:(UIApplication *)application 113 | { 114 | // 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. 115 | } 116 | 117 | - (void)applicationWillTerminate:(UIApplication *)application 118 | { 119 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 120 | } 121 | 122 | @end 123 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STBasic-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.zhiyou.$(PRODUCT_NAME:rfc1034identifier) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STBasic-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header 3 | // 4 | // The contents of this file are implicitly included at the beginning of every source file. 5 | // 6 | 7 | #import 8 | 9 | #ifndef __IPHONE_5_0 10 | #warning "This project uses features only available in iOS SDK 5.0 and later." 11 | #endif 12 | 13 | #ifdef __OBJC__ 14 | #import 15 | #import 16 | 17 | typedef enum STAlgorithmType { 18 | 19 | STAlgorithmTypeBubbleSort = 1, 20 | STAlgorithmTypeSelectSort = 2, 21 | STAlgorithmTypeInsertSort = 3, 22 | STAlgorithmTypeQuickSort = 5, 23 | 24 | STAlgorithmTypeHanoi = 11, 25 | 26 | } STAlgorithmType; 27 | #endif 28 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STCodeViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // STCodeViewController.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-4. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STCodeViewController : UIViewController 12 | 13 | @property (nonatomic, assign) STAlgorithmType algorithmType; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STCodeViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // STCodeViewController.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-4. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STCodeViewController.h" 10 | 11 | @interface STCodeViewController () 12 | 13 | @property (nonatomic, strong) UITextView * textView; 14 | 15 | @end 16 | 17 | @implementation STCodeViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | // Do any additional setup after loading the view. 22 | self.navigationItem.title = @"源码"; 23 | 24 | self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 80, 320, 400)]; 25 | self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 26 | self.textView.font = [UIFont systemFontOfSize:14]; 27 | self.textView.editable = NO; 28 | [self.view addSubview:self.textView]; 29 | 30 | NSString * insert = @"void insert(int[] a) {\n\tfor (int i = 1; i < a.length; i ++) {\n\t\tint ai = a[i], aj;\n\t\tint j = i;\n\t\twhile (j > 0 && (aj = a[j - 1]) > ai) {\n\t\t\ta[j] = aj;\n\t\t\tj --;\n\t\t}\n\t\ta[j] = ai;\n\t}\n}\n"; 31 | 32 | NSString * bubble = @"void bubble(int[] a) {\n\tBOOL exchanged = YES;\n\tfor (int i = 0; i < a.length - 1 && exchanged; i ++) {\n\t\texchanged = NO;\n\t\tfor (int j = 0; j < (a.length - 1 - i); j ++) {\n\t\t\tint aj = a[j];\n\t\t\tint aj1 = a[j + 1];\n\t\t\tif (aj > aj1) {\n\t\t\t\texchanged = YES;\n\t\t\t\ta[j + 1] = aj;\n\t\t\t\ta[j] = aj1;\n\t\t\t}\n\t\t}\n\t}\n}\n"; 33 | NSString * select = @"void select(int[] a) {\n\tfor(int i = 0; i < a.length;i ++) {\n\t\tfor(int j = i; j < a.length; j ++) {\n\t\tint ai = a[i];\n\t\tint aj = a[j];\n\t\tif(a[i] > a[j]) {\n\t\t\ta[i] = aj\n\t\t\ta[j] = ai;\n\t\t}\n\t}\n}"; 34 | 35 | NSString * quick = @"void quick(int[] a, int left, int right){\tif (left < right) {\n\t\tint key = a[left];\n\t\tint low = left;\n\t\tint high = right;\n\t\twhile (low < high) {\n\t\t\twhile (low < high && a[high] >= key) {\n\t\t\t\thigh --;\n\t\t\t}\n\t\t\ta[low] = a[high];\n\t\t\twhile (low < high && a[low] <= key) {\n\t\t\t\tlow ++;\n\t\t\t}\n\t\t\ta[high] = a[low];\n\t\t}\n\t\ta[low] = key;\n\t\tquick(a, left, low - 1);\n\t\tquick(a, low + 1, right);\n\t}\n}\n"; 36 | 37 | NSString * hanoi = @"void hanoi(int i , char A , char B , char C) {\n\tif(i == 1) {\n\t\tmove(i , A , C);\n\t} else {\t\n\t\thanoi(i - 1 , A , C , B);\n\t\tmove(i , A , C);\n\t\thanoi(i - 1 , B , A , C);\n\t}\n}\n\nvoid move(int i , char x , char y) {\n\tprintf(\"Move disk i from x to y\");\n}"; 38 | 39 | switch (self.algorithmType) { 40 | case STAlgorithmTypeInsertSort: 41 | self.textView.text = insert; 42 | break; 43 | case STAlgorithmTypeBubbleSort: 44 | self.textView.text = bubble; 45 | break; 46 | case STAlgorithmTypeSelectSort: 47 | self.textView.text = select; 48 | break; 49 | case STAlgorithmTypeQuickSort: 50 | self.textView.text = quick; 51 | break; 52 | case STAlgorithmTypeHanoi: 53 | self.textView.text = hanoi; 54 | break; 55 | 56 | default: 57 | break; 58 | } 59 | } 60 | 61 | - (void)didReceiveMemoryWarning { 62 | [super didReceiveMemoryWarning]; 63 | // Dispose of any resources that can be recreated. 64 | } 65 | 66 | @end 67 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STHanoiOperation.h: -------------------------------------------------------------------------------- 1 | // 2 | // STHanoiOperation.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-3. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | @class STHanoiView; 11 | @interface STHanoiOperation : NSOperation 12 | 13 | @property (nonatomic, assign) NSInteger index; 14 | @property (nonatomic, assign) NSInteger toIndex; 15 | @property (nonatomic, weak) STHanoiView * hanoiView; 16 | @end 17 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STHanoiOperation.m: -------------------------------------------------------------------------------- 1 | // 2 | // STHanoiOperation.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-3. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STHanoiOperation.h" 10 | #import "STHanoiView.h" 11 | 12 | @interface STHanoiOperation () 13 | 14 | @property (nonatomic, assign) NSTimeInterval duration; 15 | 16 | @property (atomic, assign) BOOL complete; 17 | 18 | @end 19 | 20 | @implementation STHanoiOperation 21 | 22 | - (instancetype) init { 23 | self = [super init]; 24 | if (self) { 25 | self.duration = [[[NSUserDefaults standardUserDefaults] valueForKey:@"STMoveAnimationDuration"] doubleValue]; 26 | } 27 | return self; 28 | } 29 | 30 | - (void) main { 31 | 32 | void (^completion)(BOOL finished) = ^(BOOL finished){ 33 | self.complete = YES; 34 | }; 35 | dispatch_async(dispatch_get_main_queue(), ^{ 36 | [self.hanoiView moveDiskAtIndex:_index toIndex:_toIndex duration:_duration completion:completion]; 37 | }); 38 | while (!self.complete) { 39 | [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]]; 40 | } 41 | } 42 | 43 | @end 44 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STHanoiView.h: -------------------------------------------------------------------------------- 1 | // 2 | // STHanoiView.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-3. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STHanoiView : UIView 12 | 13 | @property (nonatomic, assign) NSInteger numberOfHanois; 14 | 15 | - (void) reloadHanoiView; 16 | 17 | - (void) moveDiskAtIndex:(NSInteger) index 18 | toIndex:(NSInteger) toIndex 19 | duration:(NSTimeInterval) duration 20 | completion:(void(^)(BOOL finished)) completion; 21 | @end 22 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STHanoiView.m: -------------------------------------------------------------------------------- 1 | // 2 | // STHanoiView.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-3. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STHanoiView.h" 10 | 11 | @interface STHanoiView () 12 | 13 | @property (nonatomic, strong) UIView * columnView1; 14 | @property (nonatomic, strong) UIView * columnView2; 15 | @property (nonatomic, strong) UIView * columnView3; 16 | 17 | @property (nonatomic, assign) CGFloat baseWidth; 18 | @property (nonatomic, assign) CGFloat baseHeight; 19 | 20 | @property (nonatomic, strong) NSMutableArray * hanoiViewArray; 21 | 22 | @property (nonatomic, strong) NSMutableArray * columnHanoi1; 23 | @property (nonatomic, strong) NSMutableArray * columnHanoi2; 24 | @property (nonatomic, strong) NSMutableArray * columnHanoi3; 25 | 26 | @property (nonatomic, assign) CGFloat hanoiHeight; 27 | @property (nonatomic, assign) CGFloat verticalMargin; 28 | @end 29 | 30 | const NSInteger kSTDiskTagOffset = 100; 31 | 32 | @implementation STHanoiView 33 | 34 | - (id)initWithFrame:(CGRect)frame { 35 | self = [super initWithFrame:frame]; 36 | if (self) { 37 | // Initialization code 38 | self.baseWidth = CGRectGetWidth(frame) / 3; 39 | self.baseHeight = CGRectGetHeight(frame) * 2 / 3; 40 | 41 | self.columnView1 = [[UIView alloc] initWithFrame:CGRectMake( self.baseWidth * 0.5 - 3, CGRectGetHeight(frame) - self.baseHeight, 6, self.baseHeight)]; 42 | self.columnView1.backgroundColor = [UIColor blackColor]; 43 | [self addSubview:self.columnView1]; 44 | 45 | self.columnView2 = [[UIView alloc] initWithFrame:CGRectMake( self.baseWidth * 1.5 - 3, CGRectGetHeight(frame) - self.baseHeight, 6, self.baseHeight)]; 46 | self.columnView2.backgroundColor = [UIColor blackColor]; 47 | [self addSubview:self.columnView2]; 48 | 49 | self.columnView3 = [[UIView alloc] initWithFrame:CGRectMake( self.baseWidth * 2.5 - 3, CGRectGetHeight(frame) - self.baseHeight, 6, self.baseHeight)]; 50 | self.columnView3.backgroundColor = [UIColor blackColor]; 51 | [self addSubview:self.columnView3]; 52 | 53 | self.hanoiViewArray = [NSMutableArray arrayWithCapacity:3]; 54 | 55 | self.columnHanoi1 = [NSMutableArray arrayWithCapacity:3]; 56 | self.columnHanoi2 = [NSMutableArray arrayWithCapacity:3]; 57 | self.columnHanoi3 = [NSMutableArray arrayWithCapacity:3]; 58 | } 59 | return self; 60 | } 61 | 62 | - (void) reloadHanoiView { 63 | [self.hanoiViewArray enumerateObjectsUsingBlock:^(UIView * subview, NSUInteger idx, BOOL *stop) { 64 | [subview removeFromSuperview]; 65 | }]; 66 | [self.hanoiViewArray removeAllObjects]; 67 | [self.columnHanoi1 removeAllObjects]; 68 | [self.columnHanoi2 removeAllObjects]; 69 | [self.columnHanoi3 removeAllObjects]; 70 | 71 | CGFloat height = MIN(self.baseHeight / _numberOfHanois, 15); 72 | self.hanoiHeight = height; 73 | CGFloat increment = MIN(30, (self.baseWidth - 20) / _numberOfHanois); 74 | for (int i = 0; i < _numberOfHanois; i ++) { 75 | CGFloat width = self.baseWidth - i * increment; 76 | CGFloat left = (self.baseWidth - width) / 2; 77 | CGFloat top = CGRectGetHeight(self.bounds) - (i + 1) * height + 5; 78 | UIView * hanoi = [[UIView alloc] initWithFrame:CGRectMake(left, top, width, height - 5)]; 79 | hanoi.backgroundColor = [UIColor orangeColor]; 80 | [self addSubview:hanoi]; 81 | 82 | [self.hanoiViewArray addObject:hanoi]; 83 | [self.columnHanoi1 addObject:hanoi]; 84 | } 85 | } 86 | 87 | - (void) moveDiskAtIndex:(NSInteger) index 88 | toIndex:(NSInteger) toIndex 89 | duration:(NSTimeInterval) duration 90 | completion:(void(^)(BOOL finished)) completion { 91 | NSMutableArray * array1, * array2 = nil; 92 | switch (index) { 93 | case 0: 94 | array1 = self.columnHanoi1; 95 | break; 96 | case 1: 97 | array1 = self.columnHanoi2; 98 | break; 99 | case 2: 100 | array1 = self.columnHanoi3; 101 | break; 102 | default: 103 | break; 104 | } 105 | switch (toIndex) { 106 | case 0: 107 | array2 = self.columnHanoi1; 108 | break; 109 | case 1: 110 | array2 = self.columnHanoi2; 111 | break; 112 | case 2: 113 | array2 = self.columnHanoi3; 114 | break; 115 | default: 116 | break; 117 | } 118 | UIView * view = [array1 lastObject]; 119 | [array1 removeObject:view]; 120 | CGPoint center = [self centerForHanoiAtColumn:toIndex]; 121 | [array2 addObject:view]; 122 | 123 | [UIView animateWithDuration:duration animations:^{ 124 | view.center = center; 125 | } completion:^(BOOL finished) { 126 | view.center = center; 127 | completion(finished); 128 | }]; 129 | } 130 | 131 | - (void) setNumberOfHanois:(NSInteger)numberOfHanois { 132 | _numberOfHanois = numberOfHanois; 133 | [self reloadHanoiView]; 134 | } 135 | 136 | - (CGPoint) centerForHanoiAtColumn:(NSInteger) column { 137 | NSArray * array = nil; 138 | switch (column) { 139 | case 0: 140 | array = self.columnHanoi1; 141 | break; 142 | case 1: 143 | array = self.columnHanoi2; 144 | break; 145 | case 2: 146 | array = self.columnHanoi3; 147 | break; 148 | default: 149 | break; 150 | } 151 | NSInteger count = array.count; 152 | CGFloat centerX = self.baseWidth * (column + 0.5); 153 | CGFloat centerY = CGRectGetHeight(self.bounds) - count * self.hanoiHeight - (self.hanoiHeight + 5) / 2 + 5; 154 | return CGPointMake(centerX, centerY); 155 | } 156 | @end 157 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STHanoiViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // STHanoiViewController.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-3. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STHanoiViewController : UIViewController 12 | 13 | @property (nonatomic, assign) NSInteger numberOfHanois; 14 | 15 | @end 16 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STHanoiViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // STHanoiViewController.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-3. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STHanoiViewController.h" 10 | #import "STHanoiView.h" 11 | #import "STHanoiOperation.h" 12 | 13 | #import "STCodeViewController.h" 14 | 15 | 16 | @interface STHanoiViewController () 17 | 18 | @property (nonatomic, strong) STHanoiView * hanoiView; 19 | 20 | @property (nonatomic, strong) NSOperationQueue * animationQueue; 21 | @end 22 | 23 | @implementation STHanoiViewController 24 | 25 | - (instancetype) initWithCoder:(NSCoder *)aDecoder { 26 | self = [super initWithCoder:aDecoder]; 27 | if (self) { 28 | self.animationQueue = [[NSOperationQueue alloc] init]; 29 | self.animationQueue.maxConcurrentOperationCount = 1; 30 | 31 | self.numberOfHanois = 4; 32 | } 33 | return self; 34 | } 35 | 36 | - (void)viewDidLoad { 37 | [super viewDidLoad]; 38 | // Do any additional setup after loading the view. 39 | self.navigationItem.title = @"汉诺塔"; 40 | 41 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"源码" style:UIBarButtonItemStylePlain target:self action:@selector(viewSourceCode:)]; 42 | 43 | self.hanoiView = [[STHanoiView alloc] initWithFrame:CGRectMake(0, 100, 320, 300)]; 44 | self.hanoiView.numberOfHanois = self.numberOfHanois; 45 | [self.view addSubview:self.hanoiView]; 46 | } 47 | 48 | - (void)didReceiveMemoryWarning { 49 | [super didReceiveMemoryWarning]; 50 | // Dispose of any resources that can be recreated. 51 | } 52 | 53 | - (void) viewDidAppear:(BOOL)animated { 54 | [super viewDidAppear:animated]; 55 | [self.hanoiView reloadHanoiView]; 56 | [self moveHanoiWithNumber:self.numberOfHanois - 1 position0:0 position1:1 position2:2]; 57 | } 58 | 59 | - (void) viewDidDisappear:(BOOL)animated { 60 | [super viewDidDisappear:animated]; 61 | [self.animationQueue cancelAllOperations]; 62 | } 63 | 64 | - (void) moveHanoiWithNumber:(NSInteger) number 65 | position0:(NSInteger) position0 66 | position1:(NSInteger) position1 67 | position2:(NSInteger) position2 { 68 | if (number == 0) { 69 | STHanoiOperation * operation1 = [[STHanoiOperation alloc] init]; 70 | operation1.index = position0; 71 | operation1.toIndex = position2; 72 | operation1.hanoiView = self.hanoiView; 73 | [self.animationQueue addOperation:operation1]; 74 | } else { 75 | [self moveHanoiWithNumber:number - 1 position0:position0 position1:position2 position2:position1]; 76 | 77 | STHanoiOperation * operation2 = [[STHanoiOperation alloc] init]; 78 | operation2.index = position0; 79 | operation2.toIndex = position2; 80 | operation2.hanoiView = self.hanoiView; 81 | [self.animationQueue addOperation:operation2]; 82 | 83 | [self moveHanoiWithNumber:number - 1 position0:position1 position1:position0 position2:position2]; 84 | } 85 | } 86 | 87 | - (void) viewSourceCode:(id) sender { 88 | STCodeViewController * viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"STCodeViewController"]; 89 | viewController.algorithmType = STAlgorithmTypeHanoi; 90 | [self.navigationController pushViewController:viewController animated:YES]; 91 | } 92 | 93 | @end 94 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STRootViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // STRootViewController.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-2. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STRootViewController : UITableViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STRootViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // STRootViewController.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-2. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STRootViewController.h" 10 | #import "STSortViewController.h" 11 | #import "STHanoiViewController.h" 12 | 13 | @interface STRootViewController () 14 | 15 | @property (nonatomic, strong) NSArray * dataSource; 16 | @property (nonatomic, strong) NSArray * sortArray; 17 | @property (nonatomic, strong) UILabel * numberOfHanoiDiskLabel; 18 | @property (nonatomic, assign) NSInteger numberOfDisks; 19 | 20 | @end 21 | 22 | @implementation STRootViewController 23 | 24 | - (instancetype) initWithCoder:(NSCoder *)aDecoder { 25 | self = [super initWithCoder:aDecoder]; 26 | if (self) { 27 | NSArray * array = @[@(20),@(6),@(2),@(8),@(9),@(12), @(2), @(4), @(3), @(4), @(7), @(11)]; 28 | self.sortArray = array; 29 | 30 | NSMutableArray * dataSource = [NSMutableArray arrayWithCapacity:2]; 31 | 32 | NSMutableArray * section0 = [NSMutableArray arrayWithCapacity:2]; 33 | [section0 addObject:@{@"type":@(STArraySortTypeBubbleSort), @"title":@"冒泡排序"}]; 34 | [section0 addObject:@{@"type":@(STArraySortTypeSelectSort), @"title":@"选择排序"}]; 35 | [section0 addObject:@{@"type":@(STArraySortTypeInsertSort), @"title":@"插入排序"}]; 36 | [section0 addObject:@{@"type":@(STArraySortTypeQuickSort), @"title":@"快速排序"}]; 37 | NSString * section0Title = [NSString stringWithFormat:@"几种常用的排序算法, 排序数据源\n%@", [self.sortArray componentsJoinedByString:@","]]; 38 | NSDictionary * section0Dict = @{@"sectionHeaderTitle":section0Title, @"section":section0}; 39 | [dataSource addObject:section0Dict]; 40 | 41 | NSMutableArray * section1 = [NSMutableArray arrayWithCapacity:2]; 42 | [section1 addObject:@{@"type":@(STArraySortTypeBubbleSort), @"title":@"汉诺塔算法"}]; 43 | NSString * section1Title = [NSString stringWithFormat:@"经典的几种递归算法\n"]; 44 | NSDictionary * section1Dict = @{@"sectionHeaderTitle":section1Title, @"section":section1}; 45 | [dataSource addObject:section1Dict]; 46 | 47 | self.dataSource = dataSource; 48 | } 49 | return self; 50 | } 51 | 52 | - (id)initWithStyle:(UITableViewStyle)style { 53 | self = [super initWithStyle:style]; 54 | if (self) { 55 | // Custom initialization 56 | } 57 | return self; 58 | } 59 | 60 | - (void)viewDidLoad { 61 | [super viewDidLoad]; 62 | 63 | self.navigationItem.title = @"算法分析"; 64 | 65 | UIView * tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)]; 66 | 67 | UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 80, 30)]; 68 | label.textAlignment = NSTextAlignmentLeft; 69 | label.text = @"速度比例"; 70 | label.textColor = [UIColor blackColor]; 71 | [tableHeaderView addSubview:label]; 72 | 73 | id speed = [[NSUserDefaults standardUserDefaults] valueForKey:@"STMoveAnimationDuration"]; 74 | if (!speed) { 75 | speed = @(0.5); 76 | [[NSUserDefaults standardUserDefaults] setValue:speed forKey:@"STMoveAnimationDuration"]; 77 | [[NSUserDefaults standardUserDefaults] synchronize]; 78 | } 79 | UISlider * slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 10, 200, 30)]; 80 | [tableHeaderView addSubview:slider]; 81 | slider.minimumValue = 0.2; 82 | slider.maximumValue = 1.2; 83 | slider.value = [speed floatValue]; 84 | slider.continuous = NO; 85 | [slider addTarget:self action:@selector(speedChanged:) forControlEvents:UIControlEventValueChanged]; 86 | 87 | UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 80, 30)]; 88 | label2.textAlignment = NSTextAlignmentLeft; 89 | label2.text = @"汉诺塔数"; 90 | label2.textColor = [UIColor blackColor]; 91 | [tableHeaderView addSubview:label2]; 92 | 93 | id disks = [[NSUserDefaults standardUserDefaults] valueForKey:@"STNumberOfHanoiDisks"]; 94 | if (!disks) { 95 | disks = @(4); 96 | } 97 | 98 | self.numberOfHanoiDiskLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 50, 30)]; 99 | self.numberOfHanoiDiskLabel.textAlignment = NSTextAlignmentLeft; 100 | [self updateNumberOfDisks:[disks intValue]]; 101 | self.numberOfHanoiDiskLabel.textColor = [UIColor blackColor]; 102 | [tableHeaderView addSubview:self.numberOfHanoiDiskLabel]; 103 | 104 | UIStepper * stepper = [[UIStepper alloc] initWithFrame:CGRectMake(150, 50, 200, 30)]; 105 | stepper.minimumValue = 3; 106 | stepper.maximumValue = 10; 107 | stepper.value = [disks intValue]; 108 | [stepper addTarget:self action:@selector(numberOfDiskChanged:) forControlEvents:UIControlEventValueChanged]; 109 | [tableHeaderView addSubview:stepper]; 110 | self.tableView.tableHeaderView = tableHeaderView; 111 | } 112 | 113 | - (void)didReceiveMemoryWarning { 114 | [super didReceiveMemoryWarning]; 115 | // Dispose of any resources that can be recreated. 116 | } 117 | 118 | - (void) speedChanged:(UISlider *) slider { 119 | NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults]; 120 | [userDefault setValue:@(slider.value) forKey:@"STMoveAnimationDuration"]; 121 | [userDefault synchronize]; 122 | } 123 | 124 | - (void) updateNumberOfDisks:(NSInteger) numberOfDisks { 125 | self.numberOfDisks = numberOfDisks; 126 | self.numberOfHanoiDiskLabel.text = [NSString stringWithFormat:@"%d", numberOfDisks]; 127 | [[NSUserDefaults standardUserDefaults] setValue:@(numberOfDisks) forKey:@"STNumberOfHanoiDisks"]; 128 | [[NSUserDefaults standardUserDefaults] synchronize]; 129 | } 130 | 131 | - (void) numberOfDiskChanged:(UIStepper *) sender { 132 | [self updateNumberOfDisks:sender.value]; 133 | } 134 | 135 | #pragma mark - Table view data source 136 | 137 | - (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 138 | NSDictionary * sectionInfo = [self.dataSource objectAtIndex:section]; 139 | return [sectionInfo valueForKey:@"sectionHeaderTitle"]; 140 | } 141 | 142 | 143 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 144 | // Return the number of sections. 145 | return self.dataSource.count; 146 | } 147 | 148 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 149 | // Return the number of rows in the section. 150 | NSDictionary * sectionInfo = [self.dataSource objectAtIndex:section]; 151 | return [[sectionInfo valueForKey:@"section"] count]; 152 | } 153 | 154 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 155 | UITableViewCell * tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"]; 156 | // Configure the cell... 157 | NSDictionary * sectionInfo = [self.dataSource objectAtIndex:indexPath.section]; 158 | NSArray * rowInfo = [[sectionInfo valueForKey:@"section"] objectAtIndex:indexPath.row]; 159 | tableViewCell.textLabel.text = [rowInfo valueForKey:@"title"]; 160 | return tableViewCell; 161 | } 162 | 163 | 164 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 165 | 166 | NSDictionary * sectionInfo = [self.dataSource objectAtIndex:indexPath.section]; 167 | NSArray * rowInfo = [[sectionInfo valueForKey:@"section"] objectAtIndex:indexPath.row]; 168 | if (indexPath.section == 0) { 169 | STSortViewController * viewController = (STSortViewController *) [self.storyboard instantiateViewControllerWithIdentifier:@"STSortViewController"]; 170 | viewController.sortArray = self.sortArray; 171 | viewController.arraySortType = [[rowInfo valueForKey:@"type"] intValue]; 172 | [self.navigationController pushViewController:viewController animated:YES]; 173 | } else if (indexPath.section == 1) { 174 | if (indexPath.row == 0) { 175 | STHanoiViewController * viewController = (STHanoiViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"STHanoiViewController"]; 176 | viewController.numberOfHanois = self.numberOfDisks; 177 | [self.navigationController pushViewController:viewController animated:YES]; 178 | } 179 | } 180 | } 181 | /* 182 | #pragma mark - Navigation 183 | 184 | // In a story board-based application, you will often want to do a little preparation before navigation 185 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 186 | { 187 | // Get the new view controller using [segue destinationViewController]. 188 | // Pass the selected object to the new view controller. 189 | } 190 | 191 | */ 192 | 193 | @end 194 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STSortOperation.h: -------------------------------------------------------------------------------- 1 | // 2 | // STSortOperation.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-2. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef enum STSortOperationType { 12 | STSortOperationTypeMoveBaseline1 = 1, 13 | STSortOperationTypeMoveBaseline2 = 2, 14 | STSortOperationTypeMoveElement = 3, 15 | STSortOperationTypeCacheUpElement = 4, 16 | STSortOperationTypeCacheDownElement = 5, 17 | STSortOperationTypeExchangeElement = 6, 18 | } STSortOperationType; 19 | 20 | @class STSortView; 21 | @interface STSortOperation : NSOperation 22 | 23 | @property (nonatomic, assign) NSTimeInterval duration; 24 | @property (nonatomic, assign) NSInteger index1; 25 | @property (nonatomic, assign) NSInteger index2; 26 | @property (nonatomic, weak) STSortView * sortView; 27 | @property (nonatomic, assign) STSortOperationType operationType; 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STSortOperation.m: -------------------------------------------------------------------------------- 1 | // 2 | // STSortOperation.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-2. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STSortOperation.h" 10 | #import "STSortView.h" 11 | 12 | @interface STSortOperation() 13 | 14 | @property (atomic, assign) BOOL complete; 15 | @end 16 | 17 | @implementation STSortOperation 18 | 19 | - (instancetype) init { 20 | self = [super init]; 21 | if (self) { 22 | id duration = [[NSUserDefaults standardUserDefaults] valueForKey:@"STMoveAnimationDuration"]; 23 | self.duration = [duration doubleValue]; 24 | } 25 | return self; 26 | } 27 | 28 | - (void) main { 29 | void (^completion)(BOOL finished) = ^(BOOL finished) { 30 | self.complete = YES; 31 | }; 32 | dispatch_async(dispatch_get_main_queue(), ^{ 33 | switch (_operationType) { 34 | case STSortOperationTypeMoveBaseline1: 35 | [self.sortView moveBaseline1ToIndex:_index1 duration:self.duration completion:completion]; 36 | break; 37 | case STSortOperationTypeMoveBaseline2: 38 | [self.sortView moveBaseline2ToIndex:_index2 duration:self.duration completion:completion]; 39 | break; 40 | case STSortOperationTypeCacheUpElement: 41 | [self.sortView moveElementToCacheAtIndex:_index1 duration:self.duration completion:completion]; 42 | break; 43 | case STSortOperationTypeCacheDownElement: 44 | [self.sortView removeElementFromCacheWithDuration:self.duration completion:completion]; 45 | break; 46 | case STSortOperationTypeMoveElement: 47 | [self.sortView moveElementAtIndex:_index1 toIndex:_index2 duration:self.duration completion:completion]; 48 | break; 49 | case STSortOperationTypeExchangeElement: 50 | [self.sortView exchangeElementAtIndex:_index1 withElementAtIndex:_index2 duration:self.duration completion:completion]; 51 | break; 52 | default: 53 | break; 54 | } 55 | }); 56 | while (!_complete) { 57 | [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]]; 58 | } 59 | } 60 | 61 | @end -------------------------------------------------------------------------------- /排序算法图形/STBasic/STSortView.h: -------------------------------------------------------------------------------- 1 | // 2 | // STSortView.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-2. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STSortView : UIView 12 | 13 | /// default 5 14 | @property (nonatomic, assign) NSInteger factor; 15 | 16 | - (void) reloadSortDataSource:(NSArray *) dataSource; 17 | 18 | /// 基线表示参考线,用于表示 循环走到了哪个节点 19 | - (void) moveBaseline1ToIndex:(NSInteger) index 20 | duration:(NSTimeInterval) duration 21 | completion:(void(^)(BOOL finished)) completion; 22 | 23 | - (void) moveBaseline2ToIndex:(NSInteger) index 24 | duration:(NSTimeInterval) duration 25 | completion:(void(^)(BOOL finished)) completion; 26 | 27 | /// 将某个位置的元素移动到另一个位置 比如 array[toIndex] = array[index] 28 | - (void) moveElementAtIndex:(NSInteger) index 29 | toIndex:(NSInteger) toIndex 30 | duration:(NSTimeInterval) duration 31 | completion:(void (^)(BOOL finished)) completion; 32 | 33 | /// 将某一个位置的元素移动到缓冲区,比如 int temp = array[index] 34 | - (void) moveElementToCacheAtIndex:(NSInteger) index 35 | duration:(NSTimeInterval) duration 36 | completion:(void(^)(BOOL finished)) completion; 37 | 38 | - (void) removeElementFromCacheWithDuration:(NSTimeInterval) duration 39 | completion:(void(^)(BOOL finished)) completion; 40 | 41 | /// 交换其中两个元素的位置 比如 int temp = array[index1];array[index1] = array[index2];array[index2] = temp; 42 | - (void) exchangeElementAtIndex:(NSInteger) index1 43 | withElementAtIndex:(NSInteger) index2 44 | duration:(NSTimeInterval) duration 45 | completion:(void(^)(BOOL finished)) completion; 46 | @end 47 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STSortView.m: -------------------------------------------------------------------------------- 1 | // 2 | // STSortView.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-2. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STSortView.h" 10 | 11 | const NSInteger kSTSortElementTagOffset = 100; 12 | 13 | @interface STSortView () 14 | 15 | @property (nonatomic, strong) NSMutableArray * elementViewArray; 16 | 17 | @property (nonatomic, strong) UIView * baseline1; 18 | @property (nonatomic, strong) UIView * baseline2; 19 | 20 | @property (nonatomic, assign) CGFloat baseWidth; 21 | 22 | @property (nonatomic, weak) UIView * cacheView; 23 | 24 | - (UIView *) viewAtIndex:(NSInteger) index; 25 | - (CGFloat) frameXAtIndex:(NSInteger) index; 26 | 27 | @end 28 | 29 | @implementation STSortView 30 | 31 | - (id)initWithFrame:(CGRect)frame { 32 | self = [super initWithFrame:frame]; 33 | if (self) { 34 | // Initialization code 35 | self.factor = 5; 36 | self.elementViewArray = [NSMutableArray arrayWithCapacity:5]; 37 | 38 | self.baseline1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, CGRectGetHeight(frame))]; 39 | self.baseline1.backgroundColor = [UIColor blueColor]; 40 | [self addSubview:self.baseline1]; 41 | 42 | self.baseline2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, CGRectGetHeight(frame))]; 43 | self.baseline2.backgroundColor = [UIColor greenColor]; 44 | [self addSubview:self.baseline2]; 45 | } 46 | return self; 47 | } 48 | 49 | - (void) reloadSortDataSource:(NSArray *) dataSource { 50 | [self.elementViewArray enumerateObjectsUsingBlock:^(UIView * subView, NSUInteger idx, BOOL *stop) { 51 | [subView removeFromSuperview]; 52 | }]; 53 | [self.elementViewArray removeAllObjects]; 54 | 55 | int width = CGRectGetWidth(self.bounds) / dataSource.count; 56 | self.baseWidth = width; 57 | 58 | self.baseline1.frame = CGRectMake(0.5 * width, 0, 2, CGRectGetHeight(self.bounds)); 59 | self.baseline2.frame = CGRectMake(0.5 * width, 0, 2, CGRectGetHeight(self.bounds)); 60 | 61 | [dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 62 | CGFloat height = [obj intValue] * self.factor; 63 | CGRect frame = CGRectZero; 64 | frame.origin = CGPointMake((idx + 0.5) * width, CGRectGetHeight(self.bounds) - height); 65 | frame.size = CGSizeMake(width - 10, height); 66 | 67 | UIView * view = [[UIView alloc] initWithFrame:frame]; 68 | view.backgroundColor = [UIColor blackColor]; 69 | view.tag = idx + kSTSortElementTagOffset; 70 | [self addSubview:view]; 71 | 72 | [self.elementViewArray addObject:view]; 73 | }]; 74 | } 75 | 76 | /// 基线表示参考线,用于表示 循环走到了哪个节点 77 | - (void) moveBaseline1ToIndex:(NSInteger) index 78 | duration:(NSTimeInterval) duration 79 | completion:(void(^)(BOOL finished)) completion { 80 | CGFloat left = [self frameXAtIndex:index]; 81 | CGRect frame = self.baseline1.frame; 82 | frame.origin.x = left; 83 | [UIView animateWithDuration:duration animations:^{ 84 | self.baseline1.frame = frame; 85 | } completion:^(BOOL finished) { 86 | self.baseline1.frame = frame; 87 | if (completion) { 88 | completion(finished); 89 | } 90 | }]; 91 | } 92 | 93 | - (void) moveBaseline2ToIndex:(NSInteger) index 94 | duration:(NSTimeInterval) duration 95 | completion:(void(^)(BOOL finished)) completion { 96 | CGFloat left = [self frameXAtIndex:index]; 97 | CGRect frame = self.baseline2.frame; 98 | frame.origin.x = left; 99 | [UIView animateWithDuration:duration animations:^{ 100 | self.baseline2.frame = frame; 101 | } completion:^(BOOL finished) { 102 | self.baseline2.frame = frame; 103 | if (completion) { 104 | completion(finished); 105 | } 106 | }]; 107 | } 108 | 109 | /// 将某个位置的元素移动到另一个位置 比如 array[toIndex] = array[index] 110 | - (void) moveElementAtIndex:(NSInteger) index 111 | toIndex:(NSInteger) toIndex 112 | duration:(NSTimeInterval) duration 113 | completion:(void (^)(BOOL finished)) completion { 114 | 115 | UIView * subview = [self viewAtIndex:index]; 116 | CGFloat left = [self frameXAtIndex:toIndex]; 117 | CGRect frame = subview.frame; 118 | frame.origin.x = left; 119 | frame.origin.y = CGRectGetHeight(self.bounds) - CGRectGetHeight(subview.bounds); 120 | 121 | subview.tag = toIndex + kSTSortElementTagOffset; 122 | 123 | self.cacheView.tag = index + kSTSortElementTagOffset; 124 | 125 | [UIView animateWithDuration:duration animations:^{ 126 | subview.frame = frame; 127 | } completion:^(BOOL finished) { 128 | subview.frame = frame; 129 | if (completion) { 130 | completion(finished); 131 | } 132 | }]; 133 | } 134 | 135 | /// 将某一个位置的元素移动到缓冲区,比如 int temp = array[index] 136 | - (void) moveElementToCacheAtIndex:(NSInteger) index 137 | duration:(NSTimeInterval) duration 138 | completion:(void(^)(BOOL finished)) completion { 139 | UIView * subview = [self viewAtIndex:index]; 140 | CGRect frame = subview.frame; 141 | frame.origin.y = 0; 142 | 143 | [UIView animateWithDuration:duration animations:^{ 144 | subview.frame = frame; 145 | } completion:^(BOOL finished) { 146 | subview.frame = frame; 147 | if (completion) { 148 | completion(finished); 149 | } 150 | }]; 151 | self.cacheView = subview; 152 | } 153 | 154 | - (void) removeElementFromCacheWithDuration:(NSTimeInterval) duration 155 | completion:(void(^)(BOOL finished)) completion { 156 | CGRect frame = self.cacheView.frame; 157 | frame.origin.y = CGRectGetHeight(self.bounds) - CGRectGetHeight(self.cacheView.frame); 158 | frame.origin.x = [self frameXAtIndex:(self.cacheView.tag - kSTSortElementTagOffset)]; 159 | [UIView animateWithDuration:duration animations:^{ 160 | self.cacheView.frame = frame; 161 | } completion:^(BOOL finished) { 162 | self.cacheView.frame = frame; 163 | completion(finished); 164 | }]; 165 | self.cacheView = nil; 166 | 167 | } 168 | 169 | /// 交换其中两个元素的位置 比如 int temp = array[index1];array[index1] = array[index2];array[index2] = temp; 170 | - (void) exchangeElementAtIndex:(NSInteger) index1 171 | withElementAtIndex:(NSInteger) index2 172 | duration:(NSTimeInterval) duration 173 | completion:(void(^)(BOOL finished)) completion { 174 | 175 | UIView * subview1 = [self viewAtIndex:index1]; 176 | UIView * subview2 = [self viewAtIndex:index2]; 177 | 178 | CGRect frame1 = subview1.frame; 179 | CGRect frame2 = subview2.frame; 180 | 181 | frame1.origin.x = [self frameXAtIndex:index2]; 182 | frame2.origin.x = [self frameXAtIndex:index1]; 183 | 184 | subview1.tag = index2 + kSTSortElementTagOffset; 185 | subview2.tag = index1 + kSTSortElementTagOffset; 186 | 187 | [UIView animateWithDuration:duration animations:^{ 188 | subview1.frame = frame1; 189 | subview2.frame = frame2; 190 | } completion:^(BOOL finished) { 191 | subview1.frame = frame1; 192 | subview2.frame = frame2; 193 | if (completion) { 194 | completion(finished); 195 | } 196 | }]; 197 | } 198 | 199 | - (UIView *) viewAtIndex:(NSInteger) index { 200 | return [self viewWithTag:(index + kSTSortElementTagOffset)]; 201 | } 202 | 203 | - (CGFloat) frameXAtIndex:(NSInteger) index { 204 | return (index + 0.5) * self.baseWidth; 205 | } 206 | @end 207 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STSortViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // STSortViewController.h 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-1. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | typedef enum STArraySortType { 12 | STArraySortTypeBubbleSort = 1, 13 | STArraySortTypeSelectSort = 2, 14 | STArraySortTypeInsertSort = 3, 15 | STArraySortTypeQuickSort = 5, 16 | } STArraySortType; 17 | 18 | @interface STSortViewController : UIViewController 19 | 20 | @property (nonatomic, assign) STArraySortType arraySortType; 21 | @property (nonatomic, strong) NSArray * sortArray; 22 | 23 | @end 24 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/STSortViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // STSortViewController.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-1. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import "STSortViewController.h" 10 | 11 | #import "STSortOperation.h" 12 | #import "STSortView.h" 13 | 14 | #import "STCodeViewController.h" 15 | 16 | @interface STSortViewController () 17 | 18 | @property (nonatomic, strong) STSortView * sortView; 19 | 20 | @property (nonatomic, strong) NSOperationQueue * animationQueue; 21 | 22 | @property (nonatomic, strong) NSMutableArray * sortMutableArray; 23 | 24 | @end 25 | 26 | @implementation STSortViewController 27 | 28 | 29 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 30 | self.animationQueue.suspended = !self.animationQueue.suspended; 31 | } 32 | 33 | - (instancetype) initWithCoder:(NSCoder *)aDecoder { 34 | self = [super initWithCoder:aDecoder]; 35 | if (self) { 36 | self.animationQueue = [[NSOperationQueue alloc] init]; 37 | self.animationQueue.maxConcurrentOperationCount = 1; 38 | } 39 | return self; 40 | } 41 | 42 | - (void)viewDidLoad { 43 | [super viewDidLoad]; 44 | // Do any additional setup after loading the view, typically from a nib. 45 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"源码" style:UIBarButtonItemStylePlain target:self action:@selector(viewSourceCode:)]; 46 | 47 | 48 | STSortView * sortView = [[STSortView alloc] initWithFrame:CGRectMake(0, 100, 320, 300)]; 49 | [sortView reloadSortDataSource:self.sortArray]; 50 | [self.view addSubview:sortView]; 51 | self.sortView = sortView; 52 | // UITableView 53 | // NSIndexPath 54 | switch (self.arraySortType) { 55 | 56 | case STArraySortTypeBubbleSort: 57 | self.navigationItem.title = @"冒泡排序"; 58 | break; 59 | case STArraySortTypeSelectSort: 60 | self.navigationItem.title = @"选择排序"; 61 | break; 62 | case STArraySortTypeInsertSort: 63 | self.navigationItem.title = @"插入排序"; 64 | break; 65 | case STArraySortTypeQuickSort: 66 | self.navigationItem.title = @"快速排序"; 67 | break; 68 | default: 69 | break; 70 | } 71 | } 72 | 73 | - (void) viewDidAppear:(BOOL)animated { 74 | [super viewDidAppear:animated]; 75 | [self.sortView reloadSortDataSource:self.sortArray]; 76 | switch (self.arraySortType) { 77 | case STArraySortTypeBubbleSort: 78 | [self sortUsingBubbleTypeWithArray:self.sortArray]; 79 | break; 80 | case STArraySortTypeSelectSort: 81 | [self sortUsingSelectTypeWithArray:self.sortMutableArray]; 82 | break; 83 | case STArraySortTypeInsertSort: 84 | [self sortUsingInsertTypeWithArray:self.sortMutableArray]; 85 | break; 86 | case STArraySortTypeQuickSort: 87 | [self sortUsingQuickTypeWithArray:self.sortMutableArray leftOffset:0 rightOffset:self.sortArray.count - 1]; 88 | break; 89 | default: 90 | break; 91 | } 92 | } 93 | 94 | - (void) viewDidDisappear:(BOOL)animated { 95 | [super viewDidDisappear:animated]; 96 | [self.animationQueue cancelAllOperations]; 97 | } 98 | 99 | - (void)didReceiveMemoryWarning { 100 | [super didReceiveMemoryWarning]; 101 | // Dispose of any resources that can be recreated. 102 | } 103 | 104 | - (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 105 | duration:(NSTimeInterval)duration { 106 | 107 | } 108 | 109 | - (void) sortUsingInsertTypeWithArray:(NSMutableArray *) array { 110 | NSInteger count = array.count; 111 | for (int i = 1; i < count; i ++) { 112 | 113 | STSortOperation * operation1 = [[STSortOperation alloc] init]; 114 | operation1.operationType = STSortOperationTypeMoveBaseline1; 115 | operation1.index1 = i; 116 | operation1.sortView = self.sortView; 117 | [self.animationQueue addOperation:operation1]; 118 | 119 | id objI = [array objectAtIndex:i], objJ; 120 | int j = i; 121 | STSortOperation * operation2 = [[STSortOperation alloc] init]; 122 | operation2.operationType = STSortOperationTypeMoveBaseline2; 123 | operation2.index2 = j; 124 | operation2.sortView = self.sortView; 125 | [self.animationQueue addOperation:operation2]; 126 | 127 | 128 | STSortOperation * operation3 = [[STSortOperation alloc] init]; 129 | operation3.operationType = STSortOperationTypeCacheUpElement; 130 | operation3.index1 = j; 131 | operation3.sortView = self.sortView; 132 | [self.animationQueue addOperation:operation3]; 133 | 134 | while (j > 0 && [(objJ = [array objectAtIndex:j - 1]) intValue] > [objI intValue]) { 135 | 136 | [array replaceObjectAtIndex:j withObject:objJ]; 137 | 138 | STSortOperation * operation4 = [[STSortOperation alloc] init]; 139 | operation4.operationType = STSortOperationTypeMoveElement; 140 | operation4.index1 = j - 1; 141 | operation4.index2 = j; 142 | operation4.sortView = self.sortView; 143 | [self.animationQueue addOperation:operation4]; 144 | 145 | j --; 146 | STSortOperation * operation5 = [[STSortOperation alloc] init]; 147 | operation5.operationType = STSortOperationTypeMoveBaseline2; 148 | operation5.index2 = j; 149 | operation5.sortView = self.sortView; 150 | [self.animationQueue addOperation:operation5]; 151 | } 152 | [array replaceObjectAtIndex:j withObject:objI]; 153 | STSortOperation * operation6 = [[STSortOperation alloc] init]; 154 | operation6.operationType = STSortOperationTypeCacheDownElement; 155 | operation6.sortView = self.sortView; 156 | [self.animationQueue addOperation:operation6]; 157 | } 158 | } 159 | 160 | - (void) sortUsingSelectTypeWithArray:(NSMutableArray *) array { 161 | NSInteger count = array.count; 162 | for (int i = 0; i < count; i ++) { 163 | STSortOperation * operation1 = [[STSortOperation alloc] init]; 164 | operation1.operationType = STSortOperationTypeMoveBaseline1; 165 | operation1.index1 = i; 166 | operation1.sortView = self.sortView; 167 | [self.animationQueue addOperation:operation1]; 168 | 169 | for (int j = i; j < count; j ++) { 170 | STSortOperation * operation2 = [[STSortOperation alloc] init]; 171 | operation2.operationType = STSortOperationTypeMoveBaseline2; 172 | operation2.index2 = j; 173 | operation2.sortView = self.sortView; 174 | [self.animationQueue addOperation:operation2]; 175 | id objI = [array objectAtIndex:i]; 176 | id objJ = [array objectAtIndex:j]; 177 | if ([objI intValue] > [objJ intValue]) { 178 | [array exchangeObjectAtIndex:i withObjectAtIndex:j]; 179 | STSortOperation * operation3 = [[STSortOperation alloc] init]; 180 | operation3.operationType = STSortOperationTypeExchangeElement; 181 | operation3.index1 = i; 182 | operation3.index2 = j; 183 | operation3.sortView = self.sortView; 184 | [self.animationQueue addOperation:operation3]; 185 | } 186 | } 187 | } 188 | } 189 | 190 | - (void) sortUsingQuickTypeWithArray:(NSMutableArray *) array 191 | leftOffset:(NSInteger) left 192 | rightOffset:(NSInteger) right { 193 | if (left < right) { 194 | 195 | id key = [array objectAtIndex:left]; 196 | 197 | STSortOperation * operation0 = [[STSortOperation alloc] init]; 198 | operation0.operationType = STSortOperationTypeCacheUpElement; 199 | operation0.index1 = left; 200 | operation0.sortView = self.sortView; 201 | [self.animationQueue addOperation:operation0]; 202 | 203 | 204 | NSInteger low = left; 205 | NSInteger high = right; 206 | 207 | STSortOperation * operation1 = [[STSortOperation alloc] init]; 208 | operation1.operationType = STSortOperationTypeMoveBaseline1; 209 | operation1.index1 = low; 210 | operation1.sortView = self.sortView; 211 | [self.animationQueue addOperation:operation1]; 212 | 213 | STSortOperation * operation2 = [[STSortOperation alloc] init]; 214 | operation2.operationType = STSortOperationTypeMoveBaseline2; 215 | operation2.index2 = high; 216 | operation2.sortView = self.sortView; 217 | [self.animationQueue addOperation:operation2]; 218 | 219 | while (low < high) { 220 | while (low < high && [[array objectAtIndex:high] intValue] >= [key intValue]) { 221 | high --; 222 | STSortOperation * operation3 = [[STSortOperation alloc] init]; 223 | operation3.operationType = STSortOperationTypeMoveBaseline2; 224 | operation3.index2 = high; 225 | operation3.sortView = self.sortView; 226 | [self.animationQueue addOperation:operation3]; 227 | } 228 | [array replaceObjectAtIndex:low withObject:[array objectAtIndex:high]]; 229 | 230 | STSortOperation * operation5 = [[STSortOperation alloc] init]; 231 | operation5.operationType = STSortOperationTypeMoveElement; 232 | operation5.index1 = high; 233 | operation5.index2 = low; 234 | operation5.sortView = self.sortView; 235 | [self.animationQueue addOperation:operation5]; 236 | 237 | 238 | while (low < high && [[array objectAtIndex:low] intValue] <= [key intValue]) { 239 | low ++; 240 | STSortOperation * operation6 = [[STSortOperation alloc] init]; 241 | operation6.operationType = STSortOperationTypeMoveBaseline1; 242 | operation6.index1 = low; 243 | operation6.sortView = self.sortView; 244 | [self.animationQueue addOperation:operation6]; 245 | } 246 | [array replaceObjectAtIndex:high withObject:[array objectAtIndex:low]]; 247 | 248 | STSortOperation * operation8 = [[STSortOperation alloc] init]; 249 | operation8.operationType = STSortOperationTypeMoveElement; 250 | operation8.index1 = low; 251 | operation8.index2 = high; 252 | operation8.sortView = self.sortView; 253 | [self.animationQueue addOperation:operation8]; 254 | } 255 | [array replaceObjectAtIndex:low withObject:key]; 256 | STSortOperation * operation9 = [[STSortOperation alloc] init]; 257 | operation9.operationType = STSortOperationTypeCacheDownElement; 258 | operation9.index1 = left; 259 | operation9.index2 = low; 260 | operation9.sortView = self.sortView; 261 | [self.animationQueue addOperation:operation9]; 262 | 263 | [self sortUsingQuickTypeWithArray:array leftOffset:left rightOffset:low - 1]; 264 | [self sortUsingQuickTypeWithArray:array leftOffset:low + 1 rightOffset:right]; 265 | } 266 | } 267 | 268 | - (void) sortUsingBubbleTypeWithArray:(NSArray *) array { 269 | NSMutableArray * sortArray = [NSMutableArray arrayWithArray:array]; 270 | BOOL exchanged = YES; 271 | NSInteger count = sortArray.count; 272 | for (int i = 0; i < count - 1 && exchanged; i ++) { 273 | exchanged = NO; 274 | STSortOperation * operation0 = [[STSortOperation alloc] init]; 275 | operation0.operationType = STSortOperationTypeMoveBaseline1; 276 | operation0.index1 = i; 277 | operation0.sortView = self.sortView; 278 | [self.animationQueue addOperation:operation0]; 279 | 280 | for (int j = 0; j < (count - 1 - i); j ++) { 281 | STSortOperation * operation1 = [[STSortOperation alloc] init]; 282 | operation1.operationType = STSortOperationTypeMoveBaseline2; 283 | operation1.index2 = j; 284 | operation1.sortView = self.sortView; 285 | [self.animationQueue addOperation:operation1]; 286 | 287 | id obj0 = [sortArray objectAtIndex:j]; 288 | id obj1 = [sortArray objectAtIndex:j + 1]; 289 | if ([obj0 intValue] > [obj1 intValue]) { 290 | exchanged = YES; 291 | /// 需要交换 obj0,obj1 292 | [sortArray exchangeObjectAtIndex:j withObjectAtIndex:j + 1]; 293 | STSortOperation * operation3 = [[STSortOperation alloc] init]; 294 | operation3.operationType = STSortOperationTypeExchangeElement; 295 | operation3.index1 = j; 296 | operation3.index2 = j + 1; 297 | operation3.sortView = self.sortView; 298 | [self.animationQueue addOperation:operation3]; 299 | } 300 | } 301 | } 302 | } 303 | 304 | 305 | - (void) setSortArray:(NSArray *)sortArray { 306 | self.sortMutableArray = [NSMutableArray arrayWithArray:sortArray]; 307 | _sortArray = sortArray; 308 | } 309 | 310 | 311 | 312 | - (void) viewSourceCode:(id) sender { 313 | STCodeViewController * viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"STCodeViewController"]; 314 | viewController.algorithmType = self.arraySortType; 315 | [self.navigationController pushViewController:viewController animated:YES]; 316 | } 317 | 318 | - (void) logArray:(NSArray *) array { 319 | [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 320 | printf("%d ", [obj intValue]); 321 | }]; 322 | printf("\n"); 323 | } 324 | @end 325 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /排序算法图形/STBasic/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // STBasic 4 | // 5 | // Created by SunJiangting on 13-11-1. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "STAppDelegate.h" 12 | 13 | 14 | void sort(int *a, int left, int right) 15 | { 16 | if(left >= right)/*如果左边索引大于或者等于右边的索引就代表已经整理完成一个组了*/ 17 | { 18 | return ; 19 | } 20 | int i = left; 21 | int j = right; 22 | int key = a[left]; 23 | 24 | while(i < j) /*控制在当组内寻找一遍*/ 25 | { 26 | while(i < j && key <= a[j]) 27 | /*而寻找结束的条件就是,1,找到一个小于或者大于key的数(大于或小于取决于你想升 28 | 序还是降序)2,没有符合条件1的,并且i与j的大小没有反转*/ 29 | { 30 | j--;/*向前寻找*/ 31 | } 32 | 33 | a[i] = a[j]; 34 | /*找到一个这样的数后就把它赋给前面的被拿走的i的值(如果第一次循环且key是 35 | a[left],那么就是给key)*/ 36 | 37 | while(i < j && key >= a[i]) 38 | /*这是i在当组内向前寻找,同上,不过注意与key的大小关系停止循环和上面相反, 39 | 因为排序思想是把数往两边扔,所以左右两边的数大小与key的关系相反*/ 40 | { 41 | i++; 42 | } 43 | 44 | a[j] = a[i]; 45 | } 46 | 47 | a[i] = key;/*当在当组内找完一遍以后就把中间数key回归*/ 48 | 49 | sort(a, left, i - 1);/*最后用同样的方式对分出来的左边的小组进行同上的做法*/ 50 | sort(a, i + 1, right);/*用同样的方式对分出来的右边的小组进行同上的做法*/ 51 | /*当然最后可能会出现很多分左右,直到每一组的i = j 为止*/ 52 | } 53 | 54 | 55 | int main(int argc, char * argv[]) 56 | { 57 | @autoreleasepool { 58 | 59 | int a [12] = {20,6,2,8,9,12,2,4,3,4,7,11}; 60 | sort(a, 0, 11); 61 | 62 | // for (int i = 0 ; i< 12;i++ ) { 63 | // NSLog(@"快速排序 :%d",a[i]); 64 | // } 65 | 66 | 67 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([STAppDelegate class])); 68 | 69 | } 70 | } 71 | 72 | 73 | -------------------------------------------------------------------------------- /排序算法图形/STBasicTests/STBasicTests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.suen.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundlePackageType 14 | BNDL 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleSignature 18 | ???? 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /排序算法图形/STBasicTests/STBasicTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // STBasicTests.m 3 | // STBasicTests 4 | // 5 | // Created by SunJiangting on 13-11-1. 6 | // Copyright (c) 2013年 SunJiangting. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STBasicTests : XCTestCase 12 | 13 | @end 14 | 15 | @implementation STBasicTests 16 | 17 | - (void)setUp 18 | { 19 | [super setUp]; 20 | // Put setup code here. This method is called before the invocation of each test method in the class. 21 | } 22 | 23 | - (void)tearDown 24 | { 25 | // Put teardown code here. This method is called after the invocation of each test method in the class. 26 | [super tearDown]; 27 | } 28 | 29 | - (void)testExample 30 | { 31 | XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__); 32 | } 33 | 34 | @end 35 | -------------------------------------------------------------------------------- /排序算法图形/STBasicTests/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | --------------------------------------------------------------------------------