• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - UIDynamics : Spring

[复制链接]
菜鸟教程小白 发表于 2022-12-12 16:07:27 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试在我正在开发的 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap