我正在使用UIDynamics实现一些动画,以在视图之间启用一些基于物理的转换.我想以编程方式暂停和继续这些动画以响应用户的触摸.到目前为止,我发现这样做的唯一方法是从动态动画师中删除行为,然后像这样重新添加它们:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.dynamicAnimator removeBehaviour: self.behaviour1];
[self.dynamicAnimator removeBehaviour: self.behaviour2];
// etc
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.behaviour1 = [[UIGravityBehavior alloc] initWithItems: @[self.item]];
self.behaviour1.gravityDirection = self.gravityVector;
[self.dynamicAnimator addBehaviour: self.behaviour1];
self.behaviour2 = [[UICollisionBehavior alloc] initWithItems: @[self.item]];
[self.behaviour2 addBoundaryWithIdentifier: @"Boundary" forPath: self.boundary];
[self.dynamicAnimator addBehaviour: self.behaviour2];
// etc
}
Run Code Online (Sandbox Code Playgroud)
(旁白:我也使用[self.dynamicAnimator updateItemUsingCurrentState: item]而不是重新创建行为,但我仍然需要从动态动画师中删除它们并再次重新添加它们,以便在用户触摸屏幕时暂停.)
我希望能够做的是self.dynamicAnimator.running = NO暂停,然后self.dynamicAnimator.running = YES继续.这将更加简洁,涉及更少的代码(我目前有五种行为可以做到这一点),并有助于避免过程中的潜在错误.不幸的是,该dynamicAnimator.running财产是只读的,所以这是不可能的.
有没有办法暂停和继续UIDynamicAnimator一两行,我错过了?
我想使用UIKitDynamics UISnapBehaviour来增加按钮的动画(外观+旋转位置变化),该按钮本身位于使用自动布局的视图中.
我知道在应用UIKitDynamics力时我需要暂时禁用按钮的自动布局约束.我在考虑以下过程......
这是正确的方法吗?
哪些委托/布局应该用于那些相应的步骤+如何在实际基于自动布局的动画/转换发生之前从自动布局获取视图的目标中心?
如果我在UIDynamicBehavior视图中添加任何类型,它会在旋转设备时完全破坏.这是肖像画(正确显示):

在这里它是在风景中,所有破坏:
我不相信这是一个自动布局问题,因为如果我删除添加UIDynamicBehavior它的调用它没有自动布局问题.也不会抛出自动布局错误.这是代码:
@interface SWViewController () {
UICollisionBehavior *coll;
UIDynamicAnimator *dynamicAnimator;
}
@implementation SWViewController
- (void)viewDidLoad {
[super viewDidLoad];
dynamicAnimator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self setupCollisions]; // commenting this out fixes the layout
}
- (void)setupCollisions {
NSArray *dynamicViews = @[greenView];
coll = [[UICollisionBehavior alloc] initWithItems:dynamicViews];
CGFloat topBound = CGRectGetMinY(greenView.frame);
[coll addBoundaryWithIdentifier:@"top"
fromPoint:CGPointMake(0, h1)
toPoint:CGPointMake(CGRectGetWidth(greenView.frame), h1)];
[dynamicAnimator addBehavior:coll];
}
Run Code Online (Sandbox Code Playgroud)
如果我覆盖didRotateFromInterfaceOrientation我可以看到顶部边界greenView不遵循autolayout所说的应该(再次,删除调用以setupCollisions修复此问题).
自动布局边界greenView是:
height …Run Code Online (Sandbox Code Playgroud) autolayout ios7 uicollisionbehavior uidynamicbehavior uikit-dynamics
赏金等待!
想象一下UICollectionView,

(显示两个项目)
我正在为UIKit Dynamics使用流行的BPXLFlowLayout(请注意我的评论,如何在iOS 7中复制消息弹跳气泡)
我希望N个项目从屏幕顶部"落入"并在到达时反弹.但,
我希望drop in和bounce使用与集合视图正在使用相同的Dynamic
可接受但不理想的解决方法:只需使用usingSpringWithDamping放入整个视图:
但是当你用手指移动项目时,不使用集合视图使用的相同动力学似乎是"错误的".
(显然,使用SpringWithDamping时,没有"内部弹簧":整个集合视图只是作为一个单元反弹.)
我已经花了几个小时来讨论这个问题:你会认为这就像将列表放在顶部,屏幕外,然后使用scrollToItemAtIndexPath 一样简单:但不,我根本无法完成这项工作.
使用 UIKit Dynamics,我想以某种方式组合 UIAttachmentBehavior 和 UICollisionBehavior,以便用户可以拖动视图(使用 )UIPanGestureRecognizer但不能离开特定区域。
当用户/UIView 与碰撞行为的边界发生碰撞时,问题就会出现,因为此时不可能进行垂直移动。
IE 当与边界区域左侧碰撞时,会被“卡”在那里,无法向上或向下移动,就这样。将 UIView 拖回原来的路径即可。
任何有关 UIDynamicItemBehavior 解决此问题的帮助都值得高度赞赏(尝试过弹性、摩擦和阻力,但无济于事)。

我想拖动一个UIView,让它移动附加到其上的其他视图的位置,就像它们都是通过字符串连接一样.起始状态将是一堆UIViews聚集在一起.如果你拉出一个,它附加的项目将在它与被拖动的视图之间达到最小距离后开始移动.我认为实现这一目标的最佳方法是使用UIDynamicItemBehavior,考虑到我需要将其重新捕捉到适当位置并对其施加权重.如果不做荒谬的代码,我不太清楚如何实现这一点.我所拥有的代码可以在下面看到.不幸的是,我遇到的问题是被拖动的方形项目回到了square2.如果有人有任何建议,我会很乐意澄清是否需要.
- (void)viewDidLoad
{
[super viewDidLoad];
_containmentView = [[UIView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height/2, self.view.frame.size.width, 200)];
[self.view addSubview:_containmentView];
[_containmentView setBackgroundColor:[UIColor greenColor]];
_square = [[UIView alloc]initWithFrame:CGRectMake(200, 0, 100, 100)];
_square.backgroundColor = [UIColor yellowColor];
[_containmentView addSubview:_square];
_square2 = [[UIView alloc]initWithFrame:CGRectMake(100, 0, 100, 100)];
_square2.backgroundColor = [UIColor redColor];
[_containmentView addSubview:_square2];
_animator = [[UIDynamicAnimator alloc]initWithReferenceView:_containmentView];
_gravity = [[UIGravityBehavior alloc]initWithItems:@[_square, _square2]];
_gravity.gravityDirection = CGVectorMake(-1.0, 0.0);
[_animator addBehavior:_gravity];
_collision = [[UICollisionBehavior alloc]initWithItems:@[_square]];
_collision.collisionDelegate = self;
_collision.translatesReferenceBoundsIntoBoundary = YES; //causes the boundary to use the bounds of the reference …Run Code Online (Sandbox Code Playgroud) 最近我使用UIDynamics将图像视图设置为动画.但是,因为它的自动布局y-pos约束被设置为屏幕外,当从屏幕导航然后返回到它时,我的图像视图再次被放置在屏幕外.动画花了大约3秒钟,所以三秒后我就重置了约束.这感觉有点hacky.
所以我的问题是:同时处理自动布局和UIDynamics的正确方法是什么?
在很多情况下你需要"摇动"UIView.
(例如,"吸引儿童用户注意控件","连接速度慢","用户输入错误输入"等等.)
使用UIKit Dynamics可以做到这一点吗?
所以你必须......
采取观点,比如说0,0
添加一个春天的概念
给它一个轻推,对"左"说
它应该在弹簧上向后摆动,最终再次稳定在0,0
这可能吗?我在Apple演示中找不到一个例子.干杯
请注意,正如Niels在下面明确地解释的那样,弹簧不一定是你想要的某些情况的"物理感觉":在其他情况下它可能是完美的.据我所知,iOS自己的应用程序(例如消息等)中的所有物理现在都使用了UIKit Dynamics,所以对我而言,值得一提的是"UIView弹跳弹簧".
为了清楚起见,当然你可以做一些"相似"的事情,只需动画即可.例...
但现在,这与iOS的其他部分没有相同的"物理感觉".
-(void)userInputErrorShake
{
[CATransaction begin];
CAKeyframeAnimation * anim =
[CAKeyframeAnimation animationWithKeyPath:@"transform"];
anim.values = @[
[NSValue valueWithCATransform3D:
CATransform3DMakeTranslation(-4.0f, 0.0f, 0.0f) ],
[NSValue valueWithCATransform3D:
CATransform3DMakeTranslation(4.0f, 0.0f, 0.0f) ]
];
anim.autoreverses = YES;
anim.repeatCount = 1.0f;
anim.duration = 0.1f;
[CATransaction setCompletionBlock:^{}];
[self.layer addAnimation:anim forKey:nil];
[CATransaction commit];
}
Run Code Online (Sandbox Code Playgroud) 当用户点击超级视图时,我通过UITapGestureRecognizer并在函数下方调用它来创建UIImage视图,将其添加到superview,为它添加重力并让它从超级视图中删除.
问题是如果用户在第一个UIImage仍然在屏幕上时再次点击,则第一个图像停止就位,第二个图像UIImage开始下降.让多个UIImage独立于屏幕上的最佳方法是什么?
其次,我觉得,我应该UIImage从superview中删除这些以节省内存(因为可能有数百个点击但我无法弄清楚如何UIImage在屏幕外自动删除它.
请快速帮助.
func dropBall(tapLocation:CGPoint) {
let ball = UIImageView(image: UIImage(named: "White50.png"))
ball.frame = CGRect(origin: CGPoint(x:tapLocation.x-25,y:tapLocation.y-25), size: CGSize(width:50,height:50))
view.addSubview(ball)
animator = UIDynamicAnimator(referenceView: self.view)
gravity = UIGravityBehavior(items: [ball])
animator.addBehavior(gravity)
}
Run Code Online (Sandbox Code Playgroud) 每当圆形视图与故事板上的桨碰撞时,它实际上与视图的矩形框架碰撞.我该如何解决这个问题.我也试过了UIBezierPath,CAShapeLayer似乎没有用.
红色箭头是碰撞点,因为它似乎没有与球碰撞,而是与视图的框架碰撞.一次,用拨片触摸框架,它会反弹,等等.
uikit-dynamics ×10
ios ×9
objective-c ×4
swift ×4
autolayout ×3
ios7 ×2
game-physics ×1
storyboard ×1
uiview ×1