Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b50d74

Browse files
committed
初始化
Signed-off-by: Zhiping Yang <[email protected]>
1 parent 43da025 commit 7b50d74

32 files changed

+2076
-0
lines changed

DynamicsDemo.xcodeproj/project.pbxproj

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

DynamicsDemo.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.

DynamicsDemo/80.png

5.54 KB
Loading

DynamicsDemo/AppDelegate.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// AppDelegate.h
3+
// DynamicsDemo
4+
//
5+
// Created by XcodeYang on 4/2/15.
6+
// Copyright (c) 2015 XcodeYang. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface AppDelegate : UIResponder <UIApplicationDelegate>
12+
13+
@property (strong, nonatomic) UIWindow *window;
14+
15+
16+
@end
17+

DynamicsDemo/AppDelegate.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// AppDelegate.m
3+
// DynamicsDemo
4+
//
5+
// Created by XcodeYang on 4/2/15.
6+
// Copyright (c) 2015 XcodeYang. All rights reserved.
7+
//
8+
9+
#import "AppDelegate.h"
10+
#import "RootViewController.h"
11+
@interface AppDelegate ()
12+
13+
@end
14+
15+
@implementation AppDelegate
16+
17+
18+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
19+
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
20+
RootViewController *root = [[RootViewController alloc]init];
21+
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:root];
22+
[self.window makeKeyAndVisible];
23+
self.window.backgroundColor = [UIColor whiteColor];
24+
return YES;
25+
}
26+
27+
- (void)applicationWillResignActive:(UIApplication *)application {
28+
// 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.
29+
// 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.
30+
}
31+
32+
- (void)applicationDidEnterBackground:(UIApplication *)application {
33+
// 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.
34+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
35+
}
36+
37+
- (void)applicationWillEnterForeground:(UIApplication *)application {
38+
// 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.
39+
}
40+
41+
- (void)applicationDidBecomeActive:(UIApplication *)application {
42+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
43+
}
44+
45+
- (void)applicationWillTerminate:(UIApplication *)application {
46+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
47+
}
48+
49+
@end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// AttachmentViewController.h
3+
// DynamicsDemo
4+
//
5+
// Created by XcodeYang on 15/4/10.
6+
// Copyright (c) 2015年 XcodeYang. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface AttachmentViewController : UIViewController
12+
13+
@property (nonatomic) BOOL tangHuang;
14+
@end
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// AttachmentViewController.m
3+
// DynamicsDemo
4+
//
5+
// Created by XcodeYang on 15/4/10.
6+
// Copyright (c) 2015年 XcodeYang. All rights reserved.
7+
//
8+
9+
#import "AttachmentViewController.h"
10+
11+
@interface AttachmentViewController ()
12+
{
13+
UIImageView * uTang;
14+
15+
UIDynamicAnimator * _animator;
16+
17+
UIAttachmentBehavior *_attach;
18+
19+
UIImageView *kongImageView;
20+
21+
CAShapeLayer *shapeLayer;
22+
}
23+
@end
24+
25+
@implementation AttachmentViewController
26+
27+
- (void)viewDidLoad {
28+
[super viewDidLoad];
29+
self.view.backgroundColor = [UIColor lightGrayColor];
30+
31+
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
32+
33+
uTang = [[UIImageView alloc] initWithFrame:CGRectMake(100,100, 80, 80)];
34+
uTang.image = [UIImage imageNamed:@"80"];
35+
[self.view addSubview:uTang];
36+
37+
kongImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AttachmentPoint_Mask"]];
38+
kongImageView.frame = CGRectMake(0, 0, 10, 10);
39+
kongImageView.center = CGPointMake(uTang.bounds.size.width / 2 - 30, uTang.bounds.size.height / 2 - 30);
40+
kongImageView.backgroundColor = [UIColor lightGrayColor];
41+
kongImageView.layer.borderWidth = 2;
42+
kongImageView.layer.borderColor = [UIColor darkGrayColor].CGColor;
43+
kongImageView.layer.cornerRadius = 5;
44+
kongImageView.layer.masksToBounds = YES;
45+
[uTang addSubview:kongImageView];
46+
47+
// 设置观察者,在手指离开以后继续画线
48+
[uTang addObserver:self forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
49+
50+
//重力
51+
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[uTang]];
52+
[_animator addBehavior:gravity];
53+
54+
// 连接
55+
_attach = [[UIAttachmentBehavior alloc]initWithItem:uTang offsetFromCenter:UIOffsetMake(-30, -30) attachedToAnchor:CGPointMake(CGRectGetMidX(self.view.bounds), 120)];
56+
_attach.length = _tangHuang?60:120;
57+
58+
// 弹簧效果
59+
_attach.damping = 0.1;
60+
_attach.frequency = _tangHuang?0.6:0;
61+
[_animator addBehavior:_attach];
62+
63+
// 碰撞
64+
UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[uTang]];
65+
collision.collisionMode = UICollisionBehaviorModeEverything;
66+
collision.translatesReferenceBoundsIntoBoundary = YES;
67+
[_animator addBehavior:collision];
68+
69+
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
70+
[self.view addGestureRecognizer:pan];
71+
}
72+
73+
- (void)pan:(UIGestureRecognizer *)pan
74+
{
75+
CGPoint point = [pan locationInView:self.view];
76+
[_attach setAnchorPoint:point];
77+
}
78+
79+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
80+
{
81+
[self updateLine];
82+
}
83+
84+
- (void)updateLine
85+
{
86+
if (shapeLayer == nil) {
87+
shapeLayer = [CAShapeLayer layer];
88+
shapeLayer.fillColor = [[UIColor clearColor] CGColor];
89+
shapeLayer.lineJoin = kCALineJoinRound;
90+
shapeLayer.lineWidth = 3.0f;
91+
shapeLayer.strokeColor = [UIColor yellowColor].CGColor;
92+
shapeLayer.strokeEnd = 1.0f;
93+
[self.view.layer addSublayer:shapeLayer];
94+
}
95+
96+
CGPoint p = [self.view convertPoint:kongImageView.center fromView:uTang];
97+
98+
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
99+
[bezierPath moveToPoint:_attach.anchorPoint];
100+
[bezierPath addLineToPoint:p];
101+
shapeLayer.path = bezierPath.CGPath;
102+
}
103+
104+
- (void)dealloc{
105+
[uTang removeObserver:self forKeyPath:@"center"];
106+
}
107+
@end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6214" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6207"/>
5+
<capability name="Constraints with non-1.0 multipliers" minToolsVersion="5.1"/>
6+
</dependencies>
7+
<objects>
8+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
9+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
10+
<view contentMode="scaleToFill" id="iN0-l3-epB">
11+
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
12+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
13+
<subviews>
14+
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2015 XcodeYang. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye">
15+
<rect key="frame" x="20" y="439" width="441" height="21"/>
16+
<fontDescription key="fontDescription" type="system" pointSize="17"/>
17+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
18+
<nil key="highlightedColor"/>
19+
</label>
20+
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="DynamicsDemo" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX">
21+
<rect key="frame" x="20" y="140" width="441" height="43"/>
22+
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
23+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
24+
<nil key="highlightedColor"/>
25+
</label>
26+
</subviews>
27+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
28+
<constraints>
29+
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
30+
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
31+
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
32+
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
33+
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
34+
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
35+
</constraints>
36+
<nil key="simulatedStatusBarMetrics"/>
37+
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
38+
<point key="canvasLocation" x="548" y="455"/>
39+
</view>
40+
</objects>
41+
</document>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// CollosionViewController.h
3+
// DynamicsDemo
4+
//
5+
// Created by XcodeYang on 15/4/10.
6+
// Copyright (c) 2015年 XcodeYang. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface CollosionViewController : UIViewController
12+
13+
@end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// CollosionViewController.m
3+
// DynamicsDemo
4+
//
5+
// Created by XcodeYang on 15/4/10.
6+
// Copyright (c) 2015年 XcodeYang. All rights reserved.
7+
//
8+
9+
#import "CollosionViewController.h"
10+
11+
@interface CollosionViewController ()
12+
{
13+
UIImageView * uTang;
14+
UIImageView * uTang2;
15+
16+
UIDynamicAnimator * _animator;
17+
}
18+
@end
19+
20+
@implementation CollosionViewController
21+
22+
- (void)viewDidLoad {
23+
[super viewDidLoad];
24+
self.view.backgroundColor = [UIColor lightGrayColor];
25+
26+
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
27+
28+
29+
uTang = [[UIImageView alloc] initWithFrame:CGRectMake(100,50, 80, 80)];
30+
uTang.image = [UIImage imageNamed:@"80"];
31+
uTang2.transform = CGAffineTransformRotate(uTang2.transform, -M_PI_4/2.0);
32+
[self.view addSubview:uTang];
33+
34+
uTang2 = [[UIImageView alloc] initWithFrame:CGRectMake(150,20, 80, 80)];
35+
uTang2.image = [UIImage imageNamed:@"80"];
36+
uTang2.transform = CGAffineTransformRotate(uTang2.transform, M_PI_4);
37+
[self.view addSubview:uTang2];
38+
39+
[self addBehavior];
40+
41+
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
42+
[self.view addGestureRecognizer:pan];
43+
}
44+
45+
- (void)pan:(UIGestureRecognizer *)pan
46+
{
47+
[_animator removeAllBehaviors];
48+
49+
uTang.center = [pan locationInView:self.view];
50+
51+
if (pan.state==UIGestureRecognizerStateEnded) {
52+
[self addBehavior];
53+
}
54+
}
55+
56+
- (void)addBehavior{
57+
// 重力
58+
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]initWithItems:@[uTang,uTang2]];
59+
[_animator addBehavior:gravity];
60+
61+
// 碰撞
62+
UICollisionBehavior *collision = [[UICollisionBehavior alloc]initWithItems:@[uTang,uTang2]];
63+
collision.collisionMode = UICollisionBehaviorModeEverything;
64+
collision.translatesReferenceBoundsIntoBoundary = YES;
65+
collision.collisionDelegate = nil;//delegate自己看
66+
[_animator addBehavior:collision];
67+
}
68+
69+
70+
@end

0 commit comments

Comments
 (0)