简体   繁体   English

UIDynamics:春季

[英]UIDynamics : Spring

I am trying to create some animation in an iOS app I am developing. 我正在尝试在正在开发的iOS应用中创建一些动画。 I have a box that falls until it collides with a bar. 我有一个跌落的盒子,直到撞到酒吧。 I also added a bounce to the box for the impact on the bar. 我还为该框的影响添加了一个bounce框。 What I am trying to add now is a behavior on the bar so when the box hits the bar the reaction is a slight spring. 我现在要添加的是栏上的行为,因此当盒子碰到栏时,反应是轻微的弹跳。 I tried adding a UIAttachmentBehavior but couldn't figure out how to implement it correctly. 我尝试添加UIAttachmentBehavior但无法弄清楚如何正确实现它。 I have seen the WWDC videos and other but I can not get it to work in this setting. 我已经看过WWDC视频和其他视频,但无法在此设置下使用。 If you could show me in this example how to implement it that would be great. 如果您可以在此示例中向我展示如何实现它,那将是很好的。

在此处输入图片说明

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _mainView = [[UIView alloc] initWithFrame:[[self view] bounds]];
    [_mainView setBackgroundColor:[UIColor clearColor]];
    [[self view] addSubview: _mainView];

    _objectView = 
     [[UIView alloc] initWithFrame:CGRectMake(_mainView.bounds.size.width/2-40, 40,
                                              80, 80)];
    [_objectView setBackgroundColor:[UIColor redColor]];
    [_mainView addSubview: _objectView];

    _barView = 
     [[UIView alloc] initWithFrame:CGRectMake(_mainView.bounds.size.width/2-50, 
                                              (_mainView.bounds.size.height/5) * 4, 
                                              100, 3)];
    [_barView setBackgroundColor:[UIColor blackColor]];
    [_mainView addSubview: _barView];

    //----------------------------------

    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:_mainView];

    BounceCustomBehavior *bouncyBehavior = 
     [[BounceCustomBehavior alloc] 
                     initWithItems:@[_objectView] 
                            objects:[NSArray arrayWithObjects:_barView, nil]];
    [_animator addBehavior:bouncyBehavior];

    //-----------------------------------


}

#import "BounceCustomBehavior.h"

@implementation BounceCustomBehavior
-(instancetype)initWithItems:(NSArray *)items objects:(NSArray *)collisionObjs {
    if (!(self = [super init])) return nil;

    UIGravityBehavior* gravityBehavior = [[UIGravityBehavior alloc] initWithItems:items];
    [self addChildBehavior:gravityBehavior];

    UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:items];
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;

    for (UIView * view in collisionObjs) {
        CGPoint rightEdge = CGPointMake(view.frame.origin.x +
                                        view.frame.size.width, view.frame.origin.y);
        [collisionBehavior addBoundaryWithIdentifier:@""
                                    fromPoint:view.frame.origin
                                      toPoint:rightEdge];
    }
    [self addChildBehavior:collisionBehavior];

    UIDynamicItemBehavior *elasticityBehavior = [[UIDynamicItemBehavior alloc] initWithItems:items];
    elasticityBehavior.elasticity = 0.3f;
    [self addChildBehavior:elasticityBehavior];

    return self;
}
@end

While attachment behaviors are very powerful, I first suggest you try attaching the line to a snap behavior and see if it works for you. 尽管附件行为非常强大,但我首先建议您尝试将行附加到快照行为 ,看看它是否对您有用。 Typically you need several attachment behaviors (often 4) in order to stabilize an item. 通常,您需要几种附着行为(通常为4种)来稳定物品。 Snap gives similar effects with a single, often easier to use, behavior. Snap通过单一的,通常更易于使用的行为提供类似的效果。

As a separate issue, you're setting up your collisions incorrectly. 作为单独的问题,您设置的碰撞不正确。 Just add all of your views to a collision behavior (using addItem: ). 只需将所有视图添加到碰撞行为即可(使用addItem: You don't need to create a bunch of boundaries. 您无需创建一堆边界。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM