gpt4 book ai didi

ios - 了解 UIDynamics 如何计算位置

转载 作者:行者123 更新时间:2023-11-29 02:26:41 25 4
gpt4 key购买 nike

我试图了解 UIDynamicAnimator 如何计算每次迭代的值。

例如,我有一个简单的示例,其中有一个 UIAttachmentBehavior 和一个 UIDynamicItemBehavior 附加到同一个 DynamicItem

UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:test attachedToAnchor:CGPointMake(50, 50)];
attachmentBehavior.frequency = 3.5;
attachmentBehavior.damping = .4;

UIDynamicItemBehavior *dynamicItemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[test]];
dynamicItemBehavior.resistance = 10;
dynamicItemBehavior.action = ^{
NSLog(@"%f", [dynamicItemBehavior linearVelocityForItem:test]);
};

每当我更改 UIAttachmentBehavioranchorPoint 时,它都会通过 Spring 动画动画到该位置。

我不明白DynamicItemBehavior如何了解此运动,从而可以将其物理属性(阻力、密度等)应用于由不同行为触发的运动?

UIDynamicAnimator 是否以某种方式确定优先级并累积所有行为(速度、阻尼等)的值。

在我的脑海里它会做类似的事情

var velocity;

for behavior in item.behaviors
velocity += behavior.velocity * behavior.damping

item.position = delta_time * velocity

最佳答案

我不确定我是否完全理解您的问题,但几天前我在学习 UIDynamics 时遇到了这个问题。也许它可以帮助你:http://www.raywenderlich.com/76147/uikit-dynamics-tutorial-swift

在页面的一半左右,他谈到了行为在幕后是如何精确计算的:

The method signatures for the dynamic behaviors use the term items rather than views. The only requirement to apply dynamic behavior to an object is that it adopts the UIDynamicItem protocol, as so: protocol

UIDynamicItem : NSObjectProtocol {   
var center: CGPoint { get set }
var bounds: CGRect { get }
var transform: CGAffineTransform { get set } }

The UIDynamicItem protocol gives dynamics read and write access to the center and transform properties, allowing it to move the items based on its internal computations. It also has read access to bounds, which it uses to determine the size of the item. This allows it to create collision boundaries around the perimeter of the item as well as compute the item’s mass when forces are applied. This protocol means that dynamics is not tightly coupled to UIView; indeed there is another UIKit class that isn’t a view but still adopts this protocol: UICollectionViewLayoutAttributes. This allows dynamics to animate items within collection views.

除了我在这里引用的内容之外,还有更多内容。我想,从整体上看,这是一本很好的读物。希望这对您有所帮助!

关于ios - 了解 UIDynamics 如何计算位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441763/

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