码迷,mamicode.com
首页 > 移动开发 > 详细

IOS7动力系统

时间:2015-06-14 16:41:05      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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

 

IOS7动力系统

标签:

原文地址:http://www.cnblogs.com/huen/p/4478642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!