gpt4 book ai didi

ios - 是否可以将多个 UIGravityBehaviors 添加到 UIDynamicAnimator?

转载 作者:行者123 更新时间:2023-12-01 17:39:22 24 4
gpt4 key购买 nike

我正在尝试创建一个 View ,其中一些项目沿正常重力矢量落下,而其他项目沿相反矢量落下

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self];

// Normal Gravity
self.gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[ ]];
self.gravityBehavior.gravityDirection = CGVectorMake(0, 1);
[self.animator addBehavior:self.gravityBehavior];

// Inverse Gravity
self.inverseGravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[ ]];
self.inverseGravityBehavior.gravityDirection = CGVectorMake(0, -1);
[self.animator addBehavior:self.inverseGravityBehavior];

我想我可以只在一个行为中添加一些项目,在另一个行为中添加一些项目,但似乎添加第二个重力行为会覆盖第一个?
    [self.gravityBehavior        addItem: ballA];
[self.inverseGravityBehavior addItem: ballB];

这是真的吗,如果是这样,还有其他方法可以达到这种效果吗?

最佳答案

这种行为绝对是可能的。但是,无法通过使用不同 UIGravityBehavior 的动画师来实现。 s。

一种解决方案是使用两个 UIDynamicAnimator实例,每个实例都有自己的 UIGravityBehavior实例。下面是一个 View Controller 的示例,它附加了两个点击手势识别器,一个用于单击,一个用于双击。单曲将添加一个对正常重力作出 react 的 View 。双击添加了一个对倒置重力使用react的 View 。显然,需要对代码进行调整以符合您的特定要求。

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDynamicAnimator *animator;
@property (nonatomic, strong) UIDynamicAnimator *secondAnimator;

@property (nonatomic, strong) UIGravityBehavior *normalGravity;
@property (nonatomic, strong) UIGravityBehavior *inverseGravity;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

self.normalGravity = [[UIGravityBehavior alloc] init];
[self.animator addBehavior:self.normalGravity];

self.secondAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
self.inverseGravity = [[UIGravityBehavior alloc] init];
[self.inverseGravity setAngle:self.normalGravity.angle magnitude:(-1.0 * self.normalGravity.magnitude)];
[self.secondAnimator addBehavior:self.inverseGravity];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)viewWasTapped:(id)sender {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(30, 10, 25, 25)];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
[self.normalGravity addItem:v];
}

- (IBAction)viewWasDoubleTapped:(id)sender {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(90, 400, 25, 25)];
v.backgroundColor = [UIColor greenColor];
[self.view addSubview:v];
[self.inverseGravity addItem:v];
}

@end

第二种选择是使用 UIPushBehavior模拟倒置重力。下面是一个类似设置的 View Controller ,但没有第二个动画师并使用推送行为。
#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIDynamicAnimator *animator;

@property (nonatomic, strong) UIGravityBehavior *normalGravity;
@property (nonatomic, strong) UIPushBehavior *pushBehavior;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

self.normalGravity = [[UIGravityBehavior alloc] init];
[self.animator addBehavior:self.normalGravity];

self.pushBehavior = [[UIPushBehavior alloc] initWithItems:@[] mode:UIPushBehaviorModeContinuous];
[self.pushBehavior setAngle:self.normalGravity.angle magnitude:(-1.0 * self.normalGravity.magnitude)];
[self.animator addBehavior:self.pushBehavior];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)viewWasTapped:(id)sender {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(30, 10, 25, 25)];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
[self.normalGravity addItem:v];
}

- (IBAction)viewWasDoubleTapped:(id)sender {
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(90, 400, 25, 25)];
v.backgroundColor = [UIColor greenColor];
[self.view addSubview:v];

[self.pushBehavior addItem:v];
}

@end

在所有情况下,请确保您正在管理受 UIDynamicAnimator 影响的项目。以及各种行为,这样您就不会不断地添加它们并让模拟陷入困境。

关于ios - 是否可以将多个 UIGravityBehaviors 添加到 UIDynamicAnimator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26597598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com