UIPushBehavior :推动效果 ,UISnapBehavior:迅速移动效果,UIAttachmentBehavior:附着效果

来源:互联网 发布:ones刻录软件下载 编辑:程序博客网 时间:2024/05/18 18:53
// 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; //推动角度 // A continuous force vector with a magnitude of 1.0, applied to a 100 point x 100 point view whose density value is 1.0, results in view acceleration of 100 points per s^2 @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) typedef NS_ENUM(NSInteger, UIAttachmentBehaviorType) { UIAttachmentBehaviorTypeItems, //吸附一个元素 UIAttachmentBehaviorTypeAnchor //吸附一个点 } NS_ENUM_AVAILABLE_IOS(7_0); //设置吸附效果的样式 @property (readonly, nonatomic) UIAttachmentBehaviorType attachedBehaviorType;
//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   幅度

首先定义要使用的两个视图和容器为全局变量

UIDynamicAnimator *dynamicAnimator;    UIView *view1;    UIView *view2;

初始化视图 和UIDynamicAnimator

dynamicAnimator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];    view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];    view1.backgroundColor = [UIColor orangeColor];    [self.view addSubview:view1];    view2 = [[UIView alloc]initWithFrame:CGRectMake(150, 50, 100, 100)];    view2.backgroundColor = [UIColor grayColor];    [self.view addSubview:view2];    //添加一个手势来触发方法 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushView)];    [view1 addGestureRecognizer:tap];//    //    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(snap:)];//    [self.view addGestureRecognizer:tap];//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(attachment:)];//    [self.view addGestureRecognizer:tap];

推动效果

- (void)pushView{//    先把容器里面的效果移除    [dynamicAnimator removeAllBehaviors]; //   初始化一个推动效果 //initWithItems后面添加的是一个 数组    UIPushBehavior *push = [[UIPushBehavior alloc]initWithItems:@[view1] mode:UIPushBehaviorModeContinuous];//    设置推的方向//    x正右负左  y正下 负上    push.pushDirection = CGVectorMake(1, 0);    push.magnitude=1;    //添加到效果器里面    [dynamicAnimator addBehavior:push];}

吸附效果

-(void)attachment:(UITapGestureRecognizer *)sender{//同样先移除里面的效果    [dynamicAnimator removeAllBehaviors];    //初始化吸附效果    //initWithItem这里注意 后面加的是一个视图,而不是数组    //initWithItems后面加的才是数组,这里多了个  s    UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc]initWithItem:view1 offsetFromCenter:UIOffsetMake(20, 20) attachedToAnchor:[sender locationInView:self.view]];    attachment.length = 10;    attachment.damping = 0.1;    attachment.frequency = 6;    [dynamicAnimator addBehavior:attachment];UIAttachmentBehavior *attachment2 = [[UIAttachmentBehavior alloc]initWithItem:view1 offsetFromCenter:UIOffsetMake(50, 50)  attachedToItem:view2 offsetFromCenter:UIOffsetMake(50, 50)];    attachment2.length = 10;    attachment2.damping = 0.1;    attachment2.frequency = 6;    [dynamicAnimator addBehavior:attachment2];}

迅速移动效果

- (void)snap:(UITapGestureRecognizer *)sender{    [dynamicAnimator removeAllBehaviors];//    迅速移动效果 只能一次 添加到一个元素上 snapToPoint让它移动到哪一个点、    UISnapBehavior *snap = [[UISnapBehavior alloc]initWithItem:view1 snapToPoint:[sender locationInView:self.view]];    snap.damping = 0.1;    [dynamicAnimator addBehavior:snap];}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 ic卡消磁了怎么办妙招 如果电卡消磁了怎么办 学校水卡消磁了怎么办 小区水卡消磁了怎么办 乐高配件少了怎么办 乐高世界锁区怎么办 商铺到期40年后怎么办 pu皮的包包脏了怎么办 盐酸弄到皮肤上怎么办 小区主管道堵了怎么办 小玻璃渣进眼睛怎么办 吃了发霉的米饭怎么办 吃了发霉的玉米怎么办 误吃了黄曲霉素怎么办 月子里奶不够吃怎么办 打了地佐辛头晕怎么办 粉瘤化脓破了怎么办 脸里面长了粉瘤怎么办 强险保单丢了怎么办 我被财产保全了怎么办 孕妇血糖高怎么办降下来吗 血糖不高尿糖高怎么办 全血粘度1 5偏高怎么办 血粘度低切偏低怎么办 书侧面上弄上油怎么办 肌注部位打偏了怎么办 吃头孢克圬过敏怎么办 孕妇让蚊子咬了怎么办 怀孕了被蚊子咬了怎么办 夏天被蚊子咬了该怎么办 尿酸高怎么办有什么危害 RH阴性血生二胎怎么办 柴油箱里加了水怎么办 五菱之光烧机油怎么办 软轴水泵不出水怎么办 小车柴油冻住了怎么办 皮卡柴油车冻了怎么办 发现4柴油车冻了怎么办 装载机暖风不热怎么办 柴机油加汽机车怎么办 货车故障灯亮了怎么办