gpt4 book ai didi

ios - UISnapBehavior 奇怪的摇晃 - swift

转载 作者:搜寻专家 更新时间:2023-11-01 05:40:03 31 4
gpt4 key购买 nike

我正在尝试使用 UISnapBehavior 创建一个tweening back 动画。

我正在尝试制作一个可拖动的正方形,然后当正方形被释放时,它会补间回到原来的位置。每次它后退时,它似乎都有一个奇怪的轨迹,而不是以正确的角度后退,它似乎在到达目的地之前以一种奇怪的半圆后退。

提前谢谢你。

我的代码如下:

var animator:UIDynamicAnimator? = nil;
var ball : Ball!;
var ball2 : Ball!;
var snap: UISnapBehavior!
var gravity : UIGravityBehavior!;

var speed : Double! = 0;
var lastTouchPoint : CGPoint!;
var attach : UIAttachmentBehavior! = nil;
var itemBehavior : UIDynamicItemBehavior!;
var snapping : UISnapBehavior! = nil;
var currentBall : Ball! = nil;


override init(frame: CGRect) {
super.init(frame: frame);
println( "-- initiated view --" );

self.backgroundColor = UIColor.orangeColor();

ball = Ball( frame: CGRect(x: 200, y: 200, width: 50, height: 50 ) );
self.addSubview(ball);

ball2 = Ball( frame: CGRect(x: 100, y: 100, width: 50, height: 50 ) );
self.addSubview(ball2);

setup()

self.addGestureRecognizer( UIPanGestureRecognizer(target: self, action: "panHandler:") );

self.attach = UIAttachmentBehavior(item: self, attachedToAnchor: CGPoint(x: 0, y: 0) );

}

func setup() {
animator = UIDynamicAnimator(referenceView:self);

var collision: UICollisionBehavior!
collision = UICollisionBehavior(items: [ball, ball2])
collision.translatesReferenceBoundsIntoBoundary = false;
collision.collisionDelegate = self;
animator?.addBehavior(collision)

var dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [ball, ball2] );
dynamicItem.allowsRotation = false;
self.animator!.addBehavior(dynamicItem);
}

func panHandler( recognizr : UIPanGestureRecognizer ) {
switch( recognizr.state ) {
case UIGestureRecognizerState.Began:

let location = recognizr.locationInView(self);
let view : AnyObject? = self.hitTest(location, withEvent: nil);

if( view is Ball ) {

currentBall = view as! Ball;

if( self.attach != nil ){ self.animator!.removeBehavior(attach); self.attach = nil; }


self.attach = UIAttachmentBehavior(item: currentBall, attachedToAnchor: location );
self.animator!.addBehavior(self.attach);
self.attach.damping = 1;
self.attach.length = 1;

let dynamicItem : UIDynamicItemBehavior = UIDynamicItemBehavior(items: [currentBall] );
dynamicItem.allowsRotation = false;

self.animator!.addBehavior(dynamicItem);
}
break;

case UIGestureRecognizerState.Changed :

if( currentBall != nil ) {
let location = recognizr.locationInView(self);
self.attach.anchorPoint = location;
}

break;

default :
println("--- snap ---");

if( self.currentBall == nil ) {return;}

self.animator!.removeBehavior(attach);
self.attach = nil;

var snap : UISnapBehavior = UISnapBehavior(item: self.currentBall, snapToPoint: self.currentBall.startingPoint );
snap.damping = 1;

self.animator!.addBehavior(snap);

break;
}
}

最佳答案

现在是 2018 年,我看到了几乎相同的运动。我认为这是 Apple 实现中的错误,你我都应该提交错误报告,因为这是修复此问题的唯一方法。

根据我的经验,当 damping 高于 1.0 时,这种朝末端的半圆运动中的抖动显得更加明显。

在我下面的示例中,我的手势识别器严格固定了 x 轴值,因此理论上不应该有任何水平移动。

allowsRotation 设置为 false

UISnapBehavior undesired jiggle

关于ios - UISnapBehavior 奇怪的摇晃 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31798290/

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