IOS7動力系統

來源:互聯網
上載者:User

標籤:

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動力系統

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.