<图形图像,动画,多媒体> 读书笔记 --- 力学行为特性

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];


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值