UIDynamic使用

来源:互联网 发布:骑马与砍杀火与剑优化 编辑:程序博客网 时间:2024/06/06 00:15

////  ViewController.m//  UIDynamic////  Created by hq on 16/5/3.//  Copyright © 2016年 hanqing. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *ball;@property (weak, nonatomic) IBOutlet UIView *bottomVIew;@property(nonatomic,strong) UIDynamicAnimator *anim;@end@implementation ViewController-(UIDynamicAnimator *)anim{        //设置仿真范围    if (_anim==nil) {        _anim=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];    }    return _anim;}- (void)viewDidLoad {    [super viewDidLoad];        }-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{            UITouch *touch=[touches anyObject];        CGPoint currentP=[touch locationInView:self.view];        //创建一个捕捉行为    UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.ball snapToPoint:currentP];        //防抖系数,越小,越抖,最大值为1    snap.damping=0;        //必须清空上一次的才能继续使用    [self.anim removeAllBehaviors];        [self.anim addBehavior:snap];}-(void) testCollisionBehaviorByPath{        //创建一个碰撞行为    UICollisionBehavior *coll=[[UICollisionBehavior alloc] init];    //添加一个碰撞的边界(路径)        UIBezierPath *path=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)];            [coll addBoundaryWithIdentifier:@"path" forPath:path];        [coll addItem:self.ball];    //[coll addItem:self.bottomVIew];        UIGravityBehavior *grav=[[UIGravityBehavior alloc] init];    [grav addItem:self.ball];            [self.anim addBehavior:coll];    [self.anim addBehavior:grav];    }//碰撞边界为用线围起来的一块区域-(void) testCollisionBehavior1{        //创建一个碰撞行为    UICollisionBehavior *coll=[[UICollisionBehavior alloc] init];    //添加一个碰撞的边界    [coll addBoundaryWithIdentifier:@"line1" fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, self.view.bounds.size.height*0.5)];        [coll addBoundaryWithIdentifier:@"line2" fromPoint:CGPointMake(0, self.view.bounds.size.height*0.5) toPoint:CGPointMake(self.view.bounds.size.width, self.view.bounds.size.height)];        [coll addItem:self.ball];    [coll addItem:self.bottomVIew];        UIGravityBehavior *grav=[[UIGravityBehavior alloc] init];    [grav addItem:self.ball];            [self.anim addBehavior:coll];    [self.anim addBehavior:grav];}//重力仿真器-(void) testGravity{        //创建xx的仿真行为    UIGravityBehavior *grav=[[UIGravityBehavior alloc]init];        [grav addItem:self.ball];        [self.anim addBehavior:grav];    }- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    }@end


0 0