zoukankan      html  css  js  c++  java
  • IOS-UI-UIDynamic(二)

     UIPushBehavior :推动效果

    UIAttachmentBehavior:附着效果

    UISnapBehavior:迅速移动效果

    一.重要的属性

    UIPushBehavior :推动效果

     typedef NS_ENUM(NSInteger, UIPushBehaviorMode) {

     UIPushBehaviorModeContinuous, 持续的力

     UIPushBehaviorModeInstantaneous 瞬间的力

     } NS_ENUM_AVAILABLE_IOS(7_0);

     @property (nonatomic, readonly) UIPushBehaviorMode mode; 推动效果的样式

     @property (nonatomic, readwrite) BOOL active; 是否激活

     @property (readwrite, nonatomic) CGFloat angle; 推动角度

     @property (readwrite, nonatomic) CGFloat magnitude; 推动力量

     @property (readwrite, nonatomic) CGVector pushDirection; 推动的方向

     UISnapBehavior:迅速移动效果

     // The point argument is expressed in the reference coordinate system

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

     迅速移动效果 只能一次 添加到一个元素上  snapToPoint 让他移动到哪一个点

     @property (nonatomic, assign) CGFloat damping; // damping value from 0.0 to 1.0. 0.0 is the least oscillation. damping 的范围是(0.0-1.0)

    UIAttachmentBehavior:附着效果

     吸附着一个视图 或者一个点  (也可以多个连接)

     附着效果 一个视图与一个锚点或者另一个视图相连接的情况

     附着行为描述的是两点之间的连接情况,可以模拟刚性或者弹性连接

     在多个物体间设定多个UIAttachmentBehavior,可以模拟多物体连接

     typedef NS_ENUM(NSInteger, UIAttachmentBehaviorType) {

     UIAttachmentBehaviorTypeItems, 吸附一个元素

     UIAttachmentBehaviorTypeAnchor 吸附一个点

     } NS_ENUM_AVAILABLE_IOS(7_0);

     设置吸附效果的样式

     @property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType;

     @property (readwrite, nonatomic) CGPoint anchorPoint;锚点

     @property (readwrite, nonatomic) CGFloat length;距离 与锚点的距离

     @property (readwrite, nonatomic) CGFloat damping; // 1: critical damping  跳跃度

     @property (readwrite, nonatomic) CGFloat frequency; // in Hertz   幅度

    二.代码

    1.推动效果

     1 #import "ViewController.h"
     2 
     3 @interface ViewController ()
     4 {
     5     UIDynamicAnimator * dynamicAnimator;
     6     UIView *view1;
     7     
     8 }
     9 @end
    10 
    11 @implementation ViewController
    12 
    13 - (void)viewDidLoad {
    14     [super viewDidLoad];
    15     dynamicAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    16     view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
    17     view1.backgroundColor = [UIColor redColor];
    18     [self.view addSubview:view1];
    19     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushView)];
    20     
    21     [view1 addGestureRecognizer:tap];
    - (void)pushView{
        [dynamicAnimator removeAllBehaviors];//移除所有的效果
        UIPushBehavior * push = [[UIPushBehavior alloc]initWithItems:@[view1] mode:UIPushBehaviorModeContinuous];
    //    mode的两个属性
        //    UIPushBehaviorModeContinuous,一直推动
    //    UIPushBehaviorModeInstantaneous 瞬间推动
        push.pushDirection = CGVectorMake(1, 0);
    //    x是正右,负左。y是正下负上。
        push.magnitude = 5;//量级:既是推动力大小
    //设置成NO时就不再响应效果
        //    push.active = NO;
        [dynamicAnimator addBehavior:push];//把推动效果添加到效果器上
    }

    2.瞬间移动效果(viewDidLoad里面添加另一个点击手势)

     UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(snap:)];
        
        [self.view addGestureRecognizer:tap1];

    之后

    -(void)snap:(UITapGestureRecognizer *)send{
        [dynamicAnimator removeAllBehaviors];
    //    迅速移动效果 只能一次 添加到一个元素上  snapToPoint 让他移动到哪一个点
        UISnapBehavior * snap = [[UISnapBehavior alloc]initWithItem:view1 snapToPoint:[send locationInView:self.view]];
    //跳动幅度:0~1,值越小,跳动幅度越大
        snap.damping = 0;
        [dynamicAnimator addBehavior:snap];
        
        
    }

    吸附效果

    @interface ViewController ()
    {
        UIDynamicAnimator * dynamicAnimator;
        UIView *view1;
         UIView *view2;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        dynamicAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
        view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
        view1.backgroundColor = [UIColor redColor];
        
        [self.view addSubview:view1];
        
        view2 = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 90, 90)];
        view2.backgroundColor = [UIColor blueColor];
        [self.view addSubview:view2];
    
        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(attachment:)];
        
        [self.view addGestureRecognizer:tap2];
    
    }
    -(void)attachment:(UITapGestureRecognizer *)send{
        [dynamicAnimator removeAllBehaviors];
    //    吸附到视图的某个点
        UIAttachmentBehavior * attachment = [[UIAttachmentBehavior alloc]initWithItem:view1 offsetFromCenter:UIOffsetMake(20, 20) attachedToAnchor:[send locationInView:self.view]];
        attachment.length = 50;//吸附范围
        attachment.damping = 0.5;
        attachment.frequency = 5;
        [dynamicAnimator addBehavior:attachment];
    //    两个元素吸附
    //    UIAttachmentBehavior *attachment1 = [[UIAttachmentBehavior alloc]initWithItem:view1 offsetFromCenter:UIOffsetMake(50, 50) attachedToItem:view2 offsetFromCenter:UIOffsetMake(50, 50)];
    //    attachment1.length = 50;//吸附范围
    //    attachment1.damping = 0.5;
    //    attachment1.frequency = 50;
    //    [dynamicAnimator addBehavior:attachment1];
        
       }

    另外,在iOS9之前这些效果只支持矩形。IOS9之后增加了新的属性UIDynamicItemCollisionBoundsType;因为本人的Xcode没升级,没法使用。

  • 相关阅读:
    百家号开发文档测试
    python使用selenium模拟登录网易
    python使用selenium模拟操作Chrome浏览器
    java 解析网易邮箱里面的附件地址,获取下载地址
    python连接hive
    linux重定向标准输入输出,标准错误
    linux 查看网络流量命令
    kafka源码阅读环境搭建
    tomcat启动index页面显示不出来
    git学习笔记
  • 原文地址:https://www.cnblogs.com/popper123/p/4852283.html
Copyright © 2011-2022 走看看