Skip to content

Commit c3d7db2

Browse files
author
wangxiaofeinin
committed
first commit
first commit
0 parents  commit c3d7db2

File tree

20 files changed

+1305
-0
lines changed

20 files changed

+1305
-0
lines changed

iOS7中弹簧式列表的制作.xcodeproj/project.pbxproj

Lines changed: 456 additions & 0 deletions
Large diffs are not rendered by default.

iOS7中弹簧式列表的制作.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0500"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "9B65508518B75A6E00BD5E68"
18+
BuildableName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.app"
19+
BlueprintName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;"
20+
ReferencedContainer = "container:iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
27+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
28+
shouldUseLaunchSchemeArgsEnv = "YES"
29+
buildConfiguration = "Debug">
30+
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "9B6550A618B75A6F00BD5E68"
36+
BuildableName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;Tests.xctest"
37+
BlueprintName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;Tests"
38+
ReferencedContainer = "container:iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.xcodeproj">
39+
</BuildableReference>
40+
</TestableReference>
41+
</Testables>
42+
<MacroExpansion>
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "9B65508518B75A6E00BD5E68"
46+
BuildableName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.app"
47+
BlueprintName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;"
48+
ReferencedContainer = "container:iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.xcodeproj">
49+
</BuildableReference>
50+
</MacroExpansion>
51+
</TestAction>
52+
<LaunchAction
53+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
54+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
55+
launchStyle = "0"
56+
useCustomWorkingDirectory = "NO"
57+
buildConfiguration = "Debug"
58+
ignoresPersistentStateOnLaunch = "NO"
59+
debugDocumentVersioning = "YES"
60+
allowLocationSimulation = "YES">
61+
<BuildableProductRunnable>
62+
<BuildableReference
63+
BuildableIdentifier = "primary"
64+
BlueprintIdentifier = "9B65508518B75A6E00BD5E68"
65+
BuildableName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.app"
66+
BlueprintName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;"
67+
ReferencedContainer = "container:iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.xcodeproj">
68+
</BuildableReference>
69+
</BuildableProductRunnable>
70+
<AdditionalOptions>
71+
</AdditionalOptions>
72+
</LaunchAction>
73+
<ProfileAction
74+
shouldUseLaunchSchemeArgsEnv = "YES"
75+
savedToolIdentifier = ""
76+
useCustomWorkingDirectory = "NO"
77+
buildConfiguration = "Release"
78+
debugDocumentVersioning = "YES">
79+
<BuildableProductRunnable>
80+
<BuildableReference
81+
BuildableIdentifier = "primary"
82+
BlueprintIdentifier = "9B65508518B75A6E00BD5E68"
83+
BuildableName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.app"
84+
BlueprintName = "iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;"
85+
ReferencedContainer = "container:iOS7&#20013;&#24377;&#31783;&#24335;&#21015;&#34920;&#30340;&#21046;&#20316;.xcodeproj">
86+
</BuildableReference>
87+
</BuildableProductRunnable>
88+
</ProfileAction>
89+
<AnalyzeAction
90+
buildConfiguration = "Debug">
91+
</AnalyzeAction>
92+
<ArchiveAction
93+
buildConfiguration = "Release"
94+
revealArchiveInOrganizer = "YES">
95+
</ArchiveAction>
96+
</Scheme>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>iOS7中弹簧式列表的制作.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>9B65508518B75A6E00BD5E68</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>9B6550A618B75A6F00BD5E68</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
</dict>
26+
</dict>
27+
</plist>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4451" systemVersion="13A461" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="vXZ-lx-hvc">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3676"/>
5+
</dependencies>
6+
<scenes>
7+
<!--class Prefix:identifier View Controller-->
8+
<scene sceneID="ufC-wZ-h7g">
9+
<objects>
10+
<viewController id="vXZ-lx-hvc" customClass="FFViewController" sceneMemberID="viewController">
11+
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
12+
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
13+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
14+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
15+
</view>
16+
</viewController>
17+
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
18+
</objects>
19+
</scene>
20+
</scenes>
21+
<simulatedMetricsContainer key="defaultSimulatedMetrics">
22+
<simulatedStatusBarMetrics key="statusBar"/>
23+
<simulatedOrientationMetrics key="orientation"/>
24+
<simulatedScreenMetrics key="destination" type="retina4"/>
25+
</simulatedMetricsContainer>
26+
</document>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// FFAppDelegate.h
3+
// iOS7中弹簧式列表的制作
4+
//
5+
// Created by 王小飞您 on 14-2-21.
6+
// Copyright (c) 2014年 王小飞. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface FFAppDelegate : UIResponder <UIApplicationDelegate>
12+
13+
@property (strong, nonatomic) UIWindow *window;
14+
15+
@end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// FFAppDelegate.m
3+
// iOS7中弹簧式列表的制作
4+
//
5+
// Created by 王小飞您 on 14-2-21.
6+
// Copyright (c) 2014年 王小飞. All rights reserved.
7+
//
8+
9+
#import "FFAppDelegate.h"
10+
11+
@implementation FFAppDelegate
12+
13+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14+
{
15+
// Override point for customization after application launch.
16+
return YES;
17+
}
18+
19+
- (void)applicationWillResignActive:(UIApplication *)application
20+
{
21+
// 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.
22+
// 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.
23+
}
24+
25+
- (void)applicationDidEnterBackground:(UIApplication *)application
26+
{
27+
// 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.
28+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
29+
}
30+
31+
- (void)applicationWillEnterForeground:(UIApplication *)application
32+
{
33+
// 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.
34+
}
35+
36+
- (void)applicationDidBecomeActive:(UIApplication *)application
37+
{
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+
{
43+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
44+
}
45+
46+
@end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// FFSpringCollViewFlowlayout.h
3+
// iOS7中弹簧式列表的制作
4+
//
5+
// Created by 王小飞您 on 14-2-23.
6+
// Copyright (c) 2014年 王小飞. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface FFSpringCollViewFlowlayout : UICollectionViewFlowLayout
12+
13+
@end
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// FFSpringCollViewFlowlayout.m
3+
// iOS7中弹簧式列表的制作
4+
//
5+
// Created by 王小飞您 on 14-2-23.
6+
// Copyright (c) 2014年 王小飞. All rights reserved.
7+
//
8+
9+
#import "FFSpringCollViewFlowlayout.h"
10+
@interface FFSpringCollViewFlowlayout()
11+
@property (nonatomic, strong) UIDynamicAnimator *animator;
12+
@end
13+
@implementation FFSpringCollViewFlowlayout
14+
15+
// prepareLayout将在CollectionView进行排版的时候被调用
16+
- (void)prepareLayout
17+
{
18+
[super prepareLayout];
19+
20+
if (!self.animator) {
21+
self.animator = [[UIDynamicAnimator alloc] initWithCollectionViewLayout:self];
22+
23+
// 取出内容区域尺寸
24+
CGSize contentSize = [self collectionViewContentSize];
25+
26+
// 取出所有的items
27+
NSArray *items = [super layoutAttributesForElementsInRect:CGRectMake(0, 0, contentSize.width, contentSize.height)];
28+
29+
// 遍历,为每个LayoutAttribute的中点上,添加附着件Attachment,锚点
30+
for (UICollectionViewLayoutAttributes *item in items) {
31+
UIAttachmentBehavior *spring = [[UIAttachmentBehavior alloc] initWithItem:item attachedToAnchor:item.center];
32+
33+
spring.length = 0;
34+
spring.damping = 0.5;
35+
spring.frequency = 0.8;
36+
37+
[self.animator addBehavior:spring];
38+
}
39+
}
40+
}
41+
42+
#pragma 通过-initWithCollectionViewLayout:进行初始化后,这个UIDynamicAnimator实例便和我们的layout进行了绑定,之后这个layout对应的attributes都应该由绑定的UIDynamicAnimator的实例给出。
43+
44+
// 根据rect布局整体的的Attribute
45+
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
46+
{
47+
return [self.animator itemsInRect:rect];
48+
}
49+
50+
// 布局每行的Attribute
51+
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
52+
{
53+
return [self.animator layoutAttributesForCellAtIndexPath:indexPath];
54+
}
55+
56+
57+
#pragma 每次layout的bounds发生变化的时候,collectionView都会询问这个方法是否需要为这个新的边界和更新layout。一般情况下只要layout没有根据边界不同而发生变化的话,这个方法直接不做处理地返回NO,表示保持现在的layout即可,而每次bounds改变时这个方法都会被调用的特点正好可以满足我们更新锚点的需求,因此我们可以在这里面完成锚点的更新。
58+
59+
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
60+
{
61+
UIScrollView *scrollView = self.collectionView;
62+
63+
// 计算滚动的距离
64+
CGFloat scrollDelta = newBounds.origin.y - scrollView.bounds.origin.y;
65+
66+
// 取出屏幕上触摸点的坐标
67+
CGPoint touchLocation = [scrollView.panGestureRecognizer locationInView:scrollView];
68+
69+
// 遍历出所有动作,每个动作动绑定着唯一的item(LayoutAttribute)
70+
for (UIAttachmentBehavior *spring in self.animator.behaviors) {
71+
// 取出每个item上绑定锚点的坐标
72+
CGPoint anchorPoint = spring.anchorPoint;
73+
74+
// int abs(int i); // 处理int类型的取绝对值
75+
// double fabs(double i); //处理double类型的取绝对值
76+
// float fabsf(float i); //处理float类型的取绝对值
77+
78+
// 计算触摸点到锚点的距离
79+
CGFloat distanceFromTouch = fabsf(anchorPoint.y - touchLocation.y);
80+
81+
// 滚动的阻力系数
82+
CGFloat scrollResistance = distanceFromTouch / [UIScreen mainScreen].bounds.size.height;
83+
84+
// 这一个动作可能涉及好多item,这里只绑定了一个,所以取出第一个。
85+
// 更新锚点的位置
86+
UICollectionViewLayoutAttributes *item = [spring.items firstObject];
87+
CGPoint center = item.center;
88+
89+
center.y += (scrollDelta > 0) ? MIN(scrollDelta, scrollDelta * scrollResistance)
90+
: MAX(scrollDelta, scrollDelta * scrollResistance);
91+
92+
item.center = center;
93+
94+
[self.animator updateItemUsingCurrentState:item];
95+
}
96+
97+
return NO;
98+
99+
100+
101+
}
102+
103+
@end

0 commit comments

Comments
 (0)