首页 > 代码库 > <图形图像,动画,多媒体> 读书笔记 --- 力学行为特性
<图形图像,动画,多媒体> 读书笔记 --- 力学行为特性
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 = http://www.mamicode.com/@50.0;><图形图像,动画,多媒体> 读书笔记 --- 力学行为特性
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。