OGeek|极客世界-中国程序员成长平台

标题: ios - UIDynamics : Spring [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:07
标题: ios - UIDynamics : Spring

我正在尝试在我正在开发的 iOS 应用程序中创建一些动画。我有一个盒子会掉下来,直到它撞到一个酒吧。我还在方框中添加了一个 bounce 以对栏产生影响。我现在要添加的是杆上的一种行为,因此当盒子碰到杆时, react 是轻微的 Spring 。我尝试添加 UIAttachmentBehavior 但无法弄清楚如何正确实现它。我看过 WWDC 视频和其他视频,但我无法让它在此设置中工作。如果你能在这个例子中告诉我如何实现它,那就太好了。

enter image description here

@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)initWithItemsNSArray *)items objectsNSArray *)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



Best Answer-推荐答案


虽然附件行为非常强大,但我首先建议您尝试将行附加到 snap behavior看看它是否适合你。通常,您需要几种依恋行为(通常是 4 种)来稳定元素。 Snap 通过一个通常更易于使用的行为来提供类似的效果。

作为一个单独的问题,您错误地设置了碰撞。只需将所有 View 添加到碰撞行为中(使用 addItem:)。你不需要创建一堆边界。

关于ios - UIDynamics : Spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19706106/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4