• IOS7动力系统


    1.

    #import "ViewController.h"
    
    @interface ViewController ()<UICollisionBehaviorDelegate>
    {
        UIDynamicAnimator *_animator;//力学动画
        
        UIGravityBehavior *_gravity;//重力行为
        UICollisionBehavior *_collision;//碰撞行为
        //
        BOOL _firstContact;
    }
    
    @property(nonatomic,strong)UIView *squareView;
    @property(nonatomic,strong)UIPushBehavior *pushBehavior;//推移行为
    @property(nonatomic,strong)UISnapBehavior *snapBehavior;//吸附行为
    @property(nonatomic,strong)UIDynamicAnimator *dAnimator;//力学动画
    
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIView *square = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
        square.backgroundColor = [UIColor grayColor];
        [self.view addSubview:square];
        
        _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
        
        //添加重量行为
        _gravity = [[UIGravityBehavior alloc]initWithItems:@[square]];
        [_animator addBehavior:_gravity];
        
        //添加碰撞行为
        _collision = [[UICollisionBehavior alloc]initWithItems:@[square]];
        _collision.translatesReferenceBoundsIntoBoundary = YES;
        [_animator addBehavior:_collision];
        
        //
        UIView *barrier = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 130, 20)];
        barrier.backgroundColor = [UIColor redColor];
        [self.view addSubview:barrier];
        
        //添加碰撞行为
    //    _collision = [[UICollisionBehavior alloc]initWithItems:@[square,barrier]];
    //    _collision.translatesReferenceBoundsIntoBoundary = YES;
    //    [_animator addBehavior:_collision];
        
        //为碰撞行为添加碰撞的边界
        CGPoint rightEdge = CGPointMake(barrier.frame.origin.x+barrier.frame.size.width, barrier.frame.origin.y);
        [_collision addBoundaryWithIdentifier:@"barrier" fromPoint:barrier.frame.origin
                                      toPoint:rightEdge];
        //************************************part2**************************************
        _collision.action = ^{
    //        NSLog(@"%@,%@",NSStringFromCGAffineTransform(square.transform),NSStringFromCGPoint(square.center));
        };
        //设置碰撞行为代理
        _collision.collisionDelegate = self;
        //辅助行为
        //设置它们的物理属性(如质量或弹性系数)
    //    UIDynamicItemBehavior *itemBeHaviour = [[UIDynamicItemBehavior alloc]initWithItems:@[square]];
    //    itemBeHaviour.elasticity = 0.6;//弹性系数属性
    //    itemBeHaviour.friction = 0.6;//摩擦系数
    //    itemBeHaviour.density = 9.8;//密度
    //    itemBeHaviour.resistance = 0.6;//阻力
    //    itemBeHaviour.angularResistance = 0.1;//转动阻力
    //    itemBeHaviour.allowsRotation = NO;//允许旋转
    //    [_animator addBehavior:itemBeHaviour];
        
    }
    
    #pragma mark -collisionDelegate Methods-
    -(void)collisionBehavior:(UICollisionBehavior *)behavior
         beganContactForItem:(id<UIDynamicItem>)item
      withBoundaryIdentifier:(id<NSCopying>)identifier
                     atPoint:(CGPoint)p{
        NSLog(@"Boundary contact occurred - %@", identifier);
        UIView *view = (UIView *)item;
        view.backgroundColor = [UIColor yellowColor];
        [UIView animateWithDuration:0.3 animations:^{
            view.backgroundColor= [UIColor grayColor];
        }];
        //动态添加行为
        if (!_firstContact) {
            _firstContact = YES;
            UIView *square = [[UIView alloc]initWithFrame:CGRectMake(30, 0, 100, 100)];
            square.backgroundColor = [UIColor grayColor];
            [self.view addSubview:square];
            
            [_collision addItem:square];
            [_gravity addItem:square];
            //附件行为
            UIAttachmentBehavior *attach = [[UIAttachmentBehavior alloc]initWithItem:view
                                                                      attachedToItem:square];
            [_animator addBehavior:attach];
        }
    }
    
    //////////////////////////////////////////part3推移行为和吸附行为
    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        [self createSmallSquareView];
        [self createGestureRecognizer];
        [self createAnimatorAndBehaviors];
        
    }
    
    - (void) createSmallSquareView{
        self.squareView =[[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
        
        self.squareView.backgroundColor = [UIColor greenColor];
        self.squareView.center = self.view.center;
        
        [self.view addSubview:self.squareView];
    }
    
    - (void) createGestureRecognizer{  //侦测视图单击
        UITapGestureRecognizer *tapGestureRecognizer =
        [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTap:)];
        [self.view addGestureRecognizer:tapGestureRecognizer];
    }
    
    - (void) createAnimatorAndBehaviors{
        self.dAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
        //碰撞行为
        UICollisionBehavior *collision = [[UICollisionBehavior alloc]
                                          initWithItems:@[self.squareView]];
        collision.translatesReferenceBoundsIntoBoundary = YES;
        [self.dAnimator addBehavior:collision];
        //推移行为
        self.pushBehavior = [[UIPushBehavior alloc]
                             initWithItems:@[self.squareView]
                             mode:UIPushBehaviorModeContinuous];//推移模式
        
        [self.dAnimator addBehavior:self.pushBehavior];
         
    }
    
    /*
     ///推移行为
    - (void) handleTap:(UITapGestureRecognizer *)paramTap{
        CGPoint tapPoint = [paramTap locationInView:self.view];
        CGPoint squareViewCenterPoint = self.squareView.center;
        //设置推移的角度
        CGFloat deltaX = tapPoint.x - squareViewCenterPoint.x;
        CGFloat deltaY = tapPoint.y - squareViewCenterPoint.y;
        CGFloat angle = atan2(deltaY, deltaX);
        [self.pushBehavior setAngle:angle];
        
        //勾股
        CGFloat distanceBetweenPoints =
        sqrt(pow(tapPoint.x - squareViewCenterPoint.x, 2.0) +
             pow(tapPoint.y - squareViewCenterPoint.y, 2.0));
        //double pow(double x, double y);计算以x为底数的y次幂
        //double sqrt (double);开平方
        
        [self.pushBehavior setMagnitude:distanceBetweenPoints / 200.0f]; //推力的大小(移动速度)
        //每1个magnigude将会引起100/平方秒的加速度,这里分母越大,速度越小
    }
    */
    
    ///吸附行为
    - (void) handleTap:(UITapGestureRecognizer *)paramTap{
        CGPoint tapPoint = [paramTap locationInView:self.view];
        
        if (self.snapBehavior != nil){
            [self.dAnimator removeBehavior:self.snapBehavior];
        }
        self.snapBehavior = [[UISnapBehavior alloc] initWithItem:self.squareView
                                                     snapToPoint:tapPoint];
        self.snapBehavior.damping = 0.5f;  //剧列程度
        [self.dAnimator addBehavior:self.snapBehavior];
    }
    
    
    @end
  • 相关阅读:
    CC2540-BLE4.0 学历笔记1之串口体验
    cc2530的PWM实现
    用qt写的一个简单到不能在简单的上位机
    在Kubuntu14.04中安装小企鹅输入法
    初始化umi项目
    工具杂记(三) -- zookeeper
    工具杂记(二) -- Centos7
    工具杂记(一) -- vagrant
    (一)Docker导学
    JVM随笔(二)之搜索类
  • 原文地址:https://www.cnblogs.com/huen/p/4478642.html
Copyright © 2020-2023  润新知