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
  • 相关阅读:
    关于Velocity加减法等四则运算的迷思
    [有明信息]科目导向,精耕细作 ——浅谈房地产开发成本管理
    The Building Blocks-Enterprise Applications Part 2- Information Management and Business Analytics
    maven项目建立pom.xml报无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3
    编程离不开生活
    原声JS瀑布流加延迟载入
    uva 11722
    Android自定义控件View(三)组合控件
    Android自定义控件View(二)继承控件
    Android自定义控件View(一)
  • 原文地址:https://www.cnblogs.com/bigshow1949/p/5806213.html
Copyright © 2011-2022 走看看