UISnapBehavior奇怪的摇晃 - 斯威夫特

时间:2015-08-03 23:22:44

标签: ios swift animation uikit-dynamics uidynamicbehavior

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

1 个答案:

答案 0 :(得分:0)

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

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

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

allowsRotation设置为false

UISnapBehavior undesired jiggle