“启动"并删除一个 UIView [英] "Launch" and drop a UIView

查看:23
本文介绍了“启动"并删除一个 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上图说明了我想要的:

  • 红色圆圈是一个 UIView 的起点
  • 我想在 X 位置发生变化的情况下向上(Y 位置)发射它
  • 我想让它落入界限内.

我尝试了什么:

对于启动,我可以使用 UIView.animate 函数.对于放置,我可以使用 UIDynamicAnimator.但是有一些问题:

For the launch I can make use of the UIView.animate function. For the drop I can use the UIDynamicAnimator. However there are some problems:

-在 UIView.animate 中,我不能弯曲"动画,只能是一条直线.我可以在这里使用这个答案来画一条曲线:https://stackoverflow.com/a/43813458/7715250

-In the UIView.animate I cannot 'curve' the animation, only a straight line. I can use this answer here to draw a curve line: https://stackoverflow.com/a/43813458/7715250

-将这两个功能结合起来是行不通的.完成 UIView.animate 后,UIView 只是直线向下.

-Combining both functions is not working. After the UIView.animate is done, the UIView just drop straight downwards.

UIDynamicAnimator 的代码:

The code for UIDynamicAnimator:

var animator: UIDynamicAnimator!

    //view did appear
    let view2 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    view2.center = self.view.center
    view2.backgroundColor = .blue
    self.view.addSubview(view2)
    animator = UIDynamicAnimator(referenceView: self.view)
    let gravity = UIGravityBehavior(items: [view2])
    animator.addBehavior(gravity)
    let collosion = UICollisionBehavior(items: [view2])
    collosion.translatesReferenceBoundsIntoBoundary = true
    let dynamic = UIDynamicItemBehavior(items: [view2])
    dynamic.elasticity = 0.7
    animator.addBehavior(collosion)
    animator.addBehavior(dynamic)

这会降低 UIView 的弹跳效果.但是如何启动 UIView 呢?如何更改 X 和 Y 位置并保持添加的行为?

That will drop the UIView with a nice bounce effect. But how to launch the UIView? How to change the X and Y position and remain the added behaviours?

推荐答案

我认为你需要的是 UIPushBehavior.添加这个额外的行为.

I think what you need is UIPushBehavior. Add this extra behavior.

let push = UIPushBehavior(items: [view2], mode: UIPushBehaviorMode.instantaneous)
push.setAngle(CGFloat(-M_PI/2 - 0.1), magnitude: 8.0) // Adjust angle and magnitude
animator.addBehavior(push)

这篇关于“启动"并删除一个 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆