UIKit Dynamics

    xiaoxiao2021-12-15  32

    1.重力效果

    UIImageView * lview =[[UIImageView alloc]initWithFrame:CGRectMake(20, 100, 40, 40)]; lview.image =[UIImage imageNamed:@"zc.jpg"]; [self.view addSubview:lview]; animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UIGravityBehavior* gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[lview]]; [gravityBeahvior setGravityDirection:CGVectorMake(0.0f, 0.5f)]; //property changed after printing of the book [animator addBehavior:gravityBeahvior];

    注意:UIDynamicAnimator要设置成成员变量才会有用(不知道为什么?)

    2.碰撞效果

    UIImageView * lview =[[UIImageView alloc]initWithFrame:CGRectMake(20, 120, 80, 80)]; lview.layer.cornerRadius =40; lview.layer.masksToBounds =YES; lview.image =[UIImage imageNamed:@"zc.jpg"]; [self.view addSubview:lview]; UIImageView * wview =[[UIImageView alloc]initWithFrame:CGRectMake(80, 70, 30, 30)]; wview.image =[UIImage imageNamed:@"zc.jpg"]; wview.layer.cornerRadius =15; wview.layer.masksToBounds =YES; [self.view addSubview:wview]; animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UIGravityBehavior* gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[lview,wview]]; [gravityBeahvior setGravityDirection:CGVectorMake(0.0f, 0.5f)]; UICollisionBehavior * collisionBehavior =[[UICollisionBehavior alloc]initWithItems:@[lview,wview]]; [collisionBehavior setCollisionMode: UICollisionBehaviorModeEverything]; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; [animator addBehavior:collisionBehavior]; [animator addBehavior:gravityBeahvior];

    3.附着效果

    UIImageView * lview =[[UIImageView alloc]initWithFrame:CGRectMake(20, 120, 80, 80)]; lview.layer.cornerRadius =40; lview.layer.masksToBounds =YES; lview.image =[UIImage imageNamed:@"zc.jpg"]; [self.view addSubview:lview]; UIImageView * wview =[[UIImageView alloc]initWithFrame:CGRectMake(80, 70, 30, 30)]; wview.image =[UIImage imageNamed:@"zc.jpg"]; wview.layer.cornerRadius =15; wview.layer.masksToBounds =YES; [self.view addSubview:wview]; animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UIGravityBehavior* gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[lview,wview]]; [gravityBeahvior setGravityDirection:CGVectorMake(0.0f, 0.5f)]; //property changed after printing of the book UICollisionBehavior * collisionBehavior =[[UICollisionBehavior alloc]initWithItems:@[lview,wview]]; [collisionBehavior setCollisionMode: UICollisionBehaviorModeEverything]; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; CGPoint frogCenter = CGPointMake(260, 60); UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:lview attachedToAnchor:frogCenter]; [animator addBehavior:collisionBehavior]; [animator addBehavior:gravityBeahvior]; [animator addBehavior:attachmentBehavior];

    注:frogCenter是中心点,initWithItem:lview attachedToAnchor:frogCenter:lview以frogCenter为中心点移动。

    4.弹跳效果

    UIImageView * lview =[[UIImageView alloc]initWithFrame:CGRectMake(180, 120, 80, 80)]; lview.layer.cornerRadius =40; lview.layer.masksToBounds =YES; lview.image =[UIImage imageNamed:@"zc.jpg"]; [self.view addSubview:lview]; UIImageView * wview =[[UIImageView alloc]initWithFrame:CGRectMake(80, 70, 30, 30)]; wview.image =[UIImage imageNamed:@"zc.jpg"]; wview.layer.cornerRadius =15; wview.layer.masksToBounds =YES; [self.view addSubview:wview]; animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UIGravityBehavior* gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[lview,wview]]; [gravityBeahvior setGravityDirection:CGVectorMake(0.0f, 0.5f)]; //property changed after printing of the book UICollisionBehavior * collisionBehavior =[[UICollisionBehavior alloc]initWithItems:@[lview,wview]]; [collisionBehavior setCollisionMode: UICollisionBehaviorModeEverything]; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; CGPoint frogCenter = CGPointMake(lview.center.x, lview.center.y); UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:lview attachedToAnchor:frogCenter]; //设置振幅 [attachmentBehavior setFrequency:1.0f]; //校评动画峰值 [attachmentBehavior setDamping:0.1f]; [attachmentBehavior setLength:100.0f]; [animator addBehavior:collisionBehavior]; [animator addBehavior:gravityBeahvior]; [animator addBehavior:attachmentBehavior];

    5.瞬间位移

    lview =[[UIImageView alloc]initWithFrame:CGRectMake(180, 120, 80, 80)]; lview.layer.cornerRadius =40; lview.layer.masksToBounds =YES; lview.image =[UIImage imageNamed:@"zc.jpg"]; [self.view addSubview:lview]; UITapGestureRecognizer * gesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSnapGesture:)]; [self.view addGestureRecognizer:gesture]; } -(IBAction)handleSnapGesture:(UITapGestureRecognizer*)gesture { CGPoint point = [gesture locationInView:self.view]; animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UISnapBehavior* snapBehavior = [[UISnapBehavior alloc] initWithItem:lview snapToPoint:point]; snapBehavior.damping = 3.05f; [animator addBehavior:snapBehavior]; }

    注意:snapBehavior.damping = 3.05f; 数值越大,阻力越大。

    6.推力效果

    lview =[[UIImageView alloc]initWithFrame:CGRectMake(180, 120, 80, 80)]; lview.layer.cornerRadius =40; lview.layer.masksToBounds =YES; lview.image =[UIImage imageNamed:@"zc.jpg"]; [self.view addSubview:lview]; UITapGestureRecognizer * gesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSnapGesture:)]; [self.view addGestureRecognizer:gesture]; animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[lview]]; collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; [animator addBehavior:collisionBehavior]; pushBehavior = [[UIPushBehavior alloc] initWithItems:@[lview] mode:UIPushBehaviorModeInstantaneous]; pushBehavior.angle = 0.0; pushBehavior.magnitude = 0.0; [animator addBehavior:pushBehavior]; } -(IBAction)handleSnapGesture:(UITapGestureRecognizer*)gesture { CGPoint point = [gesture locationInView:self.view]; CGPoint origin = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)); NSLog(@"%f",powf(point.x-origin.x, 2.0)); CGFloat distance = sqrtf(powf(point.x-origin.x, 2.0)+powf(point.y-origin.y, 2.0)); CGFloat angle = atan2(point.y-origin.y,point.x-origin.x); distance = MIN(distance, 100.0); //加速度 [pushBehavior setMagnitude:5]; //角度 [pushBehavior setAngle:angle]; [pushBehavior setActive:TRUE]; }

    注意:powf(point.x-origin.x, 2.0):point.x-origin.x的平方 sqrtf :开根号

    转载请注明原文地址: https://ju.6miu.com/read-1000286.html

    最新回复(0)