3

我正在尝试使用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;
    }
}
4

1 回答 1

0

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

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

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

allowsRotation设置为false

UISnapBehavior 不受欢迎的抖动

于 2018-04-03T09:59:28.573 回答