zoukankan      html  css  js  c++  java
  • UIDynamic(捕捉行为)

    一、简介

    可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动

    UISnapBehavior的初始化

      - (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;

    UISnapBehavior常见属性

      @property (nonatomic, assign) CGFloat damping;

      用于减幅、减震(取值范围是0.0 ~ 1.0,值越大,震动幅度越小)

    UISnapBehavior使用注意

      如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除

    代码:

    //
    //
    //  捕捉行为:可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动
    //
    
    #import "YFSnapBehaviorViewController.h"
    @interface YFSnapBehaviorViewController()
    
    @property(nonatomic,strong)UIDynamicAnimator *animator;
    @property (nonatomic, strong) UIButton *blueView;
    
    @end
    
    
    @implementation YFSnapBehaviorViewController
    
    -(void)viewDidLoad {
    
        [super viewDidLoad];
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        UIButton *blueView = [[UIButton alloc] init];
        blueView.frame = CGRectMake(100, 100, 100, 100);
        blueView.backgroundColor = [UIColor blueColor];
        [self.view addSubview:blueView];
        self.blueView = blueView;
    }
    
    
    -(UIDynamicAnimator *)animator
    {
        if (_animator==nil) {
            //创建物理仿真器,设置仿真范围,ReferenceView为参照视图
            _animator=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
        }
        return _animator;
    }
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        //获取一个触摸点
        UITouch *touch=[touches anyObject];
        CGPoint point=[touch locationInView:touch.view];
        
        //1.创建捕捉行为
        //需要传入两个参数:一个物理仿真元素,一个捕捉点
        UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.blueView snapToPoint:point];
        //设置防震系数(0~1,数值越大,震动的幅度越小)
        snap.damping=arc4random_uniform(10)/10.0;
        
        //2.执行捕捉行为
        //注意:这个控件只能用在一个仿真行为上,如果要拥有持续的仿真行为,那么需要把之前的所有仿真行为删除
        //删除之前的所有仿真行为
        [self.animator removeAllBehaviors];
        [self.animator addBehavior:snap];
    }
    
    @end
  • 相关阅读:
    Cheap Kangaroo(求多个数的最大公约数)
    poj 1094 Sorting It All Out(拓扑排序)
    hdu 5695 Gym Class(拓扑排序)
    Cyclic Components (并查集)
    GCD LCM
    And Then There Was One (约瑟夫环变形)
    System Overload(约瑟夫环变形)
    POJ-1639 Picnic Planning 度数限制最小生成树
    Educational Codeforces Round 60 (Rated for Div. 2) E. Decypher the String
    (ACM模板)二分查找
  • 原文地址:https://www.cnblogs.com/bigshow1949/p/5806213.html
Copyright © 2011-2022 走看看