首页 技术 正文
技术 2022年11月13日
0 收藏 846 点赞 4,140 浏览 2619 个字

UIKit力学行为包括了:重力(UIGravityBehavior),碰撞(UICollisionBehavior),吸附(UIAttachmentBehavior),推(UIPushBehavior),甩(UISnapBehavior)和行为限制(UIDynamicItemBehavior).

- (void)viewDidAppear:(BOOL)animated {    [super viewDidAppear:animated];    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
//重力行为
_gravity = [[UIGravityBehavior alloc] initWithItems:@[_box]];
[_animator addBehavior:_gravity];
//碰撞行为
_collision = [[UICollisionBehavior alloc]
initWithItems:@[_box]]; [_collision addBoundaryWithIdentifier:@"barrier" fromPoint:_barrier.frame.origin
toPoint:CGPointMake(_barrier.frame.origin.x + _barrier.frame.size.width, _barrier.frame.origin.y)]; _collision.translatesReferenceBoundsIntoBoundary = YES;
_collision.collisionDelegate = self; [_animator addBehavior:_collision];
UIDynamicItemBehavior* itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[_box]];
itemBehaviour.elasticity = 0.5;
[_animator addBehavior:itemBehaviour];}- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p {
if (!_firstContact)
{
_firstContact = YES;
//设置吸附行为
self.attach = [[UIAttachmentBehavior alloc] initWithItem:_attachmentPoint attachedToItem:_box];
[self.animator addBehavior:self.attach]; //设置推行为
UIPushBehavior* push = [[UIPushBehavior alloc] initWithItems:@[_box] mode:UIPushBehaviorModeInstantaneous];
// [push setAngle:-M_PI/4 magnitude:5.0f]; //右上角45度
CGVector pushDirection = {0.5, -0.5}; //setAngle: magnitude:替代方法
[push setPushDirection:pushDirection];
[push setMagnitude:5.0f];
[_animator addBehavior:push];
}}

另一个我个人认为比較实用,就是甩行为

- (void)viewDidAppear:(BOOL)animated {    [super viewDidAppear:animated];    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];}- (IBAction)handleSnapGesture:(UITapGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:self.view]; // 移除甩行为
[_animator removeBehavior:_snap]; _snap = [[UISnapBehavior alloc] initWithItem:_box snapToPoint:point];
[self.animator addBehavior:_snap];
}

另一个就是类似桌面随手腕角度改变视图位移的方法

//设置山在X轴的偏移范围-50.0~50.0
UIInterpolatingMotionEffect *mountainEffectX;
mountainEffectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
mountainEffectX.maximumRelativeValue = @50.0;
mountainEffectX.minimumRelativeValue = @-50.0;
[self.mountain addMotionEffect:mountainEffectX]; //设置树在X轴的偏移范围-100.0~100.0
UIInterpolatingMotionEffect *treeEffectX;
treeEffectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x"
type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
treeEffectX.maximumRelativeValue = @100.0;
treeEffectX.minimumRelativeValue = @-100.0;
[self.tree addMotionEffect:treeEffectX];
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,034
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,782
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,863