zoukankan      html  css  js  c++  java
  • iOS开发UIkit动力学UIDynamicAnimator一系列动画

    UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性。

    UIAttachmentBehavior(吸附),UICollisionBehavior(碰撞),UIGravityBehavior(重力),UIPushBehavior(推动),UISnapBehavior(捕捉)。另外还有一个辅助的行为UIDynamicItemBehavior,用来在item层级设定一些参数,比如item的摩擦,阻力,角阻力,弹性密度和可允许的旋转等等。

    1.UIAttachmentBehavior(吸附):

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.dynamicAnimation = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
        // 图片
        self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100,100)];
        self.imgView.image = [UIImage imageNamed:@"dizhijieyong_icon"];
        [self.view addSubview:self.imgView];
        self.imgView.userInteractionEnabled = YES;
        
        // 添加手势
        UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestAction:)];
        [self.imgView addGestureRecognizer:panGest];
        
        // 吸附 offset可以设置吸附的偏移,anchor是设置锚点
        self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:UIOffsetMake(100, 120) attachedToAnchor:CGPointMake(100, 100)];
    }
    
    // 实现一个pan手势,让一个image跟着手势跑
    
    - (void)panGestAction: (UIPanGestureRecognizer *)panGest{
        
        NSLog(@"手势触发啦!!!");
        
        // 取得location
        CGPoint location = [panGest locationInView:self.view];
        CGPoint boxLocation = [panGest locationInView:self.imgView];
        
        switch (panGest.state) {
                // 开始拖拽
            case UIGestureRecognizerStateBegan:
                
                NSLog(@"you touch started position %@",NSStringFromCGPoint(location));
                NSLog(@"location in image started is %@",NSStringFromCGPoint(boxLocation));
                // 移除
                [self.dynamicAnimation removeAllBehaviors];
                
                UIOffset offSet = UIOffsetMake(boxLocation.x - CGRectGetMidX(self.imgView.bounds),
                                               boxLocation.y - CGRectGetMidY(self.imgView.bounds));
                
                self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:offSet attachedToAnchor:location];
                self.attachment.damping=0.5;
                self.attachment.frequency=0.8;
                [self.dynamicAnimation addBehavior:self.attachment];
                break;
             case UIGestureRecognizerStateEnded:
                [self.dynamicAnimation removeAllBehaviors];
            default:
                [self.attachment setAnchorPoint:location];
                break;
        }
    }

    2.UIGravityBehavior(重力):

    // 实现点击屏幕掉落一张图片的代码
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        self.gravity = [[UIGravityBehavior alloc]initWithItems:nil];
        [self.dynamicAnimation addBehavior:self.gravity];
        
        UIImageView *gravImagView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 50, 50)];
        gravImagView.image = [UIImage imageNamed:@"zichanlingyong_icon"];
        [self.view addSubview:gravImagView];
        gravImagView.frame = CGRectMake(100, 100, 100, 100);
        [self.gravity addItem:gravImagView];
        
    }

    3.UISnapBehavior(捕捉):

    // UISnapBehavior 将UIView通过动画吸附到某个点上
    - (void) handleTap:(UITapGestureRecognizer *)paramTap{
        CGPoint tapPoint = [paramTap locationInView:self.view];
        
        if (self.snap != nil){
            [self.dynamicAnimation removeBehavior:self.snap];
        }
        self.snap = [[UISnapBehavior alloc] initWithItem:self.imgView snapToPoint:tapPoint];
        self.snap.damping = 0.5f;  //剧列程度
        [self.dynamicAnimation addBehavior:self.snap];
    }

    还有一些没有试过,有时间补充吧!!!嘻嘻~~~~~

  • 相关阅读:
    让数据更精准,神器标配:热图
    运维监控大数据的提取与分析
    IT运营新世界大会:广通软件开启双态运维大时代
    持续交付的Mesos与Docker导入篇
    运算符
    Django 模型层(2)
    Django模型层
    Django的模板层
    Django的视图层
    Django的路由层(URLconf)
  • 原文地址:https://www.cnblogs.com/pengsi/p/5798312.html
Copyright © 2011-2022 走看看